Search in sources :

Example 1 with AvroToCouchbaseTupleConverter

use of org.apache.gobblin.couchbase.converter.AvroToCouchbaseTupleConverter in project incubator-gobblin by apache.

the class CouchbaseWriterTest method testTupleDocumentWrite.

/**
 * Test that a single tuple document can be written successfully.
 * @throws IOException
 * @throws DataConversionException
 * @throws ExecutionException
 * @throws InterruptedException
 */
@Test
public void testTupleDocumentWrite() throws IOException, DataConversionException, ExecutionException, InterruptedException {
    Properties props = new Properties();
    props.setProperty(CouchbaseWriterConfigurationKeys.BUCKET, "default");
    Config config = ConfigFactory.parseProperties(props);
    CouchbaseWriter writer = new CouchbaseWriter(_couchbaseEnvironment, config);
    try {
        Schema dataRecordSchema = SchemaBuilder.record("Data").fields().name("data").type().bytesType().noDefault().name("flags").type().intType().noDefault().endRecord();
        Schema schema = SchemaBuilder.record("TestRecord").fields().name("key").type().stringType().noDefault().name("data").type(dataRecordSchema).noDefault().endRecord();
        GenericData.Record testRecord = new GenericData.Record(schema);
        String testContent = "hello world";
        GenericData.Record dataRecord = new GenericData.Record(dataRecordSchema);
        dataRecord.put("data", ByteBuffer.wrap(testContent.getBytes(Charset.forName("UTF-8"))));
        dataRecord.put("flags", 0);
        testRecord.put("key", "hello");
        testRecord.put("data", dataRecord);
        Converter<Schema, String, GenericRecord, TupleDocument> recordConverter = new AvroToCouchbaseTupleConverter();
        TupleDocument doc = recordConverter.convertRecord("", testRecord, null).iterator().next();
        writer.write(doc, null).get();
        TupleDocument returnDoc = writer.getBucket().get("hello", TupleDocument.class);
        byte[] returnedBytes = new byte[returnDoc.content().value1().readableBytes()];
        returnDoc.content().value1().readBytes(returnedBytes);
        Assert.assertEquals(returnedBytes, testContent.getBytes(Charset.forName("UTF-8")));
        int returnedFlags = returnDoc.content().value2();
        Assert.assertEquals(returnedFlags, 0);
    } finally {
        writer.close();
    }
}
Also used : Config(com.typesafe.config.Config) Schema(org.apache.avro.Schema) Properties(java.util.Properties) TupleDocument(org.apache.gobblin.couchbase.common.TupleDocument) AvroToCouchbaseTupleConverter(org.apache.gobblin.couchbase.converter.AvroToCouchbaseTupleConverter) GenericData(org.apache.avro.generic.GenericData) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Aggregations

Config (com.typesafe.config.Config)1 Properties (java.util.Properties)1 Schema (org.apache.avro.Schema)1 GenericData (org.apache.avro.generic.GenericData)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 TupleDocument (org.apache.gobblin.couchbase.common.TupleDocument)1 AvroToCouchbaseTupleConverter (org.apache.gobblin.couchbase.converter.AvroToCouchbaseTupleConverter)1 Test (org.testng.annotations.Test)1