【问题标题】:Can not infer schema for type: <type 'unicode'> when converted RDD to DataFrame将 RDD 转换为 DataFrame 时无法推断类型的架构:<type 'unicode'>
【发布时间】:2016-08-03 20:37:55
【问题描述】:

当我尝试在 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) 中注入模式

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'

【问题讨论】:

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


    【解决方案1】:

    先尝试解析数据

    >>> 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)
    

    【讨论】:

      猜你喜欢
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2020-01-24
      • 1970-01-01
      相关资源
      最近更新 更多