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

Posted Sinsa_SI

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决方案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

以上是关于解决方案ValueError: Some of types cannot be determined by the first 100 rows的主要内容,如果未能解决你的问题,请参考以下文章

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

遇见错误:ValueError: Classification metrics can‘t handle a mix of binary and continuous targets(代码

ValueError: Classification metrics can‘t handle a mix of continuous-multioutput and multiclass targe

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery

Some api interface link of Soapui