Храните каждый элемент Nested Json как отдельный столбец в датараме - PullRequest
0 голосов
/ 09 июня 2019

json [{"entryLevel": {"a": "0", "b": "1", "c": "3", d: [], e: []}}]

Вывод: - все данные поступают в один столбец с массивом Структуры + --------------------------------------------- + | entryLevel | + --------------------------------------------- + | [WrappedArray (), 1,2,0,3, WrappedArray ()]

Ожидаемый результат: - Каждый элемент представлен в виде отдельного столбца. + --------------------------------------------- + | entryLevel | ABCDE + --------------------------------------------- + | [WrappedArray (), 1,2,0,3, WrappedArray ()] 0 1 3 [] []

Я определил Jsonschema как схему для json

public static final StructType ObjectSchema  =  new StructType(new StructField[]{
            new StructField("d",new ArrayType(DataTypes.StringType, true), true, Metadata.empty()),
            new StructField("a",DataTypes.StringType, true, Metadata.empty()),
            new StructField("b",DataTypes.StringType, true, Metadata.empty()),
            new StructField("c",DataTypes.StringType, true, Metadata.empty()),
            new StructField("e",new ArrayType(DataTypes.StringType, true), true, Metadata.empty()),
        });


public static final StructType Jsonschema  =  new StructType(new StructField[] {         new StructField("entryLevel",ObjectSchema, true, Metadata.empty())});

DataFrame parsedjson = sqlContext.read().option("multiLine","true").option("mode", "PERMISSIVE").json(lines);

        DataFrame parsedjsoncol= parsedjson.withColumn("data",explode("entryLevel"))
                  .select("a", "b", "c", "d");
...