Изменить структуру Spark DataFrame - PullRequest
0 голосов
/ 25 апреля 2019

Допустим, у меня есть Spark DataFrame со следующими столбцами:

| header1 | location | precision | header2 | velocity | data |

(Этот файл также содержит некоторые данные)

Теперь я хотел бы преобразовать df в новую структуру с 2 столбцами, каждый из которых имеет сложные поля - что-то вроде этого:

|          gps                   |         velocity          |
| header1 | location | precision | header2 | velocity | data |

Лучше всего, если бы я мог просто вызвать метод:

df1 = createStructure(df, "gps", ["header1", "gps", "precision"])
df2 = createStructure(df1, "velocity", ["header2", "velocity", "data"])

Я экспериментировал с "withColumn", но мне не повезло

1 Ответ

1 голос
/ 25 апреля 2019

Попробуйте это.

scala> import org.apache.spark.sql.functions._
import org.apache.spark.sql.functions._

scala> val df1 = Seq(("h1-4", "loc4", "prec4", "h2-4", "vel4", "d4"), ("h1-5", "loc5", "prec5", "h2-5", "vel5", "d5")).toDF("header1", "location", "precision", "header2", "velocity", "data")
df1: org.apache.spark.sql.DataFrame = [header1: string, location: string ... 4 more fields]

scala> df1.show(false)
+-------+--------+---------+-------+--------+----+
|header1|location|precision|header2|velocity|data|
+-------+--------+---------+-------+--------+----+
|h1-4   |loc4    |prec4    |h2-4   |vel4    |d4  |
|h1-5   |loc5    |prec5    |h2-5   |vel5    |d5  |
+-------+--------+---------+-------+--------+----+


scala> val outputDF = df1.withColumn("gps", struct($"header1", $"location", $"precision")).withColumn("velocity", struct($"header2", $"velocity", $"data")).select("gps", "velocity")
outputDF: org.apache.spark.sql.DataFrame = [gps: struct<header1: string, location: string ... 1 more field>, velocity: struct<header2: string, velocity: string ... 1 more field>]

scala> outputDF.printSchema
root
|-- gps: struct (nullable = false)
|    |-- header1: string (nullable = true)
|    |-- location: string (nullable = true)
|    |-- precision: string (nullable = true)
|-- velocity: struct (nullable = false)
|    |-- header2: string (nullable = true)
|    |-- velocity: string (nullable = true)
|    |-- data: string (nullable = true)


scala> outputDF.show(false)
+-------------------+----------------+
|gps                |velocity        |
+-------------------+----------------+
|[h1-4, loc4, prec4]|[h2-4, vel4, d4]|
|[h1-5, loc5, prec5]|[h2-5, vel5, d5]|
+-------------------+----------------+
...