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