.addFloatField("f_float") .addDoubleField("f_double") .addBooleanField("f_boolean") .addStringField("f_string") .addDecimalField("f_decimal")
@Test public void testPrimitiveTypes() throws Exception { Schema schema = Schema.builder() .addByteField("f_byte") .addInt16Field("f_int16") .addInt32Field("f_int32") .addInt64Field("f_int64") .addDecimalField("f_decimal") .addFloatField("f_float") .addDoubleField("f_double") .addStringField("f_string") .addDateTimeField("f_datetime") .addBooleanField("f_boolean") .build(); DateTime dateTime = new DateTime().withDate(1979, 03, 14).withTime(1, 2, 3, 4).withZone(DateTimeZone.UTC); Row row = Row.withSchema(schema) .addValues( (byte) 0, (short) 1, 2, 3L, new BigDecimal(2.3), 1.2f, 3.0d, "str", dateTime, false) .build(); CoderProperties.coderDecodeEncodeEqual(RowCoder.of(schema), row); }
.addStringField("f_string") .addDateTimeField("f_datetime") .addBooleanField("f_boolean") .build();
.addStringField("c_varchar_1") .addStringField("c_varchar_2") .addBooleanField("c_boolean_false") .addBooleanField("c_boolean_true") .addStringField("s_string_1") .addStringField("s_string_2")
.addStringField("f_string") .addDateTimeField("f_datetime") .addBooleanField("f_boolean") .build(); assertEquals(10, schema.getFieldCount());
@Test @Category(NeedsRunner.class) public void testParsesRows() throws Exception { Schema personSchema = Schema.builder() .addStringField("name") .addInt32Field("height") .addBooleanField("knowsJavascript") .build(); PCollection<String> jsonPersons = pipeline.apply( "jsonPersons", Create.of( jsonPerson("person1", "80", "true"), jsonPerson("person2", "70", "false"), jsonPerson("person3", "60", "true"), jsonPerson("person4", "50", "false"), jsonPerson("person5", "40", "true"))); PCollection<Row> personRows = jsonPersons.apply(JsonToRow.withSchema(personSchema)).setRowSchema(personSchema); PAssert.that(personRows) .containsInAnyOrder( row(personSchema, "person1", 80, true), row(personSchema, "person2", 70, false), row(personSchema, "person3", 60, true), row(personSchema, "person4", 50, false), row(personSchema, "person5", 40, true)); pipeline.run(); }
@Test public void testParsesRows() throws Exception { Schema personSchema = Schema.builder() .addStringField("name") .addInt32Field("height") .addBooleanField("knowsJavascript") .build(); PCollection<String> jsonPersons = pipeline.apply( Create.of( jsonPerson("person1", "80", "true"), jsonPerson("person2", "70", "false"), jsonPerson("person3", "60", "true"), jsonPerson("person4", "50", "false"), jsonPerson("person5", "40", "true"))); Schema resultSchema = Schema.builder().addInt32Field("avg_height").build(); PCollection<Row> sqlResult = jsonPersons .apply(JsonToRow.withSchema(personSchema)) .apply(SqlTransform.query("SELECT AVG(height) as avg_height FROM PCOLLECTION")); PAssert.that(sqlResult).containsInAnyOrder(row(resultSchema, 60)); pipeline.run(); }
@Test public void testIsNan() throws Exception { Schema resultType = Schema.builder() .addBooleanField("field_1") .addBooleanField("field_2") .addBooleanField("field_3") .addBooleanField("field_4") .build(); Row resultRow = Row.withSchema(resultType).addValues(false, false, true, true).build(); String sql = "SELECT IS_NAN(f_float_2), IS_NAN(f_double_2), IS_NAN(f_float_3), IS_NAN(f_double_3) FROM PCOLLECTION"; PCollection<Row> result = boundedInputFloatDouble.apply("testUdf", SqlTransform.query(sql)); PAssert.that(result).containsInAnyOrder(resultRow); pipeline.run().waitUntilFinish(); }
@Test public void testIsInf() throws Exception { Schema resultType = Schema.builder() .addBooleanField("field_1") .addBooleanField("field_2") .addBooleanField("field_3") .addBooleanField("field_4") .build(); Row resultRow = Row.withSchema(resultType).addValues(true, true, true, true).build(); String sql = "SELECT IS_INF(f_float_1), IS_INF(f_double_1), IS_INF(f_float_2), IS_INF(f_double_2) FROM PCOLLECTION"; PCollection<Row> result = boundedInputFloatDouble.apply("testUdf", SqlTransform.query(sql)); PAssert.that(result).containsInAnyOrder(resultRow); pipeline.run().waitUntilFinish(); }