代码之家  ›  专栏  ›  技术社区  ›  mentongwu

如何使用spark在scala中将数组[字符串]转换为字符串*

  •  0
  • mentongwu  · 技术社区  · 9 年前

    我有一个 RDD 和a Array[String] RDD公司 DataFrame Array[String]' s值是colnames,但是 DataFrame.toDf() 功能需要 String*

    这是toDF()的源代码:

        def toDF(colNames: String*): DataFrame = ds.toDF(colNames : _*)
    

    这是我的代码:

     val sqlContext = new SQLContext(sc)
        import sqlContext.implicits._
        val arr=Array(
          (1,2),
          (3,2),
          (4,2),
          (5,2),
          (7,2)
    
        )
        val colNames=Array("first","second")
        val df = sc.parallelize(arr,2).toDF("??","??")
    

    这是我的预期结果:

    +-----+------+
    |first|second|
    +-----+------+
    |    1|     2|
    |    3|     2|
    |    4|     2|
    |    5|     2|
    |    7|     2|
    +-----+------+
    
    1 回复  |  直到 9 年前
        1
  •  3
  •   chengpohi    9 年前

    使用 _* 对于 在里面 ,因此您可以通过以下方式实现: sc.parallelize(arr,2).toDF(colNames:_*)