1

I hava a RDD and a Array[String],I want to convert RDD to DataFrame,the Array[String]'s value are colnames,but DataFrame.toDf() function need a String* type

this is toDF()'s source code:

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

this is my code:

 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("??","??")

this is my expect result:

+-----+------+
|first|second|
+-----+------+
|    1|     2|
|    3|     2|
|    4|     2|
|    5|     2|
|    7|     2|
+-----+------+
mentongwu
  • 463
  • 2
  • 7
  • 21
  • Possible duplicate of [Pass List\[String\] to function that takes f(args: String\*) scala](https://stackoverflow.com/questions/38258023/pass-liststring-to-function-that-takes-fargs-string-scala) – eliasah Jul 10 '17 at 06:18

1 Answers1

3

Use _* for varargs in Scala, so you can do it by: sc.parallelize(arr,2).toDF(colNames:_*)

chengpohi
  • 14,064
  • 1
  • 24
  • 42