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|
+-----+------+
1

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.