use of org.apache.avro.util.Utf8 in project voldemort by voldemort.
the class AvroVersionedGenericSerializertest method testSimpleStringSchema.
public void testSimpleStringSchema() throws Exception {
String jsonSchema = "\"string\"";
AvroVersionedGenericSerializer serializer = new AvroVersionedGenericSerializer(jsonSchema);
String jsonSchema2 = "{\"name\": \"Str\", \"type\": \"string\"}";
AvroVersionedGenericSerializer serializer2 = new AvroVersionedGenericSerializer(jsonSchema2);
Utf8 sample = new Utf8("abc");
byte[] byte1 = serializer.toBytes(sample);
byte[] byte2 = serializer.toBytes("abc");
assertArrayEquals(" should serialize to same value", byte1, byte2);
byte[] byte3 = serializer2.toBytes("abc");
assertArrayEquals(" should serialize to same value", byte1, byte3);
// Test deserialization.
assertEquals("Expected Utf8 Class", Utf8.class, serializer.toObject(byte1).getClass());
assertEquals("Expected Utf8 Class", Utf8.class, serializer2.toObject(byte1).getClass());
assertEquals("Value got after serializing and deserailizing is not the same", sample, serializer.toObject(byte1));
assertEquals("Value got after serializing and deserailizing is not the same", sample, serializer2.toObject(byte3));
}
use of org.apache.avro.util.Utf8 in project eiger by wlloyd.
the class KSMetaData method toAvro.
public org.apache.cassandra.db.migration.avro.KsDef toAvro() {
org.apache.cassandra.db.migration.avro.KsDef ks = new org.apache.cassandra.db.migration.avro.KsDef();
ks.name = new Utf8(name);
ks.strategy_class = new Utf8(strategyClass.getName());
if (strategyOptions != null) {
ks.strategy_options = new HashMap<CharSequence, CharSequence>();
for (Map.Entry<String, String> e : strategyOptions.entrySet()) {
ks.strategy_options.put(new Utf8(e.getKey()), new Utf8(e.getValue()));
}
}
ks.cf_defs = SerDeUtils.createArray(cfMetaData.size(), org.apache.cassandra.db.migration.avro.CfDef.SCHEMA$);
for (CFMetaData cfm : cfMetaData.values()) ks.cf_defs.add(cfm.toAvro());
ks.durable_writes = durableWrites;
return ks;
}
use of org.apache.avro.util.Utf8 in project eiger by wlloyd.
the class AlterTableStatement method applyPropertiesToCfDef.
public static void applyPropertiesToCfDef(CfDef cfDef, CFPropDefs cfProps) throws InvalidRequestException {
if (cfProps.hasProperty(CFPropDefs.KW_COMPARATOR)) {
throw new InvalidRequestException("Can't change CF comparator after creation");
}
if (cfProps.hasProperty(CFPropDefs.KW_COMMENT)) {
cfDef.comment = new Utf8(cfProps.getProperty(CFPropDefs.KW_COMMENT));
}
if (cfProps.hasProperty(CFPropDefs.KW_DEFAULTVALIDATION)) {
try {
cfDef.default_validation_class = new Utf8(cfProps.getValidator().toString());
} catch (ConfigurationException e) {
throw new InvalidRequestException(String.format("Invalid validation type %s", cfProps.getProperty(CFPropDefs.KW_DEFAULTVALIDATION)));
}
}
cfDef.read_repair_chance = cfProps.getPropertyDouble(CFPropDefs.KW_READREPAIRCHANCE, cfDef.read_repair_chance);
cfDef.gc_grace_seconds = cfProps.getPropertyInt(CFPropDefs.KW_GCGRACESECONDS, cfDef.gc_grace_seconds);
cfDef.replicate_on_write = cfProps.getPropertyBoolean(CFPropDefs.KW_REPLICATEONWRITE, cfDef.replicate_on_write);
cfDef.min_compaction_threshold = cfProps.getPropertyInt(CFPropDefs.KW_MINCOMPACTIONTHRESHOLD, cfDef.min_compaction_threshold);
cfDef.max_compaction_threshold = cfProps.getPropertyInt(CFPropDefs.KW_MAXCOMPACTIONTHRESHOLD, cfDef.max_compaction_threshold);
if (!cfProps.compactionStrategyOptions.isEmpty()) {
cfDef.compaction_strategy_options = new HashMap<CharSequence, CharSequence>();
for (Map.Entry<String, String> entry : cfProps.compactionStrategyOptions.entrySet()) {
cfDef.compaction_strategy_options.put(new Utf8(entry.getKey()), new Utf8(entry.getValue()));
}
}
if (!cfProps.compressionParameters.isEmpty()) {
cfDef.compression_options = new HashMap<CharSequence, CharSequence>();
for (Map.Entry<String, String> entry : cfProps.compressionParameters.entrySet()) {
cfDef.compression_options.put(new Utf8(entry.getKey()), new Utf8(entry.getValue()));
}
}
}
use of org.apache.avro.util.Utf8 in project eiger by wlloyd.
the class SerDeUtils method deserializeWithSchema.
/**
* Deserializes a single object as stored along with its Schema by serialize(T). NB: See warnings on serialize(T).
* @param ob An empty object to deserialize into (must not be null).
* @param bytes Array to deserialize from
* @throws IOException
*/
public static <T extends SpecificRecord> T deserializeWithSchema(ByteBuffer bytes, T ob) throws IOException {
BinaryDecoder dec = DIRECT_DECODERS.createBinaryDecoder(ByteBufferUtil.getArray(bytes), null);
Schema writer = Schema.parse(dec.readString(new Utf8()).toString());
SpecificDatumReader<T> reader = new SpecificDatumReader<T>(writer);
reader.setExpected(ob.getSchema());
return reader.read(ob, dec);
}
use of org.apache.avro.util.Utf8 in project eiger by wlloyd.
the class SerDeUtils method serializeWithSchema.
/**
* Serializes a single object along with its Schema. NB: For performance critical areas, it is <b>much</b>
* more efficient to store the Schema independently.
* @param o Object to serialize
*/
public static <T extends SpecificRecord> ByteBuffer serializeWithSchema(T o) throws IOException {
OutputBuffer buff = new OutputBuffer();
BinaryEncoder enc = new BinaryEncoder(buff);
enc.writeString(new Utf8(o.getSchema().toString()));
SpecificDatumWriter<T> writer = new SpecificDatumWriter<T>(o.getSchema());
writer.write(o, enc);
enc.flush();
return ByteBuffer.wrap(buff.asByteArray());
}
Aggregations