spark read csv options code example

Example 1: read csv in spark

df = spark.read.format("csv").option("header", "true").load("csvfile.csv")

Example 2: read csv in spark

df = spark. read . format("csv") . option("header", "true")

Example 3: spark.read.option(header,inferschema) .csv example

val customSchema = StructType(Array(
    StructField("numicu", StringType, true),
    StructField("fecha_solicitud", TimestampType, true),
    StructField("codtecnica", StringType, true),
    StructField("tecnica", StringType, true),
    StructField("finexploracion", TimestampType, true),
    StructField("ultimavalidacioninforme", TimestampType, true),
    StructField("validador", StringType, true)))

val df_explo = spark.read
        .format("csv")
        .option("header", "true")
        .option("delimiter", "\t")
        .option("timestampFormat", "yyyy/MM/dd HH:mm:ss") 
        .schema(customSchema)
        .load(filename)

Tags:

Misc Example