2016-06-19 1 views
1

다음 코드를 실행하여 텍스트 파일에서 데이터 프레임을 만듭니다.데이터 프레임 생성 중 "scala.MatchError : 1201 (java.lang.Integer 클래스)"처리 중

import org.apache.spark.SparkContext 
import org.apache.spark.SparkConf 
import org.apache.spark.sql.{SQLContext, Row} 
import org.apache.spark.sql.types.{StructType, StringType, StructField} 


/** 
    * Created by PSwain on 6/19/2016. 
    */ 
object RddToDataframe extends App { 

    val scnf=new SparkConf().setAppName("RddToDataFrame").setMaster("local[1]") 
    val sc = new SparkContext(scnf) 
    val sqlContext = new SQLContext(sc) 

    val employeeRdd=sc.textFile("C:\\Users\\pswain\\IdeaProjects\\test1\\src\\main\\resources\\employee") 

    //Creating schema 

    val employeeSchemaString="id name age" 
    val schema = StructType(employeeSchemaString.split(",").map(colNmae => StructField(colNmae,StringType,true))) 

    //Creating RowRdd 
    val rowRdd= employeeRdd.map(row => row.split(",")).map(row => Row(row(0).trim.toInt,row(1),row(2).trim.toInt)) 

    //Creating dataframe = RDD[rowRdd] + schema 
    val employeeDF=sqlContext.createDataFrame(rowRdd,schema). registerTempTable("Employee") 

    sqlContext.sql("select * from Employee").show() 


} 

그러나 InteliJ에서 실행되는 동안 아래와 같은 형식 불일치 오류가 발생합니다. 이 오류가 왜 발생하는지 파악할 수 없으며 문자열을 정수로 변환하는 중입니다. 직원 파일은 아래에 입력되어 있으며 한 줄에 모두 표시되지만 한 줄로 표시됩니다.

1201 사티 25 1202 크리, 1,203 28 amith 39 1204 자 베드, 1,205 23 prudvi,

16/06/19 15:18:58 ERROR Executor: Exception in task 0.0 in stage 0.0 (TID 0) 
scala.MatchError: 1201 (of class java.lang.Integer) 
    at org.apache.spark.sql.catalyst.CatalystTypeConverters$StringConverter$.toCatalystImpl(CatalystTypeConverters.scala:295) 
    at org.apache.spark.sql.catalyst.CatalystTypeConverters$StringConverter$.toCatalystImpl(CatalystTypeConverters.scala:294) 
    at org.apache.spark.sql.catalyst.CatalystTypeConverters$CatalystTypeConverter.toCatalyst(CatalystTypeConverters.scala:102) 
+0

입니다'employeeSchemaString.split (",")'와''문자열'공간' "ID 이름 나이"로 구분하면? –

답변

3
스키마는 모든 항목 유형으로 생성

23은 StringType으로 정의됩니다.

val schema = StructType(employeeSchemaString.split(",").map(colNmae => StructField(colNmae,StringType,true))) 

그러나 rowRDD에는 int, string 및 int 유형의 열이 있습니다. 여기

는 왜 분할있는 작업 코드

val structType= { 
    val id = StructField("id", IntegerType) 
    val name = StructField("name", StringType) 
    val age = StructField("age", IntegerType) 
    new StructType(Array(id, name , age)) 
} 

val rowRdd= employeeRdd.map(row => row.split(",")).map(row => Row(row(0).trim().toInt,row(1),row(2).trim().toInt)) 

sqlContext.createDataFrame(rowRdd,structType). registerTempTable("Employee") 

sqlContext.sql("select * from Employee").show()