코드에서와 같이 보이는데 원래의 열 이름에 `` '이 (가) 있습니다. 나는 그것을 제거하고 그것은 나를 위해 일했다. 데이터 프레임 내의 열 이름을 바꿀 수있는 샘플 코드.
import org.apache.spark._
import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql._
import org.apache.spark._
import org.apache.spark.sql.DataFrame
import org.apache.spark.rdd.RDD
// Import Row.
import org.apache.spark.sql.Row;
// Import Spark SQL data types
import org.apache.spark.sql.types.{ StructType, StructField, StringType };
object RenameColumn extends Serializable {
val conf = new SparkConf().setAppName("read local file")
conf.set("spark.executor.memory", "100M")
conf.setMaster("local");
val sc = new SparkContext(conf)
// sc is an existing SparkContext.
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
def main(args: Array[String]): Unit = {
// Create an RDD
val people = sc.textFile("C:/Users/User1/Documents/test");
// The schema is encoded in a string
val schemaString = "name age"
// Generate the schema based on the string of schema
val schema =
StructType(
schemaString.split(" ").map(fieldName => StructField(fieldName, StringType, true)))
// Convert records of the RDD (people) to Rows.
val rowRDD = people.map(_.split(",")).map(p => Row(p(0), p(1).trim))
// Apply the schema to the RDD.
val peopleDataFrame = sqlContext.createDataFrame(rowRDD, schema)
peopleDataFrame.printSchema()
val renamedSchema = peopleDataFrame.withColumnRenamed("name", "name_renamed");
renamedSchema.printSchema();
sc.stop
}
}
출력 :
16/12/26 16:53:48 INFO SparkContext: Created broadcast 0 from textFile at RenameColumn.scala:28
root
root
|-- name.rename: string (nullable = true)
|-- age: string (nullable = true)
root
|-- name_renamed: string (nullable = true)
|-- age: string (nullable = true)
16/12/26 16:53:49 INFO SparkUI: Stopped Spark web UI at http://XXX.XXX.XXX.XXX:<port_number>
16/12/26 16:53:49 INFO MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
자세한 내용은 spark dataframe documentation
업데이트를 확인할 수 있습니다 난 그냥 인용 된 문자열 테스트와 예상되는 결과를 얻었다. 아래 코드와 그 출력을보십시오.
val schemaString = "`name.rename` age"
// Generate the schema based on the string of schema
val schema =
StructType(
schemaString.split(" ").map(fieldName => StructField(fieldName, StringType, true)))
// Convert records of the RDD (people) to Rows.
val rowRDD = people.map(_.split(",")).map(p => Row(p(0), p(1).trim))
// Apply the schema to the RDD.
val peopleDataFrame = sqlContext.createDataFrame(rowRDD, schema)
peopleDataFrame.printSchema()
val renamedSchema = peopleDataFrame.withColumnRenamed("`name.rename`", "name_renamed");
renamedSchema.printSchema();
sc.stop
출력 : 대답에 대한
16/12/26 20:24:24 INFO SparkContext: Created broadcast 0 from textFile at RenameColumn.scala:28
root
|-- `name.rename`: string (nullable = true)
|-- age: string (nullable = true)
root
|-- name_renamed: string (nullable = true)
|-- age: string (nullable = true)
16/12/26 20:24:25 INFO SparkUI: Stopped Spark web UI at http://xxx.xxx.xxx.x:<port_number>
16/12/26 20:24:25 INFO MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
감사합니다. 그래서 정말로 버그가있어서 수동으로해야합니까? – Marko
여기에 점이 있으면 문제가 없습니다. 문제는 따옴표로 생각합니다. 그 이상을 확인해야합니다 –
내 최신 테스트에서 어떤 버그가 여기에 보이지 않습니다. 구현에 문제가있을 수 있습니다. 설명을 위해 업데이트 된 답변을 참조하십시오. –