use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.
the class RecordPutTransformerTest method testOutputSchemaUsage.
@Test
public void testOutputSchemaUsage() {
Schema outputSchema = Schema.recordOf("output", Schema.Field.of("id", Schema.of(Schema.Type.LONG)), Schema.Field.of("name", Schema.of(Schema.Type.STRING)));
Schema inputSchema = Schema.recordOf("input", Schema.Field.of("id", Schema.of(Schema.Type.LONG)), Schema.Field.of("name", Schema.of(Schema.Type.STRING)), Schema.Field.of("age", Schema.of(Schema.Type.INT)));
StructuredRecord record = StructuredRecord.builder(inputSchema).set("id", 123L).set("name", "ABC").set("age", 10).build();
RecordPutTransformer transformer = new RecordPutTransformer("id", outputSchema);
Put put = transformer.toPut(record);
Assert.assertEquals(1, put.getValues().size());
transformer = new RecordPutTransformer("id", inputSchema);
put = transformer.toPut(record);
Assert.assertEquals(2, put.getValues().size());
}
use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.
the class StructuredRecordStringConverterTest method testTimeLogicalTypeConversion.
@Test
public void testTimeLogicalTypeConversion() throws Exception {
Schema dateSchema = Schema.recordOf("date", Schema.Field.of("id", Schema.of(Schema.Type.INT)), Schema.Field.of("name", Schema.nullableOf(Schema.of(Schema.Type.STRING))), Schema.Field.of("time", Schema.nullableOf(Schema.of(Schema.LogicalType.TIME_MILLIS))));
StructuredRecord record = StructuredRecord.builder(dateSchema).set("id", 1).set("name", "alice").setTime("time", LocalTime.now()).build();
String jsonOfRecord = StructuredRecordStringConverter.toJsonString(record);
StructuredRecord recordOfJson = StructuredRecordStringConverter.fromJsonString(jsonOfRecord, dateSchema);
assertRecordsEqual(record, recordOfJson);
}
use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.
the class StructuredRecordStringConverterTest method testDatetimeLogicalTypeConversion.
@Test
public void testDatetimeLogicalTypeConversion() throws Exception {
Schema dateSchema = Schema.recordOf("date", Schema.Field.of("id", Schema.of(Schema.Type.INT)), Schema.Field.of("name", Schema.nullableOf(Schema.of(Schema.Type.STRING))), Schema.Field.of("datetimefield", Schema.nullableOf(Schema.of(Schema.LogicalType.DATETIME))));
StructuredRecord record = StructuredRecord.builder(dateSchema).set("id", 1).set("name", "alice").setDateTime("datetimefield", LocalDateTime.now()).build();
String jsonOfRecord = StructuredRecordStringConverter.toJsonString(record);
StructuredRecord recordOfJson = StructuredRecordStringConverter.fromJsonString(jsonOfRecord, dateSchema);
assertRecordsEqual(record, recordOfJson);
}
use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.
the class StructuredRecordStringConverterTest method testTimestampLogicalTypeConversion.
@Test
public void testTimestampLogicalTypeConversion() throws Exception {
Schema dateSchema = Schema.recordOf("date", Schema.Field.of("id", Schema.of(Schema.Type.INT)), Schema.Field.of("name", Schema.nullableOf(Schema.of(Schema.Type.STRING))), Schema.Field.of("timestamp", Schema.nullableOf(Schema.of(Schema.LogicalType.TIMESTAMP_MILLIS))));
StructuredRecord record = StructuredRecord.builder(dateSchema).set("id", 1).set("name", "alice").setTimestamp("timestamp", ZonedDateTime.now()).build();
String jsonOfRecord = StructuredRecordStringConverter.toJsonString(record);
StructuredRecord recordOfJson = StructuredRecordStringConverter.fromJsonString(jsonOfRecord, dateSchema);
assertRecordsEqual(record, recordOfJson);
}
use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.
the class StructuredRecordStringConverterTest method testDelimitedWithNullsConversion.
@Test
public void testDelimitedWithNullsConversion() {
Schema schema = Schema.recordOf("x", Schema.Field.of("x", Schema.nullableOf(Schema.of(Schema.Type.STRING))), Schema.Field.of("y", Schema.nullableOf(Schema.of(Schema.Type.INT))));
StructuredRecord record = StructuredRecord.builder(schema).set("x", "abc").build();
Assert.assertEquals("abc,", StructuredRecordStringConverter.toDelimitedString(record, ","));
Assert.assertEquals(record, StructuredRecordStringConverter.fromDelimitedString("abc,", ",", schema));
record = StructuredRecord.builder(schema).set("y", 5).build();
Assert.assertEquals(",5", StructuredRecordStringConverter.toDelimitedString(record, ","));
Assert.assertEquals(record, StructuredRecordStringConverter.fromDelimitedString(",5", ",", schema));
}
Aggregations