【问题标题】:spark scala read config filespark scala 读取配置文件
【发布时间】:2017-09-13 16:31:20
【问题描述】:

我的 hdfs 中有一个配置文件,其中包含要从 json 文件中选择的列名

json 架构

  { 
    a:string
    b:int
    c struct
     {
       id: string
       count : integer
     }
  }


 Config.txt 
 "a","b","c.id"

在代码中

 val df = sqlcontext.read.json("jsonfile"
 val config_file = "Config.txt"
 val config = scala.io.Source.fromFile(config_file).mkString
 val df_contents=df.selectExpr(config)
 df_contents.printSchema()
 df_cust_att_contents.show()

我在 df.selectExpr(config) 中遇到错误。它不被认可。它的抛出错误。如何将配置文件中的列名传递给 selectExpr。请帮忙

【问题讨论】:

    标签: scala apache-spark


    【解决方案1】:

    selectExpr 每列需要一个参数。您正试图将所有列放在一个参数中。

    试试:

    df.selectExpr(config.replaceAll("\"", "").split(","): _*)
    

    【讨论】:

    • @gayathri 所以请接受它并放弃投票:)
    • @gayathri 不是这个,它的分数为零;)
    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2021-12-07
    • 2021-03-14
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多