问题
在 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)
参考:
- https://blog.csdn.net/zhufenghao/article/details/80712480
- https://blog.csdn.net/loxeed/article/details/53434555