2016-04-13 22 views
2

当我试图通过RDD转换的火花,数据帧和我得到以下异常“无法推断架构类型:”无法推断架构类型:<类型“的unicode”>转换时RDD到数据帧

例如:

>> rangeRDD.take(1).foreach(println) 
(301,301,10) 
>> sqlContext.inferSchema(rangeRDD) 
Can not infer schema for type: <type 'unicode'> 

任何指针如何解决它?我甚至尝试注入架构自己在sqlContext.createDataFrame(RDD,架构)

schema = StructType([ 
StructField("x", IntegerType(), True), 
StructField("y", IntegerType(), True), 
StructField("z", IntegerType(), True)]) 
df = sqlContext.createDataFrame(rangeRDD, schema) 
print df.first() 

但在运行时错误结束了 'ValueError异常:意外的元组U'(301,301,10) '与StructType'

回答

2

尝试解析数据第一

>>> rangeRDD = sc.parallelize([ u'(301,301,10)']) 
>>> tupleRangeRDD = rangeRDD.map(lambda x: x[1:-1]) \ 
...      .map(lambda x: x.split(",")) \ 
...      .map(lambda x: [int(y) for y in x]) 
>>> df = sqlContext.createDataFrame(tupleRangeRDD, schema) 
>>> df.first() 
Row(x=301, y=301, z=10) 
相关问题