2016-07-30 3 views
1

다음 코드에 의해 생성되는 가능한 한 버그가 :스파크 DataFrame 초기화 2.0 버그에

_struct = [ 
    types.StructField('string_field', types.StringType(), True), 
    types.StructField('long_field', types.LongType(), True), 
    types.StructField('double_field', types.DoubleType(), True) 
] 
_rdd = sc.parallelize([Row(string_field='1', long_field=1, double_field=1.1)]) 
_schema = types.StructType(_struct) 
_df = sqlContext.createDataFrame(_rdd, schema=_schema) 
_df.take(1) 

예상 출력이 생성되어야 1 로우와 RDD가있다.

그러나

나는 다음과 같은 오류가 발생 현재 행동 :

DoubleType can not accept object '1' in type <type 'str'> 

PS : 나는 스칼라 2.10

편집에에

감사를 컴파일 스파크 2.0을 사용하고 답변자의 제안을 제대로 이해할 수 있습니다. 단순화하기 위해 구조체가 정렬되어 있는지 확인하십시오. 다음 코드는 이에 대해 설명합니다 :

# This doesn't work: 
_struct = [ 
    SparkTypes.StructField('string_field', SparkTypes.StringType(), True), 
    SparkTypes.StructField('long_field', SparkTypes.LongType(), True), 
    SparkTypes.StructField('double_field', SparkTypes.DoubleType(), True) 
] 
_rdd = sc.parallelize([Row(string_field='1', long_field=1, double_field=1.1)]) 

# But this will work, since schema is sorted: 
_struct = sorted([ 
    SparkTypes.StructField('string_field', SparkTypes.StringType(), True), 
    SparkTypes.StructField('long_field', SparkTypes.LongType(), True), 
    SparkTypes.StructField('double_field', SparkTypes.DoubleType(), True) 
], key=lambda x: x.name) 
params = {'string_field':'1', 'long_field':1, 'double_field':1.1} 
_rdd = sc.parallelize([Row(**params)]) 


_schema = SparkTypes.StructType(_struct) 

_df = sqlContext.createDataFrame(_rdd, schema=_schema) 
_df.take(1) 

_schema = SparkTypes.StructType(_struct) 

_df = sqlContext.createDataFrame(_rdd, schema=_schema) 
_df.take(1) 
+2

스칼라 2.10을 의미합니까? – eliasah

답변

4

이것은 1.x와 2.x 사이의 동작이 변경된 것처럼 보이지만 버그 인 것 같습니다. 특히 개체를 만들 때 kwargs (인수로 명명 됨) the fields are sorted by names과 함께합니다. 간단한 예제와 함께 그것을 설명하자

Row(string_field='1', long_field=1, double_field=1.1) 
## Row(double_field=1.1, long_field=1, string_field='1' 

필드가 변경되어 더 이상 스키마에 반영됩니다 당신이 순서를 볼 수 있듯이.

2.0.0보다 앞선 스파크는 only if data argument for createDataFrame is a local data structure 유형을 확인합니다. 그래서 코드를 다음

sqlContext.createDataFrame(
    data=[Row(string_field='1', long_field=1, double_field=1.1)], 
    schema=_schema 
) 

뿐만 아니라

스파크 2.0.0 도입 verification for RDDs을 1.6에 실패하고 로컬 및 분산 입력 사이의 일관된 동작을 제공한다.