“skip.header.line.count”=“1” does not work in Hive

2019-07-25 13:39发布

问题:

I am trying to load a CSV data into Hive table using SparkSession. I want to skip the header data while loading into hive table and setting tblproperties("skip.header.line.count"="1") is also not working.

I am using the following code.

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

Error in consol:

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

AS shown in the result , I want to skip the first row of data

回答1:

If you are using sql then the workaround is to add filter to the sql:

sql("select * from BookTemp limit 5 where BookTitle!='Book-Title'").show

This Jira is related: https://issues.apache.org/jira/browse/SPARK-11374

Also read this: https://github.com/apache/spark/pull/14638 - you can use CSV Reader option:

spark.read.option("header","true").csv("/data").show

Or remove header using shell before loading:

file="myfile.csv"
tail -n +2 "$file" > "$file.tmp" && mv "$file.tmp" "$file"