use of org.apache.gora.persistency.impl.PersistentBase in project gora by apache.
the class CassandraStore method addOrUpdateField.
/**
* Add a field to Cassandra according to its type.
* @param key the key of the row where the field should be added
* @param field the Avro field representing a datum
* @param schema the schema belonging to the particular Avro field
* @param value the field value
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private void addOrUpdateField(K key, Field field, Schema schema, Object value) {
Type type = schema.getType();
// checking if the value to be updated is used for saving union schema
if (!field.name().contains(CassandraStore.UNION_COL_SUFIX)) {
switch(type) {
case STRING:
case BOOLEAN:
case INT:
case LONG:
case BYTES:
case FLOAT:
case DOUBLE:
case FIXED:
this.cassandraClient.addColumn(key, field.name(), value);
break;
case RECORD:
if (value != null) {
if (value instanceof PersistentBase) {
PersistentBase persistentBase = (PersistentBase) value;
try {
byte[] byteValue = AvroSerializerUtil.serializer(persistentBase, schema);
this.cassandraClient.addColumn(key, field.name(), byteValue);
} catch (IOException e) {
LOG.warn(field.name() + " named record could not be serialized.");
}
} else {
LOG.warn("Record with value: " + value.toString() + " not supported for field: " + field.name());
}
} else {
LOG.warn("Setting content of: " + field.name() + " to null.");
String familyName = this.cassandraClient.getCassandraMapping().getFamily(field.name());
this.cassandraClient.deleteColumn(key, familyName, this.cassandraClient.toByteBuffer(field.name()));
}
break;
case MAP:
if (value != null) {
if (value instanceof Map<?, ?>) {
Map<CharSequence, Object> map = (Map<CharSequence, Object>) value;
Schema valueSchema = schema.getValueType();
Type valueType = valueSchema.getType();
if (Type.UNION.equals(valueType)) {
Map<CharSequence, Object> valueMap = new HashMap<>();
for (CharSequence mapKey : map.keySet()) {
Object mapValue = map.get(mapKey);
int valueUnionIndex = getUnionSchema(mapValue, valueSchema);
valueMap.put((mapKey + UNION_COL_SUFIX), valueUnionIndex);
valueMap.put(mapKey, mapValue);
}
map = valueMap;
}
String familyName = this.cassandraClient.getCassandraMapping().getFamily(field.name());
// If map is not super column. We using Avro serializer.
if (!this.cassandraClient.isSuper(familyName)) {
try {
byte[] byteValue = AvroSerializerUtil.serializer(map, schema);
this.cassandraClient.addColumn(key, field.name(), byteValue);
} catch (IOException e) {
LOG.warn(field.name() + " named map could not be serialized.");
}
} else {
this.cassandraClient.addStatefulHashMap(key, field.name(), map);
}
} else {
LOG.warn("Map with value: " + value.toString() + " not supported for field: " + field.name());
}
} else {
// delete map
LOG.warn("Setting content of: " + field.name() + " to null.");
this.cassandraClient.deleteStatefulHashMap(key, field.name());
}
break;
case ARRAY:
if (value != null) {
if (value instanceof DirtyListWrapper<?>) {
DirtyListWrapper fieldValue = (DirtyListWrapper<?>) value;
GenericArray valueArray = new Array(fieldValue.size(), schema);
for (int i = 0; i < fieldValue.size(); i++) {
valueArray.add(i, fieldValue.get(i));
}
this.cassandraClient.addGenericArray(key, field.name(), (GenericArray<?>) valueArray);
} else {
LOG.warn("Array with value: " + value.toString() + " not supported for field: " + field.name());
}
} else {
LOG.warn("Setting content of: " + field.name() + " to null.");
this.cassandraClient.deleteGenericArray(key, field.name());
}
break;
case UNION:
// adding union schema index
String columnName = field.name() + UNION_COL_SUFIX;
String familyName = this.cassandraClient.getCassandraMapping().getFamily(field.name());
if (value != null) {
int schemaPos = getUnionSchema(value, schema);
LOG.debug("Union with value: " + value.toString() + " at index: " + schemaPos + " supported for field: " + field.name());
this.cassandraClient.getCassandraMapping().addColumn(familyName, columnName, columnName);
if (this.cassandraClient.isSuper(familyName)) {
this.cassandraClient.addSubColumn(key, columnName, columnName, schemaPos);
} else {
this.cassandraClient.addColumn(key, columnName, schemaPos);
}
//this.cassandraClient.getCassandraMapping().addColumn(familyName, columnName, columnName);
// adding union value
Schema unionSchema = schema.getTypes().get(schemaPos);
addOrUpdateField(key, field, unionSchema, value);
//this.cassandraClient.addColumn(key, field.name(), value);
} else {
LOG.warn("Setting content of: " + field.name() + " to null.");
if (this.cassandraClient.isSuper(familyName)) {
this.cassandraClient.deleteSubColumn(key, field.name());
} else {
this.cassandraClient.deleteColumn(key, familyName, this.cassandraClient.toByteBuffer(field.name()));
}
}
break;
default:
LOG.warn("Type: " + type.name() + " not considered for field: " + field.name() + ". Please report this to dev@gora.apache.org");
}
}
}
use of org.apache.gora.persistency.impl.PersistentBase in project gora by apache.
the class CassandraStore method getFieldValue.
/**
* For every field within an object, we pass in a field schema, Type and value.
* This enables us to process fields (based on their characteristics)
* preparing them for persistence.
* @param fieldSchema the associated field schema
* @param type the field type
* @param fieldValue the field value.
* @return
*/
private Object getFieldValue(Schema fieldSchema, Type type, Object fieldValue) {
switch(type) {
case RECORD:
PersistentBase persistent = (PersistentBase) fieldValue;
PersistentBase newRecord = (PersistentBase) SpecificData.get().newRecord(persistent, persistent.getSchema());
for (Field member : fieldSchema.getFields()) {
if (member.pos() == 0 || !persistent.isDirty()) {
continue;
}
Schema memberSchema = member.schema();
Type memberType = memberSchema.getType();
Object memberValue = persistent.get(member.pos());
newRecord.put(member.pos(), getFieldValue(memberSchema, memberType, memberValue));
}
fieldValue = newRecord;
break;
case MAP:
Map<?, ?> map = (Map<?, ?>) fieldValue;
fieldValue = map;
break;
case ARRAY:
fieldValue = (List<?>) fieldValue;
break;
case UNION:
// be stored as soon as we get break out.
if (fieldValue != null) {
int schemaPos = getUnionSchema(fieldValue, fieldSchema);
Schema unionSchema = fieldSchema.getTypes().get(schemaPos);
Type unionType = unionSchema.getType();
fieldValue = getFieldValue(unionSchema, unionType, fieldValue);
}
//p.put(fieldPos, fieldValue);
break;
default:
break;
}
return fieldValue;
}
use of org.apache.gora.persistency.impl.PersistentBase in project gora by apache.
the class TestIOUtils method testSerializeDeserialize.
@SafeVarargs
@SuppressWarnings("unchecked")
public static <T> void testSerializeDeserialize(T... objects) throws Exception {
ByteBufferOutputStream os = new ByteBufferOutputStream();
DataOutputStream dos = new DataOutputStream(os);
ByteBufferInputStream is = null;
DataInputStream dis = null;
GoraMapReduceUtils.setIOSerializations(conf, true);
try {
for (T before : objects) {
IOUtils.serialize(conf, dos, before, (Class<T>) before.getClass());
dos.flush();
}
is = new ByteBufferInputStream(os.getBufferList());
dis = new DataInputStream(is);
for (T before : objects) {
T after = IOUtils.deserialize(conf, dis, null, (Class<T>) before.getClass());
if (before instanceof BoolArrayWrapper) {
if (after instanceof BoolArrayWrapper) {
log.info("Before : " + java.util.Arrays.toString(((BoolArrayWrapper) before).arr));
log.info("After : " + java.util.Arrays.toString(((BoolArrayWrapper) after).arr));
}
} else if (before instanceof StringArrayWrapper) {
if (after instanceof StringArrayWrapper) {
log.info("Before : " + java.util.Arrays.toString(((StringArrayWrapper) before).arr));
log.info("After : " + java.util.Arrays.toString(((StringArrayWrapper) after).arr));
}
} else {
log.info("Before : " + before);
log.info("After : " + before);
}
assertEquals(before, after);
if ((before instanceof PersistentBase) && (after instanceof PersistentBase)) {
assertEquals(Arrays.equals(((PersistentBase) before).getDirtyBytes().array(), ((PersistentBase) after).getDirtyBytes().array()), true);
}
}
//assert that the end of input is reached
long skipped = dis.skip(1);
assertEquals(0, skipped);
} catch (EOFException expected) {
//either should throw exception or return 0 as skipped
} finally {
org.apache.hadoop.io.IOUtils.closeStream(dos);
org.apache.hadoop.io.IOUtils.closeStream(os);
org.apache.hadoop.io.IOUtils.closeStream(dis);
org.apache.hadoop.io.IOUtils.closeStream(is);
}
}
Aggregations