【解决方案】ValueError: Some of types cannot be determined by the first 100 rows

问题

在 spark 中试图将 RDD 转换成 DataFrame 时,有时会提示 ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling,此时有 2 种解决方案:

解决方案

方案一:提高数据采样率(sampling ratio)
sqlContext.createDataFrame(rdd, samplingRatio=0.01)

或者

rdd.toDF(samplingRatio=0.01)

其中的 samplingRatio 参数就是数据采样率,如果不设该参数,则默认取前 100 个元素。上面代码中设置的 samplingRatio 是 0.01,意味着 spark 将会取 RDD 中前 1% 的元素作为样本去推断元素中各个字段的数据类型。可以先设置为 0.01 试试,如果不行,可以继续增加。

方案二:显式声明要创建的 DataFrame 的数据结构,即 schema
from pyspark.sql.types import *
schema = StructType([
    StructField("c1", StringType(), True),
    StructField("c2", IntegerType(), True)
])
df = sqlContext.createDataFrame(rdd, schema=schema)

或者

from pyspark.sql.types import *
schema = StructType([
    StructField("c1", StringType(), True),
    StructField("c2", IntegerType(), True)
])
df = rdd.toDF(schema=schema)

参考:

  1. https://blog.csdn.net/zhufenghao/article/details/80712480
  2. https://blog.csdn.net/loxeed/article/details/53434555
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值