Search in sources :

Example 76 with StructuredRecord

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

the class StructuredRecordBuilderTest method testUnionSchemaType.

@Test
public void testUnionSchemaType() {
    Schema schema = Schema.recordOf("x", Schema.Field.of("x", Schema.unionOf(Schema.unionOf(Schema.nullableOf(Schema.of(Schema.LogicalType.TIMESTAMP_MILLIS)), Schema.of(Schema.Type.STRING)), Schema.of(Schema.Type.NULL), Schema.of(Schema.Type.INT), Schema.of(Schema.Type.LONG), Schema.of(Schema.LogicalType.DATE), Schema.nullableOf(Schema.of(Schema.LogicalType.TIME_MILLIS)), Schema.nullableOf(Schema.decimalOf(3)))));
    LocalDate date = LocalDate.of(2018, 11, 11);
    ZonedDateTime zonedDateTime = ZonedDateTime.of(2018, 11, 11, 11, 11, 11, 0, ZoneId.ofOffset("UTC", ZoneOffset.UTC));
    LocalTime time = LocalTime.of(21, 1, 1);
    BigDecimal d = new BigDecimal(new BigInteger("111"), 0);
    Assert.assertEquals(date, StructuredRecord.builder(schema).setDate("x", date).build().getDate("x"));
    StructuredRecord x = StructuredRecord.builder(schema).setTimestamp("x", zonedDateTime).build();
    Assert.assertEquals(zonedDateTime, x.getTimestamp("x"));
    Assert.assertEquals(time, StructuredRecord.builder(schema).setTime("x", time).build().getTime("x"));
    Assert.assertNull(StructuredRecord.builder(schema).setTime("x", null).build().getTime("x"));
    Assert.assertEquals(d, StructuredRecord.builder(schema).setDecimal("x", d).build().getDecimal("x"));
}
Also used : LocalTime(java.time.LocalTime) ZonedDateTime(java.time.ZonedDateTime) Schema(io.cdap.cdap.api.data.schema.Schema) BigInteger(java.math.BigInteger) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 77 with StructuredRecord

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

the class StructuredRecordBuilderTest method testUnexpectedDateType.

@Test
public void testUnexpectedDateType() {
    Schema schema = Schema.recordOf("test", Schema.Field.of("x", Schema.of(Schema.LogicalType.DATE)));
    StructuredRecord record = StructuredRecord.builder(schema).set("x", 5L).build();
    thrown.expect(ClassCastException.class);
    thrown.expectMessage("Field 'x' is expected to be a date");
    record.getDate("x");
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 78 with StructuredRecord

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

the class StructuredRecordBuilderTest method testUnexpectedTimeType.

@Test
public void testUnexpectedTimeType() {
    Schema schema = Schema.recordOf("test", Schema.Field.of("x", Schema.of(Schema.LogicalType.TIME_MICROS)));
    StructuredRecord record = StructuredRecord.builder(schema).set("x", "12:00:00").build();
    thrown.expect(ClassCastException.class);
    thrown.expectMessage("Field 'x' is expected to be a time");
    record.getTime("x");
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 79 with StructuredRecord

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

the class StructuredRecordBuilderTest method testDateTimeSupport.

@Test
public void testDateTimeSupport() {
    String fieldName = "datetimefield";
    Schema schema = Schema.recordOf("test", Schema.Field.of("id", Schema.of(Schema.Type.INT)), Schema.Field.of("name", Schema.of(Schema.Type.STRING)), Schema.Field.of(fieldName, Schema.of(Schema.LogicalType.DATETIME)));
    LocalDateTime localDateTime = LocalDateTime.now();
    StructuredRecord record = StructuredRecord.builder(schema).set("id", 1).set("name", "test").setDateTime(fieldName, localDateTime).build();
    Assert.assertEquals(localDateTime, record.getDateTime(fieldName));
}
Also used : LocalDateTime(java.time.LocalDateTime) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 80 with StructuredRecord

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

the class CombinedLogRecordFormatTest method testCLFLog.

@Test
public void testCLFLog() throws UnsupportedTypeException, UnexpectedFormatException {
    CombinedLogRecordFormat format = new CombinedLogRecordFormat();
    FormatSpecification spec = new FormatSpecification(CombinedLogRecordFormat.class.getCanonicalName(), null, ImmutableMap.of());
    format.initialize(spec);
    String data = "10.10.10.10 - - [01/Feb/2015:06:47:10 +0000] \"GET /browse/COOP-DBT-JOB1-238/artifact HTTP/1.1\"" + " 301 256 \"-\" \"Mozilla/5.0 (compatible; AhrefsBot/5.0; +http://ahrefs.com/robot/)\"";
    StructuredRecord output = format.read(ByteBuffer.wrap(Bytes.toBytes(data)));
    Assert.assertEquals("10.10.10.10", output.get("remote_host"));
    Assert.assertNull(output.get("remote_login"));
    Assert.assertNull(output.get("auth_user"));
    Assert.assertEquals("01/Feb/2015:06:47:10 +0000", output.get("request_time"));
    Assert.assertEquals("GET /browse/COOP-DBT-JOB1-238/artifact HTTP/1.1", output.get("request"));
    Assert.assertEquals(301, (int) output.get("status"));
    Assert.assertEquals(256, (int) output.get("content_length"));
    Assert.assertNull(output.get("referrer"));
    Assert.assertEquals("Mozilla/5.0 (compatible; AhrefsBot/5.0; +http://ahrefs.com/robot/)", output.get("user_agent"));
}
Also used : FormatSpecification(io.cdap.cdap.api.data.format.FormatSpecification) 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