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