Scala - Как получить все имена полей из схемы Avro? - PullRequest
0 голосов
/ 15 мая 2018

У меня есть пример схемы Avro:

{
    "type": "record",
    "name": "wpmcn.MyPair",
    "doc": "A pair of strings",
    "fields": [
        {"name": "left", "type": "string"},
        {"name": "right", "type": "string"}
    ]
}

В Java это был бы способ получить все имена полей:

public static void main(String[] args) throws IOException {
    Schema schema =
         new Schema.Parser().parse(AvroTest.class.getClassLoader().
           getResourceAsStream("pair.avsc"));

     //Collect all field values to an array list
     List<String> fieldValues = new ArrayList<>();
     for(Field field : schema.getFields()) {
         fieldValues.add(field.name());
     }

     //Iterate the arraylist
     for(String value: fieldValues)  {
         System.out.println(value);
     }
}

Как мне сделать то же самое, используяScala?

1 Ответ

0 голосов
/ 19 мая 2018
val myAvroSchemaText= io.Source.fromInputStream(getClass.getResourceAsStream("pair.avsc")).mkString
val avroSchema = new Schema.Parser().parse(myAvroSchemaText)

avroSchema.getFields().foreach {
  f =>
    println(f.name)
}
...