[英]Spark join dataframe based on column of different type spark 1.6
I have 2 dataframes df1 and df2.我有 2 个数据框 df1 和 df2。 I am joining both df1 and df2 based on columns col1
and col2
.我根据列col1
和col2
加入 df1 和 df2 。 However the datatype of col1 is string
in df1
and type of col2 is int
in df2
.但是 col1 的数据类型是df1
中的string
,而 col2 的类型是df2
中的int
。 When I try to join like below,当我尝试像下面这样加入时,
val df3 = df1.join(df2,df1("col1") === df2("col2"),inner).select(df2("col2"))
The join does not work and return empty datatype.连接不起作用并返回空数据类型。 Will it be possible to get proper output without changing type of col2
in df2
是否可以在不更改df2
中col2
类型的情况下获得正确的 output
val dDF1 = List("1", "2", "3").toDF("col1")
val dDF2 = List(1, 2).toDF("col2")
val res1DF = dDF1.join(dDF2, dDF1.col("col1") === dDF2.col("col2").cast("string"), "inner")
.select(dDF2.col("col2"))
res1DF.printSchema()
res1DF.show(false)
// root
// |-- col2: integer (nullable = false)
//
// +----+
// |col2|
// +----+
// |1 |
// |2 |
// +----+
get schema DataFrame获取架构 DataFrame
val sch1 = dDF1.schema
sch1: org.apache.spark.sql.types.StructType = StructType(StructField(col1,StringType,true))
// public StructField(String name,
// DataType dataType,
// boolean nullable,
// Metadata metadata)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.