Row implements Serializable { public static Builder withSchema(Schema schema) { return new Builder(schema); } Row(Schema schema); @Nullable @SuppressWarnings("TypeParameterUnusedInFormals") abstract T getValue(int fieldIdx); abstract int getFieldCount(); abstract List<Object> getValues(); List<Object> getBaseValues(); @Nullable @SuppressWarnings("TypeParameterUnusedInFormals") T getValue(String fieldName); @Nullable Byte getByte(String fieldName); byte @Nullable [] getBytes(String fieldName); @Nullable Short getInt16(String fieldName); @Nullable Integer getInt32(String fieldName); @Nullable Long getInt64(String fieldName); @Nullable BigDecimal getDecimal(String fieldName); @Nullable Float getFloat(String fieldName); @Nullable Double getDouble(String fieldName); @Nullable String getString(String fieldName); @Nullable ReadableDateTime getDateTime(String fieldName); @Nullable Boolean getBoolean(String fieldName); @Nullable Collection<T> getArray(String fieldName); @Nullable Iterable<T> getIterable(String fieldName); @Nullable Map<T1, T2> getMap(String fieldName); @Nullable T getLogicalTypeValue(String fieldName, Class<T> clazz); @Nullable T getBaseValue(String fieldName, Class<T> clazz); @Nullable Object getBaseValue(String fieldName); @Nullable Row getRow(String fieldName); @Nullable Byte getByte(int idx); byte @Nullable [] getBytes(int idx); @Nullable Short getInt16(int idx); @Nullable Integer getInt32(int idx); @Nullable Float getFloat(int idx); @Nullable Double getDouble(int idx); @Nullable Long getInt64(int idx); @Nullable String getString(int idx); @Nullable ReadableDateTime getDateTime(int idx); @Nullable BigDecimal getDecimal(int idx); @Nullable Boolean getBoolean(int idx); @Nullable Collection<T> getArray(int idx); @Nullable Iterable<T> getIterable(int idx); @Nullable Map<T1, T2> getMap(int idx); @Nullable T getLogicalTypeValue(int idx, Class<T> clazz); @Nullable T getBaseValue(int idx, Class<T> clazz); @Nullable Object getBaseValue(int idx); @Nullable Row getRow(int idx); Schema getSchema(); @Override boolean equals(@Nullable Object o); @Override int hashCode(); @Override String toString(); static Builder withSchema(Schema schema); static FieldValueBuilder fromRow(Row row); static Collector<T, List<Object>, Row> toRow(Schema schema); static Row nullRow(Schema schema);  }
@Test public void testCreateWithNames() { Schema type = Stream.of( Schema.Field.of("f_str", FieldType.STRING), Schema.Field.of("f_byte", FieldType.BYTE), Schema.Field.of("f_short", FieldType.INT16), Schema.Field.of("f_int", FieldType.INT32), Schema.Field.of("f_long", FieldType.INT64), Schema.Field.of("f_float", FieldType.FLOAT), Schema.Field.of("f_double", FieldType.DOUBLE), Schema.Field.of("f_decimal", FieldType.DECIMAL), Schema.Field.of("f_boolean", FieldType.BOOLEAN), Schema.Field.of("f_datetime", FieldType.DATETIME), Schema.Field.of("f_bytes", FieldType.BYTES), Schema.Field.of("f_array", FieldType.array(FieldType.STRING)), Schema.Field.of("f_iterable", FieldType.iterable(FieldType.STRING)), Schema.Field.of("f_map", FieldType.map(FieldType.STRING, FieldType.STRING))) .collect(toSchema()); DateTime dateTime = new DateTime().withDate(1979, 03, 14).withTime(1, 2, 3, 4).withZone(DateTimeZone.UTC); byte[] bytes = new byte[] {1, 2, 3, 4}; Row row = Row.withSchema(type) .withFieldValue("f_str", "str1") .withFieldValue("f_byte", (byte) 42) .withFieldValue("f_short", (short) 43) .withFieldValue("f_int", (int) 44) .withFieldValue("f_long", (long) 45) .withFieldValue("f_float", (float) 3.14) .withFieldValue("f_double", (double) 3.141) .withFieldValue("f_decimal", new BigDecimal(3.1415)) .withFieldValue("f_boolean", true) .withFieldValue("f_datetime", dateTime) .withFieldValue("f_bytes", bytes) .withFieldValue("f_array", Lists.newArrayList("one", "two")) .withFieldValue("f_iterable", Lists.newArrayList("one", "two", "three")) .withFieldValue("f_map", ImmutableMap.of("hello", "goodbye", "here", "there")) .build(); Row expectedRow = Row.withSchema(type) .addValues( "str1", (byte) 42, (short) 43, (int) 44, (long) 45, (float) 3.14, (double) 3.141, new BigDecimal(3.1415), true, dateTime, bytes, Lists.newArrayList("one", "two"), Lists.newArrayList("one", "two", "three"), ImmutableMap.of("hello", "goodbye", "here", "there")) .build(); assertEquals(expectedRow, row); }
@Test public void testCreateWithCollectionNames() { Schema type = Stream.of( Schema.Field.of("f_array", FieldType.array(FieldType.INT32)), Schema.Field.of("f_iterable", FieldType.iterable(FieldType.INT32)), Schema.Field.of("f_map", FieldType.map(FieldType.STRING, FieldType.STRING))) .collect(toSchema()); Row row = Row.withSchema(type) .withFieldValue("f_array", ImmutableList.of(1, 2, 3)) .withFieldValue("f_iterable", ImmutableList.of(1, 2, 3)) .withFieldValue("f_map", ImmutableMap.of("one", "two")) .build(); Row expectedRow = Row.withSchema(type) .addValues( ImmutableList.of(1, 2, 3), ImmutableList.of(1, 2, 3), ImmutableMap.of("one", "two")) .build(); assertEquals(expectedRow, row); }
@Test public void testThrowsForIncorrectNumberOfFields() { Schema type = Stream.of( Schema.Field.of("f_int", FieldType.INT32), Schema.Field.of("f_str", FieldType.STRING), Schema.Field.of("f_double", FieldType.DOUBLE)) .collect(toSchema()); thrown.expect(IllegalArgumentException.class); Row.withSchema(type).addValues(1, "2").build(); }
@Test public void testByteArrayEquality() { byte[] a0 = new byte[] {1, 2, 3, 4}; byte[] b0 = new byte[] {1, 2, 3, 4}; Schema schema = Schema.of(Schema.Field.of("bytes", Schema.FieldType.BYTES)); Row a = Row.withSchema(schema).addValue(a0).build(); Row b = Row.withSchema(schema).addValue(b0).build(); assertEquals(a, b); }
@Test public void testByteBufferEquality() { byte[] a0 = new byte[] {1, 2, 3, 4}; byte[] b0 = new byte[] {1, 2, 3, 4}; Schema schema = Schema.of(Schema.Field.of("bytes", Schema.FieldType.BYTES)); Row a = Row.withSchema(schema).addValue(ByteBuffer.wrap(a0)).build(); Row b = Row.withSchema(schema).addValue(ByteBuffer.wrap(b0)).build(); assertEquals(a, b); }
@Test(expected = IllegalArgumentException.class) public void testLogicalTypeWithInvalidInputValueByFieldName() { Schema schema = Schema.builder().addLogicalTypeField("char", FixedBytes.of(10)).build(); byte[] byteArrayWithLengthFive = {1, 2, 3, 4, 5}; Row row = Row.withSchema(schema).withFieldValue("char", byteArrayWithLengthFive).build(); }
@Test(expected = IllegalArgumentException.class) public void testLogicalTypeWithInvalidInputValueByFieldIndex() { Schema schema = Schema.builder().addLogicalTypeField("char", FixedBytes.of(10)).build(); byte[] byteArrayWithLengthFive = {1, 2, 3, 4, 5}; Row row = Row.withSchema(schema).addValues(byteArrayWithLengthFive).build(); }