@Override
public void put(K key, T val) {
try{
Mutation m = new Mutation(new Text(toBytes(key)));
Schema schema = val.getSchema();
StateManager stateManager = val.getStateManager();
Iterator<Field> iter = schema.getFields().iterator();
int count = 0;
for (int i = 0; iter.hasNext(); i++) {
Field field = iter.next();
if (!stateManager.isDirty(val, i)) {
continue;
}
Object o = val.get(i);
Pair<Text,Text> col = mapping.fieldMap.get(field.name());
switch (field.schema().getType()) {
case MAP:
if (o instanceof StatefulMap) {
StatefulMap map = (StatefulMap) o;
Set<?> es = map.states().entrySet();
for (Object entry : es) {
Object mapKey = ((Entry) entry).getKey();
State state = (State) ((Entry) entry).getValue();
switch (state) {
case NEW:
case DIRTY:
m.put(col.getFirst(), new Text(toBytes(mapKey)), new Value(toBytes(map.get(mapKey))));
count++;
break;
case DELETED:
m.putDelete(col.getFirst(), new Text(toBytes(mapKey)));
count++;
break;
}
}
} else {
Map map = (Map) o;
Set<?> es = map.entrySet();
for (Object entry : es) {
Object mapKey = ((Entry) entry).getKey();
Object mapVal = ((Entry) entry).getValue();
m.put(col.getFirst(), new Text(toBytes(mapKey)), new Value(toBytes(mapVal)));
count++;
}
}
break;
case ARRAY:
GenericArray array = (GenericArray) o;
int j = 0;
for (Object item : array) {
m.put(col.getFirst(), new Text(toBytes(j++)), new Value(toBytes(item)));
count++;
}
break;
case RECORD:
SpecificDatumWriter writer = new SpecificDatumWriter(field.schema());
ByteArrayOutputStream os = new ByteArrayOutputStream();
BinaryEncoder encoder = new BinaryEncoder(os);
writer.write(o, encoder);
encoder.flush();
m.put(col.getFirst(), col.getSecond(), new Value(os.toByteArray()));
break;
default:
m.put(col.getFirst(), col.getSecond(), new Value(toBytes(o)));
count++;
}
}