SchemaUtils { public static StructType subsetSchema(StructType schema, final List<String> fieldNames) { Seq<StructField> fieldSeq = schema.toTraversable().filter(new AbstractFunction1<StructField, Object>() { @Override public Object apply(StructField field) { return fieldNames.contains(field.name()); } }).toSeq(); StructType subset = DataTypes.createStructType(JavaConversions.seqAsJavaList(fieldSeq)); return subset; }  static StructType stringValueSchema(); static StructType binaryValueSchema(); static StructType subsetSchema(StructType schema, final List<String> fieldNames); static StructType subtractSchema(StructType schema, List<String> subtractFieldNames); static StructType appendFields(StructType from, List<StructField> fields); static Set<InstantiatedComponent> getSchemaComponents(Config config,
      boolean configure, String ...schemaConfigPaths);  }
@Test public void testSubsetSchemaSomeFields() { 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 subset = SchemaUtils.subsetSchema(schema, Lists.newArrayList("field1", "field3")); assertEquals(subset.fields().length, 2); assertEquals(subset.fields()[0].name(), "field1"); assertEquals(subset.fields()[1].name(), "field3"); }
@Test public void testSubsetSchemaAllFields() { 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 subset = SchemaUtils.subsetSchema(schema, Lists.newArrayList("field1", "field2", "field3")); assertEquals(subset.fields().length, 3); assertEquals(subset.fields()[0].name(), "field1"); assertEquals(subset.fields()[1].name(), "field2"); assertEquals(subset.fields()[2].name(), "field3"); }
@Test public void testSubsetSchemaNoFields() { 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 subset = SchemaUtils.subsetSchema(schema, Lists.<String>newArrayList()); assertEquals(subset.fields().length, 0); }