Search in sources :

Example 1 with Kafka09JsonSource

use of org.apache.gobblin.source.extractor.extract.kafka.Kafka09JsonSource in project incubator-gobblin by apache.

the class Kafka09JsonIntegrationTest method testHappyPath.

@Test
public void testHappyPath() throws IOException, DataRecordException {
    String topic = "testKafka09JsonSource";
    kafkaTestHelper.provisionTopic(topic);
    SourceState state = createSourceState(topic);
    // Produce a record
    state.setProp(KAFKA_PRODUCER_CONFIG_PREFIX + "bootstrap.servers", "localhost:" + kafkaTestHelper.getKafkaServerPort());
    state.setProp(KAFKA_TOPIC, topic);
    Destination destination = Destination.of(Destination.DestinationType.KAFKA, state);
    Kafka09JsonObjectWriterBuilder writerBuilder = new Kafka09JsonObjectWriterBuilder();
    writerBuilder.writeTo(destination);
    DataWriter<JsonObject> writer = writerBuilder.build();
    final String json = "{\"number\":27}";
    JsonObject record = gson.fromJson(json, JsonObject.class);
    writer.write(record);
    writer.flush();
    writer.close();
    Kafka09JsonSource source = new Kafka09JsonSource();
    List<WorkUnit> workUnitList = source.getWorkunits(state);
    // Test the right value serializer is set
    Assert.assertEquals(state.getProp(Kafka09ConsumerClient.GOBBLIN_CONFIG_VALUE_DESERIALIZER_CLASS_KEY), Kafka09JsonSource.KafkaGsonDeserializer.class.getName());
    // Test there is only one non-empty work unit
    MultiWorkUnitUnpackingIterator iterator = new MultiWorkUnitUnpackingIterator(workUnitList.iterator());
    Assert.assertTrue(iterator.hasNext());
    WorkUnit workUnit = iterator.next();
    Assert.assertEquals(workUnit.getProp(ConfigurationKeys.EXTRACT_TABLE_NAME_KEY), topic);
    Assert.assertFalse(iterator.hasNext());
    // Test extractor
    WorkUnitState workUnitState = new WorkUnitState(workUnit, state);
    final String jsonSchema = "[{\"columnName\":\"number\",\"comment\":\"\",\"isNullable\":\"false\",\"dataType\":{\"type\":\"int\"}}]";
    workUnitState.setProp("source.kafka.json.schema", jsonSchema);
    Extractor<JsonArray, JsonObject> extractor = source.getExtractor(workUnitState);
    Assert.assertEquals(extractor.getSchema().toString(), jsonSchema);
    Assert.assertEquals(extractor.readRecord(null).toString(), json);
}
Also used : Destination(org.apache.gobblin.writer.Destination) SourceState(org.apache.gobblin.configuration.SourceState) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) Kafka09JsonSource(org.apache.gobblin.source.extractor.extract.kafka.Kafka09JsonSource) MultiWorkUnitUnpackingIterator(org.apache.gobblin.runtime.util.MultiWorkUnitUnpackingIterator) WorkUnit(org.apache.gobblin.source.workunit.WorkUnit) Kafka09JsonObjectWriterBuilder(org.apache.gobblin.kafka.writer.Kafka09JsonObjectWriterBuilder) Test(org.testng.annotations.Test)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 SourceState (org.apache.gobblin.configuration.SourceState)1 WorkUnitState (org.apache.gobblin.configuration.WorkUnitState)1 Kafka09JsonObjectWriterBuilder (org.apache.gobblin.kafka.writer.Kafka09JsonObjectWriterBuilder)1 MultiWorkUnitUnpackingIterator (org.apache.gobblin.runtime.util.MultiWorkUnitUnpackingIterator)1 Kafka09JsonSource (org.apache.gobblin.source.extractor.extract.kafka.Kafka09JsonSource)1 WorkUnit (org.apache.gobblin.source.workunit.WorkUnit)1 Destination (org.apache.gobblin.writer.Destination)1 Test (org.testng.annotations.Test)1