如何正确地分隔列

jdzmm42g  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(499)

我在使用sparksql时遇到了问题。我尝试将csv文件导入spark db。我的列用分号分隔。所以我做了一个 sep 把柱子分开。但似乎不起作用。有什么建议吗?

USE CarSalesP1935727;
CREATE TABLE IF NOT EXISTS Products
USING CSV
OPTIONS (path "/FileStore/tables/Products.csv", header "true", inferSchema
"true", sep ";");

SELECT * FROM Products LIMIT 10

hfsqlsce

hfsqlsce1#

对问题不太清楚,效果不错-
请注意,env不是 databricks ```
val path = getClass.getResource("/csv/test2.txt").getPath
println(path)

/**
  * file data
  * -----------
  * id;sequence;sequence
  * 1;657985;657985
  * 2;689654;685485
  */
spark.sql(
  s"""
    |CREATE TABLE IF NOT EXISTS Products
    |USING CSV
    |OPTIONS (path "$path", header "true", inferSchema
    |"true", sep ";")
  """.stripMargin)

spark.sql("select * from Products").show(false)
/**
  * +---+---------+---------+
  * |id |sequence1|sequence2|
  * +---+---------+---------+
  * |1  |657985   |657985   |
  * |2  |689654   |685485   |
  * +---+---------+---------+
  */

相关问题