我正在尝试使用 SparkSession 将 CSV 数据加载到 Hive 表中。
我想在加载到配置单元表时跳过标题数据,并且设置 tblproperties("skip.header.line.count"="1") 也不起作用。
我正在使用以下代码。
import java.io.File
import org.apache.spark.sql.{SparkSession,Row,SaveMode}
case class Record(key: Int, value: String)
val warehouseLocation=new File("spark-warehouse").getAbsolutePath
val spark=SparkSession.builder().appName("Apache Spark Book Crossing Analysis").config("spark.sql.warehouse.dir",warehouseLocation).enableHiveSupport().getOrCreate()
import spark.implicits._
import spark.sql
//sql("set hive.vectorized.execution.enabled=false")
sql("drop table if exists BookTemp")
sql ("create table BookTemp(ISBN int,BookTitle String,BookAuthor String ,YearOfPublication int,Publisher String,ImageURLS String,ImageURLM String,ImageURLL String)row format delimited fields terminated by ';' ")
sql("alter table BookTemp set TBLPROPERTIES("skip.header.line.count"="1")")
sql("load data local inpath 'BX-Books.csv' into table BookTemp")
sql("select * from BookTemp limit 5").show
控制台错误:
res55: org.apache.spark.sql.DataFrame = []
<console>:1: error: ')' expected but '.' found.
sql("alter table BookTemp set TBLPROPERTIES("skip.header.line.count"="1")")
2019-02-20 22:48:09 WARN LazyStruct:151 - Extra bytes detected at the end of the row! Ignoring similar problems.
+----+--------------------+--------------------+-----------------+--------------------+--------------------+--------------------+--------------------+
|ISBN| BookTitle| BookAuthor|YearOfPublication| Publisher| ImageURLS| ImageURLM| ImageURLL|
+----+--------------------+--------------------+-----------------+--------------------+--------------------+--------------------+--------------------+
|null| "Book-Title"| "Book-Author"| null| "Publisher"| "Image-URL-S"| "Image-URL-M"| "Image-URL-L"|
|null|"Classical Mythol...|"Mark P. O. Morford"| null|"Oxford Universit...|"http://images.am...|"http://images.am...|"http://images.am...|
|null| "Clara Callan"|"Richard Bruce Wr...| null|"HarperFlamingo C...|"http://images.am...|"http://images.am...|"http://images.am...|
|null|"Decision in Norm...| "Carlo D'Este"| null| "HarperPerennial"|"http://images.am...|"http://images.am...|"http://images.am...|
|null|"Flu: The Story o...| "Gina Bari Kolata"| null|"Farrar Straus Gi...|"http://images.am...|"http://images.am...|"http://images.am...|
+----+--------------------+--------------------+-----------------+--------------------+--------------------+--------------------+--------------------+
only showing top 5 rows
如结果所示,我想跳过第一行数据
如果您使用 sql,那么解决方法是向 sql 添加过滤器:
sql("select * from BookTemp limit 5 where BookTitle!='Book-Title'").show
这个 Jira 是相关的:https://issues.apache.org/jira/browse/SPARK-11374 https://issues.apache.org/jira/browse/SPARK-11374
另请阅读以下内容:https://github.com/apache/spark/pull/14638 https://github.com/apache/spark/pull/14638- 您可以使用 CSV 阅读器选项:
spark.read.option("header","true").csv("/data").show
或者在加载之前使用 shell 删除标头:
file="myfile.csv"
tail -n +2 "$file" > "$file.tmp" && mv "$file.tmp" "$file"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)