繁体   English   中英

如何使用Scala从XML文件传入字符串作为Spark中dataFrames的连接条件?

[英]How can I pass in a string from XML file as a join condition for dataFrames in Spark Using Scala?

我想在某些列(称为键)上加入数据框,并从XML文件中读取这些列的名称。

private def joinTables(xmlTable : Node) {
        var key:String = (xmlTable \ "@key").text;
        println(key)
        val df1= //logic;
        val df2 = //logic;
        val join_df = df1.join(df2, Seq(key), "outer")
}

这是println(key)的输出

"col1", "col2"

我希望在输入密钥后,这两个等价物看起来像这样

val join_df = df1.join(df2, Seq("col1", "col2"), "outer") //If I hardcode and type it like this, the code runs fine
val join_df = df1.join(df2, Seq(key), "outer")

但是,运行后,出现以下错误

org.apache.spark.sql.AnalysisException: USING column `"col1", "col2"` cannot be resolved on the left side of the join. The left-side columns: [col1, col2, col3, col4];

密钥的XML看起来像这样

 <table name="table1" key="&quot;col1&quot;, &quot;col2&quot;, &quot;col3&quot;, &quot;col4&quot;">
</table>

即使我修改了上面的xml,我也遇到了同样的错误,因此键只有一列

<table name="table1" key="&quot;col1&quot;">
    </table>

错误信息:

org.apache.spark.sql.AnalysisException:  USING column `"col1"` cannot be resolved on the left side of the join. The left-side columns: [col1, col2, col3, col4];

总结:如何从XML读取字符串,并使用Scala将其用于联接两个数据框? 我的方法不正确吗?

从XML文件获取并存储在key是一个字符串。

var key:String = (xmlTable \ "@key").text;

那不是一个字符串列表,而是一个字符串。 要创建一系列列,您需要使用split和各种strip功能。

val cols = key.split(", ").map(_.stripPrefix("\"").stripSuffix("\""))

以下先前的问题涵盖了这些内容。

在Scala中整理字符串

如何在Scala中将字符串除以字符串

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM