无法推断类型的架构:<type'unicode'=“”>将RDD转换为DataFrame

时间:2016-04-13 01:00:42

标签: apache-spark dataframe pyspark rdd spark-dataframe

当我尝试通过RDD转换为Spark中的Dataframe时,我得到以下异常“无法推断类型的架构:”

示例:

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

任何指针如何修复它?我甚至尝试在sqlContext.createDataFrame(rdd,schema)

中自己注入模式
schema = StructType([
StructField("x", IntegerType(), True),
StructField("y", IntegerType(), True),
StructField("z", IntegerType(), True)]) 
df = sqlContext.createDataFrame(rangeRDD, schema)
print df.first()

但最后出现在运行时错误'ValueError:Unexpected tuple u'(301,301,10)'with StructType'

1 个答案:

答案 0 :(得分: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)