Вы можете использовать com.databricks.spark.csv
для чтения CSV-файлов. Пожалуйста, найдите образец кода, как показано ниже.
import org.apache.spark.sql.SparkSession
object SparkCSVTest extends App {
val spark = SparkSession
.builder()
.master("local")
.appName("File_Streaming")
.getOrCreate()
val df = spark.read
.format("com.databricks.spark.csv")
.option("header", "true")
.option("delimiter", "|")
.option("inferSchema", "false")
.load("C:/Users/KZAPAGOL/Desktop/CSV/csvSample.csv")
df.show()
}
Используется CSV-файл:
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|AA|BB|CC|DD|EE|FF|GG|HH|II|JJ|KK|LL|MM|NN|OO|PP|QQ|RR|SS|TT|UU|VV|WW|XX|YY|ZZ|TGHJ|HG|EEE|ASD|EFFDCLDT|QSAS|WWW|DATIME|JOBNM|VFDCXS|REWE|XCVVCX|ASDFF
QW|8|2344|H02|1002| |1|2019-01-20|9999-12-31| |EE|2014-01-20|2014-01-20|2014-01-20|CNB22345 |IN|9|1234444| | | |10|QQ|8|BMX10290M|EWR| |.000000000|00|M |2027-01-20|2027-01-20| |.00|.00|.00|.00|2014-01-20|1901-01-01|3423.25| | | | |RE|WW| |RQ| | | | | | | | |1901-01-01|0|SED2233345 |2019-01-15 22:10:23|213EDSFDS |78978775|2019-03-23 07:38:34.823000000| |
с заголовком:
+---+---+----+---+----+--------------+---+----------+----------+---+---+----------+----------+----------+--------------------+---+---+-------+---+--------+--------+---+---+---+---------+---+---+----------+---+---+----------+----------+---+---+---+---+---+----------+----------+-------+---+----------+----------+------+---+---+---+---+---+---+---+--------+-----+--------+---+---+----------+----+-------------+-------------------+----------+--------+--------------------+--------+-----+
| A| B| C| D| E| F| G| H| I| J| K| L| M| N| O| P| Q| R| S| T| U| V| W| X| Y| Z| AA| BB| CC| DD| EE| FF| GG| HH| II| JJ| KK| LL| MM| NN| OO| PP| QQ| RR| SS| TT| UU| VV| WW| XX| YY| ZZ| TGHJ| HG|EEE|ASD| EFFDCLDT|QSAS| WWW| DATIME| JOBNM| VFDCXS| REWE| XCVVCX|ASDFF|
+---+---+----+---+----+--------------+---+----------+----------+---+---+----------+----------+----------+--------------------+---+---+-------+---+--------+--------+---+---+---+---------+---+---+----------+---+---+----------+----------+---+---+---+---+---+----------+----------+-------+---+----------+----------+------+---+---+---+---+---+---+---+--------+-----+--------+---+---+----------+----+-------------+-------------------+----------+--------+--------------------+--------+-----+
| QW| 8|2344|H02|1002| | 1|2019-01-20|9999-12-31| | EE|2014-01-20|2014-01-20|2014-01-20|CNB22345 | IN| 9|1234444| | | | 10| QQ| 8|BMX10290M|EWR| |.000000000| 00| M |2027-01-20|2027-01-20| |.00|.00|.00|.00|2014-01-20|1901-01-01|3423.25| | | | | RE| WW| | RQ| | | | | | | | |1901-01-01| 0|SED2233345 |2019-01-15 22:10:23|213EDSFDS |78978775|2019-03-23 07:38:...| | null|
+---+---+----+---+----+--------------+---+----------+----------+---+---+----------+----------+----------+--------------------+---+---+-------+---+--------+--------+---+---+---+---------+---+---+----------+---+---+----------+----------+---+---+---+---+---+----------+----------+-------+---+----------+----------+------+---+---+---+---+---+---+---+--------+-----+--------+---+---+----------+----+-------------+-------------------+----------+--------+--------------------+--------+-----+
без заголовка:
+---+---+----+---+----+--------------+---+----------+----------+---+----+----------+----------+----------+--------------------+----+----+-------+----+--------+--------+----+----+----+---------+----+----+----------+----+----+----------+----------+----+----+----+----+----+----------+----------+-------+----+----------+----------+------+----+----+----+----+----+----+----+--------+-----+--------+----+----+----------+----+-------------+-------------------+----------+--------+--------------------+--------+-----+
|_c0|_c1| _c2|_c3| _c4| _c5|_c6| _c7| _c8|_c9|_c10| _c11| _c12| _c13| _c14|_c15|_c16| _c17|_c18| _c19| _c20|_c21|_c22|_c23| _c24|_c25|_c26| _c27|_c28|_c29| _c30| _c31|_c32|_c33|_c34|_c35|_c36| _c37| _c38| _c39|_c40| _c41| _c42| _c43|_c44|_c45|_c46|_c47|_c48|_c49|_c50| _c51| _c52| _c53|_c54|_c55| _c56|_c57| _c58| _c59| _c60| _c61| _c62| _c63| _c64|
+---+---+----+---+----+--------------+---+----------+----------+---+----+----------+----------+----------+--------------------+----+----+-------+----+--------+--------+----+----+----+---------+----+----+----------+----+----+----------+----------+----+----+----+----+----+----------+----------+-------+----+----------+----------+------+----+----+----+----+----+----+----+--------+-----+--------+----+----+----------+----+-------------+-------------------+----------+--------+--------------------+--------+-----+
| A| B| C| D| E| F| G| H| I| J| K| L| M| N| O| P| Q| R| S| T| U| V| W| X| Y| Z| AA| BB| CC| DD| EE| FF| GG| HH| II| JJ| KK| LL| MM| NN| OO| PP| QQ| RR| SS| TT| UU| VV| WW| XX| YY| ZZ| TGHJ| HG| EEE| ASD| EFFDCLDT|QSAS| WWW| DATIME| JOBNM| VFDCXS| REWE| XCVVCX|ASDFF|
| QW| 8|2344|H02|1002| | 1|2019-01-20|9999-12-31| | EE|2014-01-20|2014-01-20|2014-01-20|CNB22345 | IN| 9|1234444| | | | 10| QQ| 8|BMX10290M| EWR| |.000000000| 00| M |2027-01-20|2027-01-20| | .00| .00| .00| .00|2014-01-20|1901-01-01|3423.25| | | | | RE| WW| | RQ| | | | | | | | |1901-01-01| 0|SED2233345 |2019-01-15 22:10:23|213EDSFDS |78978775|2019-03-23 07:38:...| | null|
+---+---+----+---+----+--------------+---+----------+----------+---+----+----------+----------+----------+--------------------+----+----+-------+----+--------+--------+----+----+----+---------+----+----+----------+----+----+----------+----------+----+----+----+----+----+----------+----------+-------+----+----------+----------+------+----+----+----+----+----+----+----+--------+-----+--------+----+----+----------+----+-------------+-------------------+----------+--------+--------------------+--------+-----+
build.sbt
"com.databricks" %% "spark-csv" % "1.5.0",
"org.apache.spark" %% "spark-core" % "2.2.2",
"org.apache.spark" %% "spark-sql" % "2.2.2"
Снимок экрана для Ref. :
Надеюсь, это поможет!