SchemaUtils { public static StructType subtractSchema(StructType schema, List<String> subtractFieldNames) { List<String> fieldNames = Lists.newArrayList(); for (StructField schemaField : schema.fields()) { if (!subtractFieldNames.contains(schemaField.name())) { fieldNames.add(schemaField.name()); } } StructType subtracted = subsetSchema(schema, fieldNames); return subtracted; }  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 testSubtractSchemaSomeFields() { 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.subtractSchema(schema, Lists.newArrayList("field1", "field3")); assertEquals(subset.fields().length, 1); assertEquals(subset.fields()[0].name(), "field2"); }
@Test public void testSubtractSchemaAllFields() { 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.subtractSchema(schema, Lists.newArrayList("field1", "field2", "field3")); assertEquals(subset.fields().length, 0); }
@Test public void testSubtractSchemaNoFields() { 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.subtractSchema(schema, Lists.<String>newArrayList()); assertEquals(subset.fields().length, 3); assertEquals(subset.fields()[0].name(), "field1"); assertEquals(subset.fields()[1].name(), "field2"); assertEquals(subset.fields()[2].name(), "field3"); }