在Apache Spark中指定CSV的模式

时间:2018-05-15 11:59:02

标签: python apache-spark pyspark apache-spark-sql pyspark-sql

我在一个简单的案例中遇到错误:

我想阅读一堆CSV,它们都具有相同的格式,但没有标题。

所以,我正在尝试指定标题。

from pyspark.sql import SQLContext
from pyspark.sql.types import *
sqlContext = SQLContext(sc)

schema = StructType([        
    StructField("c0", StringType(), True),    
    StructField("c1", StringType(), True),
    StructField("c2", StringType(), True),
    StructField("c3", TimestampType, True),
    StructField("c4", TimestampType, True),
    StructField("c5", StringType(), True),
    StructField("c6", StringType(), True),
    StructField("c7", StringType(), True),
    StructField("c8", StringType(), True),
    StructField("c9", StringType(), True),
    StructField("c10", StringType(), True),
    StructField("c11", StringType(), True),
    StructField("c12", StringType(), True),
    StructField("c13", StringType(), True),
    StructField("c14", StringType(), True),
    StructField("c15", StringType(), True),
    StructField("c16", StringType(), True),
    StructField("c17", StringType(), True)    
    ])

df = sqlContext.read.load('good_loc.csv', 
                          format='com.databricks.spark.csv', 
                          header='false', 
                          inferSchema='true')

我收到错误:

dataType should be DataType
Traceback (most recent call last):
  File "/usr/hdp/current/spark2-client/python/pyspark/sql/types.py", line 403, in __init__
    assert isinstance(dataType, DataType), "dataType should be DataType"
AssertionError: dataType should be DataType

我认为错误来自TimeStamp Type。我正在使用Spark 2.2

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

StructField("c3", TimestampType, True),
StructField("c4", TimestampType, True),

成为

StructField("c3", TimestampType(), True),
StructField("c4", TimestampType(), True),