2016-12-26 3 views
1

나는 Spark 1.5를 사용한다.도트로 열의 이름을 바꾸는 방법은 무엇입니까?

이름에 점들이 포함 된 열 (예 : param.x.y)에 어려움을 겪고 있습니다. 나는 처음에 그것들을 선택하는 문제를 가지고 있었지만`character (`param.x.y`)를 사용해야한다는 것을 알았다.

이제 열의 이름을 바꾸려고 할 때 문제가 있습니다. 나는 유사한 접근 방식을 사용하고 있지만, 작동하지 않는 것 같습니다 :

df.withColumnRenamed("`param.x.y`", "param_x_y") 

그래서 내가 확인하고 싶어서 -이 정말 버그, 아니면 내가 뭔가 잘못하고있는 중이 야?

답변

0

코드에서와 같이 보이는데 원래의 열 이름에 `` '이 (가) 있습니다. 나는 그것을 제거하고 그것은 나를 위해 일했다. 데이터 프레임 내의 열 이름을 바꿀 수있는 샘플 코드.

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! 
+0

감사합니다. 그래서 정말로 버그가있어서 수동으로해야합니까? – Marko

+0

여기에 점이 있으면 문제가 없습니다. 문제는 따옴표로 생각합니다. 그 이상을 확인해야합니다 –

+0

내 최신 테스트에서 어떤 버그가 여기에 보이지 않습니다. 구현에 문제가있을 수 있습니다. 설명을 위해 업데이트 된 답변을 참조하십시오. –