Search in sources :

Example 56 with Utf8

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));
}
Also used : Utf8(org.apache.avro.util.Utf8)

Example 57 with Utf8

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;
}
Also used : KsDef(org.apache.cassandra.thrift.KsDef) Utf8(org.apache.avro.util.Utf8)

Example 58 with Utf8

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()));
        }
    }
}
Also used : Utf8(org.apache.avro.util.Utf8) InvalidRequestException(org.apache.cassandra.thrift.InvalidRequestException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 59 with Utf8

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);
}
Also used : Schema(org.apache.avro.Schema) Utf8(org.apache.avro.util.Utf8) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) BinaryDecoder(org.apache.avro.io.BinaryDecoder)

Example 60 with Utf8

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());
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) Utf8(org.apache.avro.util.Utf8) OutputBuffer(org.apache.cassandra.io.util.OutputBuffer) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Aggregations

Utf8 (org.apache.avro.util.Utf8)123 Test (org.junit.Test)34 WebPage (org.apache.gora.examples.generated.WebPage)32 GenericRecord (org.apache.avro.generic.GenericRecord)17 Schema (org.apache.avro.Schema)14 GenericData (org.apache.avro.generic.GenericData)13 ByteBuffer (java.nio.ByteBuffer)12 HashMap (java.util.HashMap)12 Map (java.util.Map)12 Employee (org.apache.gora.examples.generated.Employee)11 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Field (org.apache.avro.Schema.Field)6 Record (org.apache.avro.generic.GenericData.Record)5 File (java.io.File)4 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)4 Metadata (org.apache.gora.examples.generated.Metadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Iterator (java.util.Iterator)3 List (java.util.List)3