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();
}
}
Aggregations