在spark sql中转换两个数据框



我有两个数据框架在spark scala注册为表。从这两个表

表1:

   +-----+--------+
   |id   |values  |
   +-----+-----   +
   |   0 |  v1    |
   |   0 |  v2    |
   |   1 |  v3    |
   |   1 |  v1    |
   +-----+-----   +
表2:

   +-----+----+--- +----+
   |id   |v1  |v2  | v3
   +-----+-------- +----+
   |   0 |  a1|  b1| -  |
   |   1 |  a2|  - | c2 |
   +-----+---------+----+   

我想用上面的两个表生成一个新表。

表3:

   +-----+--------+--------+
   |id   |values  | field  |
   +-----+--------+--------+
   |   0 |  v1    | a1     |
   |   0 |  v2    | b1     |
   |   1 |  v3    | c2     |
   |   1 |  v1    | a2     |
   +-----+--------+--------+

这里v1的形式是

 v1: struct (nullable = true)
    |    |-- level1: string (nullable = true)
    |    |-- level2: string (nullable = true)
    |    |-- level3: string (nullable = true)
    |    |-- level4: string (nullable = true)
    |    |-- level5: string (nullable = true)

我在scala中使用spark sql。

是否可以通过编写一些sql查询或在数据框架上使用一些spark函数来完成所需的事情

下面是您可以使用的示例代码,它将生成以下输出:

代码如下:

val df1=sc.parallelize(Seq((0,"v1"),(0,"v2"),(1,"v3"),(1,"v1"))).toDF("id","values")
val df2=sc.parallelize(Seq((0,"a1","b1","-"),(1,"a2","-","b2"))).toDF("id","v1","v2","v3")
val joinedDF=df1.join(df2,"id")
val resultDF=joinedDF.rdd.map{row=>
val id=row.getAs[Int]("id")
val values=row.getAs[String]("values")
val feilds=row.getAs[String](values)
(id,values,feilds)
}.toDF("id","values","feilds")

在控制台测试时:

scala> val df1=sc.parallelize(Seq((0,"v1"),(0,"v2"),(1,"v3"),(1,"v1"))).toDF("id","values")
df1: org.apache.spark.sql.DataFrame = [id: int, values: string]
scala> df1.show
+---+------+
| id|values|
+---+------+
|  0|    v1|
|  0|    v2|
|  1|    v3|
|  1|    v1|
+---+------+

scala> val df2=sc.parallelize(Seq((0,"a1","b1","-"),(1,"a2","-","b2"))).toDF("id","v1","v2","v3")
df2: org.apache.spark.sql.DataFrame = [id: int, v1: string ... 2 more fields]
scala> df2.show
+---+---+---+---+
| id| v1| v2| v3|
+---+---+---+---+
|  0| a1| b1|  -|
|  1| a2|  -| b2|
+---+---+---+---+

scala> val joinedDF=df1.join(df2,"id")
joinedDF: org.apache.spark.sql.DataFrame = [id: int, values: string ... 3 more fields]
scala> joinedDF.show
+---+------+---+---+---+                                                        
| id|values| v1| v2| v3|
+---+------+---+---+---+
|  1|    v3| a2|  -| b2|
|  1|    v1| a2|  -| b2|
|  0|    v1| a1| b1|  -|
|  0|    v2| a1| b1|  -|
+---+------+---+---+---+

scala> val resultDF=joinedDF.rdd.map{row=>
     | val id=row.getAs[Int]("id")
     | val values=row.getAs[String]("values")
     | val feilds=row.getAs[String](values)
     | (id,values,feilds)
     | }.toDF("id","values","feilds")
resultDF: org.apache.spark.sql.DataFrame = [id: int, values: string ... 1 more field]
scala> 
scala> resultDF.show
+---+------+------+                                                             
| id|values|feilds|
+---+------+------+
|  1|    v3|    b2|
|  1|    v1|    a2|
|  0|    v1|    a1|
|  0|    v2|    b1|
+---+------+------+
我希望这可能是你的问题。谢谢!

相关内容

  • 没有找到相关文章