0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Spark SQL - explode の使い方

Last updated at Posted at 2021-07-01

explode は配列のカラムに対して適用すると各要素をそれぞれ行に展開してくれます。

// 配列のカラムを持つ DataFrame 作成
scala> val df = Seq(Array(1,2,3), Array(4,6,7), Array(9,2,5,6)).toDF
df: org.apache.spark.sql.DataFrame = [value: array<int>]

// 中身チラ見
scala> df.show
+------------+
|       value|
+------------+
|   [1, 2, 3]|
|   [4, 6, 7]|
|[9, 2, 5, 6]|
+------------+

// explode やってみる
scala> val unnestDF = df.select(explode(df("value")))
unnestDF: org.apache.spark.sql.DataFrame = [col: int]

// 変更後のスキーマ定義
scala> unnestDF.printSchema
root
 |-- col: integer (nullable = false)


// 中身チラ見
scala> unnestDF.show
+---+
|col|
+---+
|  1|
|  2|
|  3|
|  4|
|  6|
|  7|
|  9|
|  2|
|  5|
|  6|
+---+

おわり。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?