public static Schema schemaForStructType(StructType structType){
  List<String> fieldNames=Lists.newArrayList();
  List<String> fieldTypes=Lists.newArrayList();
  for (  StructField field : structType.fields()) {
    fieldNames.add(field.name());
    DataType fieldType=field.dataType();
    if (fieldType.equals(DataTypes.StringType)) {
      fieldTypes.add("string");
    }
 else     if (fieldType.equals(DataTypes.FloatType)) {
      fieldTypes.add("float");
    }
 else     if (fieldType.equals(DataTypes.DoubleType)) {
      fieldTypes.add("double");
    }
 else     if (fieldType.equals(DataTypes.IntegerType)) {
      fieldTypes.add("int");
    }
 else     if (fieldType.equals(DataTypes.LongType)) {
      fieldTypes.add("long");
    }
 else     if (fieldType.equals(DataTypes.BooleanType)) {
      fieldTypes.add("boolean");
    }
 else {
      throw new RuntimeException("Unsupported Spark SQL field type: " + fieldType);
    }
  }
  return RecordUtils.schemaFor(fieldNames,fieldTypes);
}

public static Schema schemaForStructType ( StructType structType ) { List < String > fieldNames = Lists . newArrayList ( ) ; List < String > fieldTypes = Lists . newArrayList ( ) ; for ( StructField field : structType . fields ( ) ) { fieldNames . add ( field . name ( ) ) ; DataType fieldType = field . dataType ( ) ; if ( fieldType . equals ( DataTypes . StringType ) ) { fieldTypes . add ( "string" ) ; } else if ( fieldType . equals ( DataTypes . FloatType ) ) { fieldTypes . add ( "float" ) ; } else if ( fieldType . equals ( DataTypes . DoubleType ) ) { fieldTypes . add ( "double" ) ; } else if ( fieldType . equals ( DataTypes . IntegerType ) ) { fieldTypes . add ( "int" ) ; } else if ( fieldType . equals ( DataTypes . LongType ) ) { fieldTypes . add ( "long" ) ; } else if ( fieldType . equals ( DataTypes . BooleanType ) ) { fieldTypes . add ( "boolean" ) ; } else { throw new RuntimeException ( "Unsupported Spark SQL field type: " + fieldType ) ; } } return RecordUtils . schemaFor ( fieldNames , fieldTypes ) ; }
the equivalent avro schema for the given spark sql schema .
