将来自不同数据集的两列合并到Spark中的一个表中

时间:2019-01-28 18:55:35

标签: scala apache-spark

我想从两个不同的表中获取两列,并将它们合并到一个表中,但不要使用两者之间通用的任何主键。例如:

    val testDSArray : java.util.List[Integer] = new util.ArrayList[Integer]()
    testDSArray.add(4)
    testDSArray.add(7)
    testDSArray.add(10)

    val testDS: DataFrame = spark.createDataset(testDSArray)(Encoders.INT).toDF("col1")
    val testDS2: DataFrame = spark.createDataset(testDSArray)(Encoders.INT).toDF("col2")

    val columns = testDS.withColumn("col2", testDS2.col("col2"))
    columns.show(5)

我希望这段代码显示如下内容:

---------------
| col1 | col2 |
---------------
|   4  |   4  |
|   7  |   7  |
|  10  |  10  |
---------------

但是,上面的代码无法运行并出现错误

Exception in thread "main" org.apache.spark.sql.AnalysisException: resolved attribute(s) col2#12 missing from col1#6 in operator !Project [col1#6, col2#12 AS col2#15];

0 个答案:

没有答案
相关问题