RowUtils { public static Row subsetRow(Row row, StructType subsetSchema) { Object[] values = new Object[subsetSchema.length()]; int i = 0; for (String fieldName : subsetSchema.fieldNames()) { values[i] = row.get(row.fieldIndex(fieldName)); i++; } Row subset = new RowWithSchema(subsetSchema, values); return subset; }  static Object toRowValue(Object item, DataType type, Map<RowUtils.RowValueMetadata, Object> metadata); static Object toRowValue(Object item, DataType type); static Row subsetRow(Row row, StructType subsetSchema); static Row set(Row row, String fieldName, Object replacement); static Row append(Row row, Object value); static Row append(Row row, String fieldName, DataType fieldType, boolean nullable, Object value); static Row append(Row row, Row append); static Row remove(Row row, String fieldName); static Row remove(Row row, List<String> fieldNames); static Object[] valuesFor(Row row); static boolean different(Row first, Row second, List<String> valueFieldNames); static Column[] toColumnArray(List<String> columnList);  }
@Test public void testSubsetRowAllFields() { StructField field1 = DataTypes.createStructField("field1", DataTypes.StringType, true); StructField field2 = DataTypes.createStructField("field2", DataTypes.IntegerType, true); StructField field3 = DataTypes.createStructField("field3", DataTypes.FloatType, true); StructType schema = DataTypes.createStructType(Lists.newArrayList(field1, field2, field3)); Row row = new RowWithSchema(schema, "hello", 1, 2.0); Row subsetRow = RowUtils.subsetRow(row, schema); assertEquals(row, subsetRow); }
@Test public void testSubsetRowSomeFields() { StructField field1 = DataTypes.createStructField("field1", DataTypes.StringType, true); StructField field2 = DataTypes.createStructField("field2", DataTypes.IntegerType, true); StructField field3 = DataTypes.createStructField("field3", DataTypes.FloatType, true); StructType schema = DataTypes.createStructType(Lists.newArrayList(field1, field2, field3)); StructType subsetSchema = DataTypes.createStructType(Lists.newArrayList(field1, field3)); Row row = new RowWithSchema(schema, "hello", 1, 2.0); Row subsetRow = RowUtils.subsetRow(row, subsetSchema); assertEquals(subsetRow.length(), 2); assertEquals(subsetRow.get(0), "hello"); assertEquals(subsetRow.get(1), 2.0); }
@Test public void testSubsetRowNoFields() { StructField field1 = DataTypes.createStructField("field1", DataTypes.StringType, true); StructField field2 = DataTypes.createStructField("field2", DataTypes.IntegerType, true); StructField field3 = DataTypes.createStructField("field3", DataTypes.FloatType, true); StructType schema = DataTypes.createStructType(Lists.newArrayList(field1, field2, field3)); StructType subsetSchema = DataTypes.createStructType(Lists.<StructField>newArrayList()); Row row = new RowWithSchema(schema, "hello", 1, 2.0); Row subsetRow = RowUtils.subsetRow(row, subsetSchema); assertEquals(subsetRow.length(), 0); }