Search in sources :

Example 1 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class MetadataWriterWrapper method write.

@Override
@SuppressWarnings("unchecked")
public void write(Object untypedRecord) throws IOException {
    if (untypedRecord instanceof RecordWithMetadata) {
        RecordWithMetadata record = (RecordWithMetadata) untypedRecord;
        GlobalMetadata globalMetadata = record.getMetadata().getGlobalMetadata();
        metadataCollector.processMetadata(globalMetadata);
        if (RecordWithMetadata.class.isAssignableFrom(writerDataClass)) {
            wrappedWriter.write(record);
        } else {
            wrappedWriter.write(record.getRecord());
        }
    } else {
        metadataCollector.processMetadata(null);
        wrappedWriter.write(untypedRecord);
    }
}
Also used : RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) GlobalMetadata(org.apache.gobblin.metadata.types.GlobalMetadata)

Example 2 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class EncryptedSerializedRecordToSerializedRecordConverterTest method setUp.

@BeforeTest
public void setUp() {
    workUnitState = new WorkUnitState();
    converter = new EncryptedSerializedRecordToSerializedRecordConverter();
    sampleRecord = new RecordWithMetadata<>(new byte[] { 'b', 'c', 'd', 'e' }, new Metadata());
    shiftedValue = new byte[] { 'a', 'b', 'c', 'd' };
    insecureShiftTag = InsecureShiftCodec.TAG;
}
Also used : WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) Metadata(org.apache.gobblin.metadata.types.Metadata) RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class RecordWithMetadataToEnvelopedRecordWithMetadataTest method testSuccessWithString.

@Test
public void testSuccessWithString() throws DataConversionException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    String innerRecord = "abracadabra";
    RecordWithMetadataToEnvelopedRecordWithMetadata converter = new RecordWithMetadataToEnvelopedRecordWithMetadata();
    RecordWithMetadata<String> record = new RecordWithMetadata<>(innerRecord, new Metadata());
    Iterator<RecordWithMetadata<byte[]>> recordWithMetadataIterator = converter.convertRecord("", record, null).iterator();
    RecordWithMetadata recordWithMetadata = recordWithMetadataIterator.next();
    JsonNode parsedElement = objectMapper.readValue((byte[]) recordWithMetadata.getRecord(), JsonNode.class);
    Assert.assertEquals(parsedElement.get("mId").getTextValue(), record.getMetadata().getGlobalMetadata().getId());
    Assert.assertEquals(parsedElement.get("r").getTextValue(), innerRecord);
    Assert.assertEquals(recordWithMetadata.getMetadata().getGlobalMetadata().getContentType(), "lnkd+recordWithMetadata");
    Assert.assertNull(recordWithMetadata.getMetadata().getGlobalMetadata().getInnerContentType());
}
Also used : RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) Metadata(org.apache.gobblin.metadata.types.Metadata) RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

Example 4 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class EnvelopedRecordWithMetadataToRecordWithMetadata method convertRecord.

@Override
public Iterable<RecordWithMetadata<?>> convertRecord(Object outputSchema, RecordWithMetadata<byte[]> inputRecord, WorkUnitState workUnit) throws DataConversionException {
    try {
        try (JsonParser parser = jsonFactory.createJsonParser(inputRecord.getRecord())) {
            parser.setCodec(objectMapper);
            JsonNode jsonNode = parser.readValueAsTree();
            // extracts required record
            if (!jsonNode.has(RECORD_KEY)) {
                throw new DataConversionException("Input data does not have record.");
            }
            String record = jsonNode.get(RECORD_KEY).getTextValue();
            // Extract metadata field
            Metadata md = new Metadata();
            if (jsonNode.has(METADATA_KEY) && jsonNode.get(METADATA_KEY).has(METADATA_RECORD_KEY)) {
                md.getRecordMetadata().putAll(objectMapper.readValue(jsonNode.get(METADATA_KEY).get(METADATA_RECORD_KEY), Map.class));
            }
            return Collections.singleton(new RecordWithMetadata<>(record, md));
        }
    } catch (IOException e) {
        throw new DataConversionException(e);
    }
}
Also used : Metadata(org.apache.gobblin.metadata.types.Metadata) RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) Map(java.util.Map) JsonParser(org.codehaus.jackson.JsonParser)

Example 5 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class MetadataConverterWrapper method convertRecord.

@Override
public Iterable<RecordWithMetadata<DO>> convertRecord(SO outputSchema, Object inputRecord, WorkUnitState workUnit) throws DataConversionException {
    final Metadata metadata = getMetadataFromRecord(inputRecord);
    final DI innerRecord = getRecordFromRecord(inputRecord);
    Iterable<DO> outputRecords = innerConverter.convertRecord(outputSchema, innerRecord, workUnit);
    return Iterables.transform(outputRecords, new Function<DO, RecordWithMetadata<DO>>() {

        @Nullable
        @Override
        public RecordWithMetadata<DO> apply(@Nullable DO input) {
            return new RecordWithMetadata<>(input, metadata);
        }
    });
}
Also used : RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) Metadata(org.apache.gobblin.metadata.types.Metadata) RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) Nullable(javax.annotation.Nullable)

Aggregations

RecordWithMetadata (org.apache.gobblin.type.RecordWithMetadata)19 Metadata (org.apache.gobblin.metadata.types.Metadata)13 Test (org.testng.annotations.Test)11 JsonNode (org.codehaus.jackson.JsonNode)7 WorkUnitState (org.apache.gobblin.configuration.WorkUnitState)6 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)6 HashMap (java.util.HashMap)3 GlobalMetadata (org.apache.gobblin.metadata.types.GlobalMetadata)3 BeforeTest (org.testng.annotations.BeforeTest)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 JsonParser (org.codehaus.jackson.JsonParser)1