我试图通过以下步骤将数据帧加载到Hive表中:
val yearDF = spark.read.format("jdbc").option("url", connectionUrl).option("dbtable", s"(${execQuery}) as year2016").option("user", devUserName).option("password", devPassword).option("partitionColumn","header_id").option("lowerBound", 199199).option("upperBound", 284058).option("numPartitions",10).load()
val hiveCols = col1:coldatatype|col2:coldatatype|col3:coldatatype|col4:coldatatype...col200:datatype
val schemaList = hiveCols.split("\\|")
val hiveColumnOrder = schemaList.map(e => e.split("\\:")).map(e => e(0)).toSeq
val finalDF = yearDF.selectExpr(hiveColumnOrder:_*)
我在“execQuery”中读取的列的顺序与“hiveColumnOrder”相同,只是为了确保顺序,我使用selectExpr再次选择yearDF中的列newDF.write.format("CSV").save("hdfs://username/apps/hive/warehouse/database.db/lines_test_data56/")
如果不存在,则创建表schema.tablename(col1 coldatatype,col2 coldatatype,col3 coldatatype,col4 coldatatype ... col200数据类型) 由','终止的行格式删除字段 存储为文本文件
LOCATION'hdfs://username/apps/hive/warehouse/database.db/lines_test_data56/';
在我将数据框加载到创建的表后,我在这里遇到的问题是当我查询表时,我在查询中得到了不正确的输出。例如:如果我在将数据保存为文件之前对数据帧应用以下查询:
finalDF.createOrReplaceTempView("tmpTable")
select header_id,line_num,debit_rate,debit_rate_text,credit_rate,credit_rate_text,activity_amount,activity_amount_text,exchange_rate,exchange_rate_text,amount_cr,amount_cr_text from tmpTable where header_id=19924598 and line_num=2
我得到了正确的输出。所有值都与列正确对齐:
[19924598,2,null,null,381761.40000000000000000000,381761.4,-381761.40000000000000000000,-381761.4,0.01489610000000000000,0.014896100000000,5686.76000000000000000000,5686.76]
但是在将数据帧保存在CSV文件中之后,在其上创建一个表(步骤4)并在创建的表上应用相同的查询,我看到数据混乱并且与列不正确地映射:
select header_id,line_num,debit_rate,debit_rate_text,credit_rate,credit_rate_text,activity_amount,activity_amount_text,exchange_rate,exchange_rate_text,amount_cr,amount_cr_text from schema.tablename where header_id=19924598 and line_num=2
+---------------+--------------+-------------+------------------+-------------+------------------+--------------------------+-------------------------------+------------------------+-----------------------------+--------------------+-------------------------+--+
| header_id | line_num | debit_rate | debit_rate_text | credit_rate | credit_rate_text | activity_amount | activity_amount_text | exchange_rate | exchange_rate_text | amount_cr | amount_cr_text |
+---------------+--------------+-------------+------------------+-------------+------------------+--------------------------+-------------------------------+------------------------+-----------------------------+--------------------+-------------------------+--+
| 19924598 | 2 | NULL | | 381761.4 | | 5686.76 | 5686.76 | NULL | -5686.76 | NULL | |
所以我尝试使用另一种方法,我在前面创建了hive表,并从dataframe中插入数据:
如果我在作业完成后运行上述选择查询,即使这样也会失败。我尝试使用refresh table schema.table
和msckrepair table schema.table
刷新表,只是为了查看元数据是否有任何问题,但似乎没有任何问题。
任何人都可以让我知道造成这种现象的原因,我在这里操作数据的方式有什么问题吗?
代码使用Spark 2.3.2进行测试
而是从CSV文件创建Spark数据帧,然后将其注册为Hive Table,您可以轻松地运行SQL命令并从CSV文件创建Hive表
val conf = new SparkConf
conf
.set("hive.server2.thrift.port", "10000")
.set("spark.sql.hive.thriftServer.singleSession", "true")
.set("spark.sql.warehouse.dir", "hdfs://PATH_FOR_HIVE_METADATA")
.set("spark.sql.catalogImplementation","hive")
.setMaster("local[*]")
.setAppName("ThriftServer")
val spark = SparkSession.builder()
.config(conf)
.enableHiveSupport()
.getOrCreate()
现在使用spark
对象,您可以作为Hive用户运行SQL命令:
spark.sql("DROP DATABASE IF EXISTS my_db CASCADE")
spark.sql("create database if not exists my_db")
spark.sql("use my_db")
使用以下代码,您可以在HDFS目录中加载所有csv_files(或者您可以指定一个CSV文件的路径):
spark.sql(
"CREATE TABLE test_table(" +
"id int," +
"time_stamp bigint," +
"user_name string) " +
"ROW FORMAT DELIMITED " +
"FIELDS TERMINATED BY ',' " +
"STORED AS TEXTFILE " +
"LOCATION 'hdfs://PATH_TO_CSV_Directory_OR_CSV_FILE' "
)
并在最后注册Spark sqlContext对象作为Hive ThriftServer:
HiveThriftServer2.startWithContext(spark.sqlContext)
这将在端口10000上创建ThriftServer端点。
INFO ThriftCLIService: Starting ThriftBinaryCLIService on port 10000 with 5...500 worker threads
现在您可以运行beeline并连接到ThriftServer:
beeline> !connect jdbc:hive2://localhost:10000
Connecting to jdbc:hive2://localhost:10000
Enter username for jdbc:hive2://localhost:10000: enter optional_username
Enter password for jdbc:hive2://localhost:10000: leave blank
Connected to: Spark SQL (version 2.3.2)
Driver: Hive JDBC (version 1.2.1.spark2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://localhost:10000>
并测试表test_table
是否在my_db
数据库下创建:
0: jdbc:hive2://localhost:10000> use my_db;
0: jdbc:hive2://localhost:10000> show tables ;
+-----------+-----------------------+--------------+--+
| database | tableName | isTemporary |
+-----------+-----------------------+--------------+--+
| my_db | test_table | false |
+-----------+-----------------------+--------------+--+
此外,您可以使用ThrifServer JDBC端点创建任何其他Hive表(或任何HiveQL命令)。
以下是所需的依赖项:
libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-core" % sparkVersion,
"org.apache.spark" %% "spark-sql" % sparkVersion,
"org.apache.spark" %% "spark-hive" % sparkVersion,
"org.apache.spark" %% "spark-hive-thriftserver" % sparkVersion,
"org.apache.hadoop" % "hadoop-hdfs" % "2.8.3",
"org.apache.hadoop" % "hadoop-common" % "2.8.3",
)
我在Hive DDL中使用了rowformat serde:org.apache.hadoop.hive.serde2.OpenCSVSerde。这也有','作为默认分隔符char,我没有给出任何其他分隔符。