Search in sources :

Example 1 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class StructuredRecordWritableTest method testComparison.

@Test
public void testComparison() {
    Schema schema = Schema.recordOf("l", Schema.Field.of("l", Schema.of(Schema.Type.LONG)));
    StructuredRecord record1 = StructuredRecord.builder(schema).set("l", 0L).build();
    StructuredRecord record2 = StructuredRecord.builder(schema).set("l", -1L).build();
    StructuredRecordWritable writable1 = new StructuredRecordWritable(record1);
    StructuredRecordWritable writable2 = new StructuredRecordWritable(record2);
    Assert.assertNotEquals(0, writable1.compareTo(writable2));
    Assert.assertNotEquals(writable1, writable2);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 2 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class MultiConnectorSource method transform.

@Override
public void transform(KeyValue<LongWritable, Text> input, Emitter<RecordInfo<StructuredRecord>> emitter) throws Exception {
    StructuredRecord output;
    String inputStr = input.getValue().toString();
    StructuredRecord recordWithSchema = StructuredRecordStringConverter.fromJsonString(inputStr, RECORD_WITH_SCHEMA);
    String stageName = recordWithSchema.get("stageName");
    if (schema == null) {
        Schema outputSchema = Schema.parseJson((String) recordWithSchema.get("schema"));
        output = StructuredRecordStringConverter.fromJsonString((String) recordWithSchema.get("record"), outputSchema);
    } else {
        output = StructuredRecordStringConverter.fromJsonString(inputStr, schema);
    }
    RecordType recordType = RecordType.valueOf((String) recordWithSchema.get("type"));
    emitter.emit(RecordInfo.builder(output, stageName, recordType).build());
}
Also used : RecordType(io.cdap.cdap.etl.common.RecordType) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Example 3 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class SingleConnectorSink method transform.

@Override
public void transform(StructuredRecord input, Emitter<KeyValue<NullWritable, Text>> emitter) throws Exception {
    StructuredRecord modifiedRecord = modifyRecord(input);
    emitter.emit(new KeyValue<>(NullWritable.get(), new Text(StructuredRecordStringConverter.toJsonString(modifiedRecord))));
}
Also used : Text(org.apache.hadoop.io.Text) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Example 4 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class StructuredRecordComparatorTest method testMultipleMatchingSchemasUnion.

@Test
public void testMultipleMatchingSchemasUnion() {
    Schema fullSchema = Schema.recordOf("full", Schema.Field.of("x", INT_SCHEMA), Schema.Field.of("y", Schema.nullableOf(LONG_SCHEMA)));
    Schema subsetSchema = Schema.recordOf("subset", Schema.Field.of("x", INT_SCHEMA));
    Schema unionSchema = Schema.recordOf("u", Schema.Field.of("u", Schema.unionOf(subsetSchema, fullSchema)));
    // These records have the same field values, but different schemas. They should not be equal.
    StructuredRecord fullRecord = StructuredRecord.builder(fullSchema).set("x", 0).build();
    StructuredRecord subsetRecord = StructuredRecord.builder(subsetSchema).set("x", 0).build();
    StructuredRecord r1 = StructuredRecord.builder(unionSchema).set("u", fullRecord).build();
    StructuredRecord r2 = StructuredRecord.builder(unionSchema).set("u", subsetRecord).build();
    testRecordsNotEqual(r1, r2);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 5 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class StructuredRecordComparatorTest method testArraysOfRecords.

@Test
public void testArraysOfRecords() {
    Schema innerSchema = Schema.recordOf("x", Schema.Field.of("string", STRING_SCHEMA), Schema.Field.of("int", INT_SCHEMA));
    Schema arrSchema = Schema.recordOf("arr", Schema.Field.of("r", Schema.arrayOf(innerSchema)));
    StructuredRecord r0 = StructuredRecord.builder(innerSchema).set("string", "0").set("int", 0).build();
    StructuredRecord r1 = StructuredRecord.builder(innerSchema).set("string", "1").set("int", 1).build();
    StructuredRecord r2 = StructuredRecord.builder(innerSchema).set("string", "2").set("int", 2).build();
    StructuredRecord arr0 = StructuredRecord.builder(arrSchema).set("r", Arrays.asList(r0, r1, r2)).build();
    StructuredRecord arr1 = StructuredRecord.builder(arrSchema).set("r", Arrays.asList(r2, r1, r0)).build();
    Assert.assertEquals(0, COMPARATOR.compare(arr0, arr0));
    testRecordsNotEqual(arr0, arr1);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Aggregations

StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)210 Schema (io.cdap.cdap.api.data.schema.Schema)169 Test (org.junit.Test)119 Table (io.cdap.cdap.api.dataset.table.Table)76 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)73 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)73 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)68 ApplicationManager (io.cdap.cdap.test.ApplicationManager)68 ETLBatchConfig (io.cdap.cdap.etl.proto.v2.ETLBatchConfig)59 WorkflowManager (io.cdap.cdap.test.WorkflowManager)54 HashSet (java.util.HashSet)50 ArrayList (java.util.ArrayList)44 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)40 HashMap (java.util.HashMap)25 File (java.io.File)17 ETLPlugin (io.cdap.cdap.etl.proto.v2.ETLPlugin)16 FormatSpecification (io.cdap.cdap.api.data.format.FormatSpecification)15 DataStreamsConfig (io.cdap.cdap.etl.proto.v2.DataStreamsConfig)14 SparkManager (io.cdap.cdap.test.SparkManager)12 Map (java.util.Map)12