2014-11-17 2 views
7

spark sql이 csv 데이터를 자동으로로드하는 방법을 제공합니까? 나는 다음과 같은 락스를 발견 https://issues.apache.org/jira/browse/SPARK-2360 있지만 다음과 같이Spark SQL : csv의 자동 스키마

은 현재 내가 csv 파일을로드 할 것입니다 .... 폐쇄되었다 : CSV 파일에서 자동 스키마 공제에

case class Record(id: String, val1: String, val2: String, ....) 

sc.textFile("Data.csv") 
.map(_.split(",")) 
.map { r =>     
    Record(r(0),r(1), .....) 
}.registerAsTable("table1") 

어떤 힌트를? 특히 a) 스키마를 나타내는 클래스를 생성하는 방법과 b) 자동으로 채울 수있는 방법 (예 : Record (r (0), r (1), .....))은 무엇입니까?

업데이트

: 남은 것은이 단계 지정된 속성의 수에 대한 동적 map(p => Row(p(0), p(1).trim))을 수행하는 방법이 될 것입니다 http://spark.apache.org/docs/1.1.0/sql-programming-guide.html#data-sources

// 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 peopleSchemaRDD = sqlContext.applySchema(rowRDD, schema) 

: 나는 여기에 스키마 생성에 대한 부분적인 해답을 찾았나요?

지원해 주셔서 감사합니다. Joerg

답변

5

당신은 spark-csv를 사용할 수 있습니다.

+0

원래 질문은'DataFrame' 대신에'case class'에 대해 질문하고 있다고 생각합니다 –

+1

@ user955091 downvoting에 대한 추론은 고려하지 않았습니다 : 1) 질문은 spark sql과 csv는 케이스 클래스에 관한 것보다 훨씬 낫습니다. 2) 질문 작성자가 대답을 수락했습니다. 3) 답안이 downvote 이전에 4 개의 upvotes를 누적했습니다. 그럼 당신 downvoting 조금 가혹한 생각하지 않아? – dimitrisli

4
val schemaString = "name age".split(" ") 
// Generate the schema based on the string of schema 
val schema = StructType(schemaString.map(fieldName => StructField(fieldName, StringType, true))) 
val lines = people.flatMap(x=> x.split("\n")) 
val rowRDD = lines.map(line=>{ 
    Row.fromSeq(line.split(" ")) 
}) 
val peopleSchemaRDD = sqlContext.applySchema(rowRDD, schema) 

이 링크는 도움이 될 수 있습니다. 당신이 열 이름을 정의하고 헤더를 자동으로 사용하지 않아도 몇 번의 키 입력을 저장할 수있는

http://devslogics.blogspot.in/2014/11/spark-sql-automatic-schema-from-csv.html