Как создать данные для сложного типа структуры, типа массива структуры в Scala Python и HiveQL? - PullRequest
1 голос
/ 10 июня 2019

Как создать схему, DataFrame и загрузить 2-3 строки образцов данных, которые вписываются в эту схему.Ищите решение для Scala, Java и Python, а также решение на основе HiveQL.

https://www.cloudera.com/documentation/enterprise/5-7x/topics/impala_struct.html предоставило способы создания схемы файла паркета, но как лучше всего вставить данные, которые представляют собой Struct Array

   |-- f2: string (nullable = true)
   |-- f3: array (nullable = true)
   |    |-- element: struct (containsNull = true)
   |    |    |-- f3_1: string (nullable = true)
   |    |    |-- f3_2: string (nullable = true)
   |    |    |-- f3_3: string (nullable = true)

Scala Code

val schemaStruct = StructType(
    StructField("f1", StringType, true) ::
    StructField("f2", StringType, true) ::
    StructField("f3", ArrayType(StructType(
    StructField("f3_1", StringType, true) ::
    StructField("f3_2", StringType, true) ::
    StructField("f3_3", StringType, true) :: Nil), true)) ::Nil)

val smallRow = Row("f1","f2","f3", <Some Thing >)
val dfsmall = sparkContext.createDataFrame(sc.parallelize(smallRow::Nil,1), schemaStruct )

Hive QL

    CREATE TABLE struct_demo(
      id BIGINT,
      name STRING,
    -- A STRUCT as a top-level column. Demonstrates how the table ID column
    -- and the ID field within the STRUCT can coexist without a name conflict.
      employee_info STRUCT < employer: STRING, id: BIGINT, address: STRING >,

    -- A STRUCT as the element type of an ARRAY.
      places_lived ARRAY < STRUCT <street: STRING, city: STRING, country: STRING >>,

    -- A STRUCT as the value portion of the key-value pairs in a MAP.
      memorable_moments MAP < STRING, STRUCT < year: INT, place: STRING, details: STRING >>,

    -- A STRUCT where one of the fields is another STRUCT.
      current_address STRUCT < street_address: STRUCT <street_number: INT, street_name: STRING, street_type: STRING>, country: STRING, postal_code: STRING >
    )
    STORED AS PARQUET;

Как вставить .. Что-то вроде

insert into struct_demo 12, "myName",  [ ["Street_1","NY_CITY","USA"],["Street_3","Chichago","USA"] ]  

...