当我尝试在 Spark 中通过 RDD 转换为 Dataframe 时,出现以下异常“无法推断类型的架构:”
示例:
>> rangeRDD.take(1).foreach(println)
(301,301,10)
>> sqlContext.inferSchema(rangeRDD)
Can not infer schema for type: <type 'unicode'>
有什么办法可以解决吗?我什至尝试自己在 sqlContext.createDataFrame(rdd, schema) 中注入(inject)模式
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”
最佳答案
首先尝试解析数据
>>> 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)
关于apache-spark - 无法推断类型 : <type 'unicode' > when converted RDD to DataFrame 的架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36586719/