Dataframe에 새로운 칼럼 추가하기
withColumn 메서드를 사용한다.
// def withColumn(colName: String,col: org.apache.spark.sql.Column): org.apache.spark.sql.DataFrame
val df = List((1, 2),(4, 8)).toDF("col1", "col2")
+----+----+
|col1|col2|
+----+----+
| 1| 2|
| 4| 8|
+----+----+
// 1. 특정 값으로 채워진 칼럼 추가
// literal 함수를 사용한다. literal은 프로그래밍 언어의 리터럴 값을 스파크가 이해할 수 있는 값으로 변화한다.
// org.apche.spark.sql.functions.lit
df.withColumn("new", lit(null))
+----+----+----+
|col1|col2| new|
+----+----+----+
| 1| 2|null|
| 4| 8|null|
+----+----+----+
// 2. 다른 칼럼들을 연산하여 값 채우기
// def withColumn(colnName: String, col: org.apache.spark.sql.Column)
df.withColumn("sum", $"col1" + $"col2")
+----+----+---+
|col1|col2|sum|
+----+----+---+
| 1| 2| 3|
| 4| 8| 12|
+----+----+---+
// 3. 인덱스 넣기(0부터, 1부터)
TBD