다음 코드에 의해 생성되는 가능한 한 버그가 :스파크 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.10을 의미합니까? – eliasah