Spark에서 DataFrame으로 장난치다가

 requirement failed: Currently correlation calculation for columns with dataType string not supported.라는 에러를 만날 수 있다. 


이는 데이터 타입을 inferScheme을 통해 추론하는데 데이터 타입이 Long/Int로 변환되야 하는데 String 타입으로 변환된 컬럼 데이터를 corr라는 sql 함수로 계산하다가 에러가 발생한 것이다.


이럴 때는 명시적으로 StructType을 사용해 스키마를 지원하는 것이 좋다.



import org.apache.spark.sql.types.{StructType, StructField, StringType, LongType}

val myManualSchema = StructType(Array(
StructField("InvoiceNo", LongType, true),
StructField("StockCode", StringType, true),
StructField("Description", StringType, true),
StructField("Quantity", LongType, true),
StructField("InvoiceDate", StringType, true),
StructField("UnitPrice", LongType, true),
StructField("CustomerID", StringType, true),
StructField("Country", StringType, true)
))

val df = spark.read.format("csv")
.option("header", true)
.schema(myManualSchema)
.load("origin-source/data/retail-data/by-day/2010-12-01.csv")


Posted by '김용환'
,