如何Load TXT 到HDInsight Hive table

如何Load TXT 到HDInsight Hive table

记得以前做过一个小项目,需要把客户整理的TXT导入到数据库,然后结合客户的需求统计分析特定条件的报表,比如表的schema为:time, name, meeting, level。需求统计特定的时间有多少人开过会等等。

迁移txt到数据库的方法有很多,比如:SSIS或者开发entity framework,读txt文件内容,然后写到数据库。这里我们介绍如何用HDInsight load txt到 HDI hive table,同样可以实现客户的需求。

上传hivetable.txt到HDI的headnode。

SSH到创建好的HDInsight headnode,查看文件内容。

sshuser@hn0-hdites:~$ cat hivetable.txt
linlin,123,male
brian,345,male
lin,567,female

复制txt文件到HDFS存储:

hdfs dfs -copyFromLocal hivetable.txt wasb://[email protected]/hive/

Note: hditest.blob.core.windows.net为Azure Blob存储数据库。

连接到Hive接口:

beeline -u 'jdbc:hive2://headnodehost:10001/;transportMode=http'

针对TXT文件,创建表结构如下:

CREATE TABLE hiveexample (
name string,
id int,
sex string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
0: jdbc:hive2://headnodehost:10001/> SHOW CREATE TABLE hiveexample;
+-------------------------------------------------------------------------------------------------------------------------------+--+
|                                                        createtab_stmt                                                         |
+-------------------------------------------------------------------------------------------------------------------------------+--+
| CREATE TABLE `hiveexample`(                                                                                                   |
|   `name` string,                                                                                                              |
|   `id` int,                                                                                                                   |
|   `sex` string)                                                                                                               |
| ROW FORMAT DELIMITED                                                                                                          |
|   FIELDS TERMINATED BY ','                                                                                                    |
|   LINES TERMINATED BY '\n'                                                                                                    |
| STORED AS INPUTFORMAT                                                                                                         |
|   'org.apache.hadoop.mapred.TextInputFormat'                                                                                  |
| OUTPUTFORMAT                                                                                                                  |
|   'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'                                                                |
| LOCATION                                                                                                                      |
|   'wasb://[email protected]/hive/warehouse/hiveexample'  |
| TBLPROPERTIES (                                                                                                               |
|   'numFiles'='1',                                                                                                             |
|   'numRows'='0',                                                                                                              |
|   'rawDataSize'='0',                                                                                                          |
|   'totalSize'='49',                                                                                                           |
|   'transient_lastDdlTime'='1570359102')                                                                                       |
+-------------------------------------------------------------------------------------------------------------------------------+--+
19 rows selected (0.656 seconds)

创建好的表结构如下:

0: jdbc:hive2://headnodehost:10001/> show tables;
+------------------+--+
|     tab_name     |
+------------------+--+
| hiveexample      | |
+------------------+--+

导入HDFS上的存储文件hivetable.txt到表hiveexample:

LOAD DATA INPATH '/hive/hivetable.txt' OVERWRITE INTO TABLE hiveexample;

查看表内容:

0: jdbc:hive2://headnodehost:10001/> select * from hiveexample;
+-------------------+-----------------+------------------+--+
| hiveexample.name  | hiveexample.id  | hiveexample.sex  |
+-------------------+-----------------+------------------+--+
| linlin            | 123             | male             |
| brian             | 345             | male             |
| lin               | 567             | female           |
+-------------------+-----------------+------------------+--+

0: jdbc:hive2://headnodehost:10001/> select * from hiveexample where sex = 'male';
+-------------------+-----------------+------------------+--+
| hiveexample.name  | hiveexample.id  | hiveexample.sex  |
+-------------------+-----------------+------------------+--+
| linlin            | 123             | male             |
| brian             | 345             | male             |
+-------------------+-----------------+------------------+--+
2 rows selected (0.615 seconds)

这样你就可以用SQL的语言对表hiveexample做操作。

个人觉得Hive这个操作起来更灵活,方便,你值得拥有。

在这里插入图片描述

发布了13 篇原创文章 · 获赞 0 · 访问量 969
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章