我正在使用以下函数来解析 url,但它会抛出错误,
val b = Seq(("http://spark.apache.org/path?query=1"),("https://people.apache.org/~pwendell/spark-nightly/spark-master-docs/latest/api/sql/#negative")).toDF("url_col")
.withColumn("host",parse_url($"url_col","HOST"))
.withColumn("query",parse_url($"url_col","QUERY"))
.show(false)
错误:
<console>:285: error: not found: value parse_url
.withColumn("host",parse_url($"url_col","HOST"))
^
<console>:286: error: not found: value parse_url
.withColumn("query",parse_url($"url_col","QUERY"))
^
请指导如何将 url 解析为不同的部分。
最佳答案
@Ramesh 的回答是正确的,但您可能还需要一些 hacky 方法来在没有 SQL 查询的情况下使用此函数:)
事实上,“callUDF”函数不仅调用 UDF,还调用任何可用函数。
所以你可以这样写:
import org.apache.spark.sql._
import org.apache.spark.sql.functions._
b.withColumn("host", callUDF("parse_url", $"url_col", lit("HOST"))).
withColumn("query", callUDF("parse_url", $"url_col", lit("QUERY"))).
show(false)
编辑:this Pull Request 之后合并后,您可以像普通函数一样使用 parse_url
。在这个问题之后进行的 PR :)
关于scala - 如何在 spark sql(Scala) 中解析 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52041342/