scala - Spark Scala 函数调用
问题描述
要求如下:
val value = Array["id","sd","cd"] -- List of columns
val cols_list = Array["cd","id","tm","no","in","ts","nm"] - -- List of columns
abcd 是模式名称。需要有值的列和 cols_list 中没有值的列。
val alter = df.select(value + ("abcd." + x.toUpperCase() for x <- cols_list if x.toUpperCase() not in value)).where(df.status =="ALERT")
错误是它无法读取 x。要求是具有选择条件的数据框和不具有操作的 for 循环。请有任何想法/建议。
我试过如下 val diff_cols = value diff cols_list --- 看起来这不是个好主意。
val alter = df.select(value + ("abcd." + diff_cols).where(df.status
=="ALERT")
但我现在看到的问题不是列 [Ljava.lang.String;@6cc9bbea 正在传递,但它失败了。
请建议是否有人有任何其他解决方案?
解决方案
请检查以下代码。
在 spark 中,您可以访问没有模式名称的列。
scala> val value = Array("id","sd","cd")
value: Array[String] = Array(id, sd, cd)
scala> val cols_list = Array("cd","id","tm","no","in","ts","nm")
cols_list: Array[String] = Array(cd, id, tm, no, in, ts, nm)
scala> val columns = value ++ cols_list.diff(value)
columns: Array[String] = Array(id, sd, cd, tm, no, in, ts, nm)
scala> val schema = "abcd"
schema: String = abcd
scala> columns.map(column => s"${schema}.${column}") // This step is not required, in spark you can access columns without schema name. if you still want you can use like this.
res14: Array[String] = Array(abcd.id, abcd.sd, abcd.cd, abcd.tm, abcd.no, abcd.in, abcd.ts, abcd.nm)
scala> df.select(columns.head,columns.tail:_*).where($"status" === "ALERT")
推荐阅读
- botframework - 如何从单独的瀑布对话框中取消所有对话框
- html - CSS:它的背景色来自哪里的边距和填充?
- batch-file - 批处理:'FOR' cmd 部分工作,'skip=' 选项不起作用
- etl - 根据 Pentaho Kettle 中的关键字过滤数据的 ETL 作业
- python - 无法使用 open() 在 Django python 中打开 xml 文件
- ios - UIAccessibilityCustomAction 选择器没有被触发
- python - 如何将标签放在板上?
- python - python - 仅将选择列附加为行
- javascript - 导入子模块
- postgresql - 在完成命令 postgresql 之前关闭客户端