Search in sources :

Example 61 with StructuredRecord

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());
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Put(io.cdap.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 62 with StructuredRecord

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);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 63 with StructuredRecord

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);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 64 with StructuredRecord

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);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 65 with StructuredRecord

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