【发布时间】:2017-08-28 10:52:17
【问题描述】:
我已经定义了两个这样的表:
val tableName = "table1"
val tableName2 = "table2"
val format = new SimpleDateFormat("yyyy-MM-dd")
val data = List(
List("mike", 26, true),
List("susan", 26, false),
List("john", 33, true)
)
val data2 = List(
List("mike", "grade1", 45, "baseball", new java.sql.Date(format.parse("1957-12-10").getTime)),
List("john", "grade2", 33, "soccer", new java.sql.Date(format.parse("1978-06-07").getTime)),
List("john", "grade2", 32, "golf", new java.sql.Date(format.parse("1978-06-07").getTime)),
List("mike", "grade2", 26, "basketball", new java.sql.Date(format.parse("1978-06-07").getTime)),
List("lena", "grade2", 23, "baseball", new java.sql.Date(format.parse("1978-06-07").getTime))
)
val rdd = sparkContext.parallelize(data).map(Row.fromSeq(_))
val rdd2 = sparkContext.parallelize(data2).map(Row.fromSeq(_))
val schema = StructType(Array(
StructField("name", StringType, true),
StructField("age", IntegerType, true),
StructField("isBoy", BooleanType, false)
))
val schema2 = StructType(Array(
StructField("name", StringType, true),
StructField("grade", StringType, true),
StructField("howold", IntegerType, true),
StructField("hobby", StringType, true),
StructField("birthday", DateType, false)
))
val df = sqlContext.createDataFrame(rdd, schema)
val df2 = sqlContext.createDataFrame(rdd2, schema2)
df.createOrReplaceTempView(tableName)
df2.createOrReplaceTempView(tableName2)
我正在尝试构建查询以从 table1 返回 table2 中没有匹配行的行。 我试过用这个查询来做:
Select * from table1 LEFT JOIN table2 ON table1.name = table2.name AND table1.age = table2.howold AND table2.name IS NULL AND table2.howold IS NULL
但这只是给了我 table1 中的所有行:
列表({"name":"john","age":33,"isBoy":true}, {"name":"susan","age":26,"isBoy":false}, {"name":"mike","age":26,"isBoy":true})
如何在 Spark 中高效地进行这种类型的连接?
我正在寻找一个 SQL 查询,因为我需要能够指定要在两个表之间进行比较的列,而不是像在其他推荐问题中那样逐行比较。就像使用减法一样,除了等。
【问题讨论】:
-
根据您对我的答案的编辑和评论,我认为您正在寻找:stackoverflow.com/questions/29537564/… 特别是@Interfector 对第一个答案的评论
-
RDD#cogroup 应该可以工作
-
请尝试以下查询。 "Select * from table1 LEFT JOIN table2 ON table1.name = table2.name AND table1.age = table2.howold WHERE table2.name IS NULL AND table2.howold IS NULL"
标签: scala apache-spark