Создание тестового примера для scala для массива столбцов - PullRequest
0 голосов
/ 06 мая 2020
 |-- column1 integer (nullable = true)
 |-- column2: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- column21: string (nullable = true)
 |    |    |-- column22: string (nullable = true)
 |    |    |-- column23: integer (nullable = true)

Я хочу создать последовательность строк для приведенной выше схемы для создания тестового примера и хочу предложить предложения для того же. Я пытался вот так

test("example") {
val input = createDataFrame(sparkSession, SNAPSHOT, SCHEMA)
}
val SNAPSHOT = Seq(
    Row("cust1",  List(Row(["2015-12-01 20:59:12", "rr"]),Row(["2015-12-01 20:59:12", "AAP"]))),
    Row("cust1",  List(Row(["2015-12-01 20:59:12", "qq"]),Row(["2015-12-01 20:59:12", "AAP"]))),

  )

  val SCHEMA = StructType(
    StructField(column1, StringType) ::
    StructField(column2,ArrayType(
          StructType(
      StructField(column21, StringType) ::
      StructField(column22, StringType) ::
      StructField(column23, StringType) ::
      Nil)
        )
        )::
      Nil
  )
basicaly the above implementation is wrong.

1 Ответ

1 голос
/ 06 мая 2020
  import org.apache.spark.sql.types.{ArrayType, IntegerType, StringType, StructType}
  import spark.implicits._

  val df = Seq(
    (1, Seq(("a", "b", 1))),
    (2, Seq(("c", "d", 2)))
  ).toDF()

  val schema = new StructType()
    .add("column1", IntegerType)
    .add("column2", ArrayType(new StructType()
      .add("column2_1", StringType)
      .add("column2_2", StringType)
      .add("column2_3", IntegerType)
    )
  )


  val df2 = spark.createDataFrame(df.rdd, schema)
  df2.printSchema()
  df2.show()

вывод:

root
 |-- column1: integer (nullable = true)
 |-- column2: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- column2_1: string (nullable = true)
 |    |    |-- column2_2: string (nullable = true)
 |    |    |-- column2_3: integer (nullable = true)

+-------+-----------+
|column1|    column2|
+-------+-----------+
|      1|[[a, b, 1]]|
|      2|[[c, d, 2]]|
+-------+-----------+
...