我才刚开始学习火花。我知道,如果我们将inferSchema选项设置为true,则模式将自动推断。我正在读一个简单的csv文件。如何在代码中不指定任何自定义架构的情况下动态推断架构。代码应该能够为任何传入数据集构建架构。
是否可以这样做呢?
我尝试使用readStream并将我的格式指定为csv,完全跳过了地狱模式选项,但在任何情况下我似乎都需要提供这个选项。
val ds1: DataFrame = spark
.readStream
.format("csv")
.load("/home/vaibha/Downloads/C2ImportCalEventSample.csv")
println(ds1.show(2))发布于 2019-08-12 08:47:41
您可以动态推断模式,但在某些csv格式的情况下可能会变得有些乏味。更多阅读这里。参考代码示例中的CSV文件,并假设它与一个这里相同,下面的内容将给出您所需的内容:
scala> val df = spark.read.
| option("header", "true").
| option("inferSchema", "true").
| option("timestampFormat","MM/dd/yyyy").
| csv("D:\\texts\\C2ImportCalEventSample.csv")
df: org.apache.spark.sql.DataFrame = [Start Date : timestamp, Start Time: string ... 15 more fields]
scala> df.printSchema
root
|-- Start Date : timestamp (nullable = true)
|-- Start Time: string (nullable = true)
|-- End Date: timestamp (nullable = true)
|-- End Time: string (nullable = true)
|-- Event Title : string (nullable = true)
|-- All Day Event: string (nullable = true)
|-- No End Time: string (nullable = true)
|-- Event Description: string (nullable = true)
|-- Contact : string (nullable = true)
|-- Contact Email: string (nullable = true)
|-- Contact Phone: string (nullable = true)
|-- Location: string (nullable = true)
|-- Category: integer (nullable = true)
|-- Mandatory: string (nullable = true)
|-- Registration: string (nullable = true)
|-- Maximum: integer (nullable = true)
|-- Last Date To Register: timestamp (nullable = true)https://stackoverflow.com/questions/57456970
复制相似问题