如何在单个coumn的spark scala中创建Dataframe

时间:2017-05-18 16:14:51

标签: spark-dataframe

我是新来的火花scala。 我有数据框,其中包含10列,但我想为该数据框添加一列,该列是日期格式日期将由随机数生成。

import java.util.Date
import java.util.ArrayList
import java.text.SimpleDateFormat

object Datecolumn {
  def main(args: Array[String]) {



  val dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
val date = new Date();
//println(dateFormat.format(date));

  val li= new ArrayList[String]
  for(i<- 1 to 10)
  {
    li.add(dateFormat.format(date))
  }


 // val dateColumn =

 val Lii = li.listIterator()
 while(Lii.hasNext())
 {
   println(Lii.next())
 }

  li.toDF("Date") //.toDF is not a member of Java.util.ArrayList

}
}

1 个答案:

答案 0 :(得分:0)

 //Initialise Spark Session
  val spark = SparkSession
    .builder()
    .master("local")
    .appName("ParquetAppendMode")
    .getOrCreate()

  import spark.implicits._

//create a simple dataframe with one column
  val dataFrame = spark.sparkContext.parallelize(1 to 10).toDF("number")

  dataFrame.show

//add another column with current timestamp 
  dataFrame.withColumn("timestamp", unix_timestamp()).show()

希望如果我了解你,这会有所帮助!