我爱编程

[一起学Hive]之七-向Hive表中加载数据

2018-05-23  本文已影响0人  antyzhu

在Hive中建好表之后,需要将数据加载进来,以便做后续查询分析,本文介绍向Hive表中加载数据的几种方式。

6.1 建表时候直接指定

如果你的数据已经在HDFS上存在,已经为结构化数据,并且数据所在的HDFS路径不需要维护,那么可以直接在建表的时候使用location指定数据所在的HDFS路径即可。

比如:

<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. CREATE [EXTERNAL] TABLE t_lxw1234 (
  2. day STRING,
  3. url STRING)
  4. ROW FORMAT DELIMITED
  5. FIELDS TERMINATED BY ' '
  6. stored as textfile
  7. location '/tmp/lxw1234/';

</pre>

这里内部表和外部表都可以指定,但需要注意,如果是内部表,那么在DROP该表的时候,同时会将LOCATION所指定的目录一起删除。

6.2 从本地文件系统或者HDFS的一个目录中加载

如果数据在本地,或者HDFS的某一个目录下,需要加载到目标中或分区中,那么使用LOAD DATA命令即可加载数据:

LOAD DATA LOCAL INPATH ‘/home/lxw1234/t_lxw1234/’

INTO TABLE t_lxw1234 PARTITION (day = ‘2015-06-15’);

LOAD DATA INPATH ‘/user/lxw1234/t_lxw1234/’

INTO TABLE t_lxw1234 PARTITION (day = ‘2015-06-15’);

6.3 从一个子查询中加载数据

这个比较简单,就是将一个查询结果插入到目标表或分区中:

INSERT overwrite TABLE t_lxw1234 PARTITION (day = ‘2015-06-15’)

SELECT day,url from source_table;

6.4 导出Hive中的数据到文件系统

这里也介绍一下从Hive中导出数据到文件系统(HDFS和本地文件系统)。

语法为:

<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. INSERT OVERWRITE [LOCAL] DIRECTORY directory1
  2. [ROW FORMAT row_format] [STORED AS file_format]
  3. SELECT ... FROM ...

</pre>

如果指定了LOCAL关键字,则为导出到本地文件系统,否则,导出到HDFS。
使用ROW FORMAT关键字可以指定导出的文件分隔符,比如:

<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. INSERT OVERWRITE LOCAL DIRECTORY '/tmp/lxw1234/'
  2. ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
  3. SELECT * FROM t_lxw1234;

</pre>

该语句将t_lxw1234表的所有数据导出到本地文件系统/tmp/lxw1234/目录,字段间的分隔符为逗号。

cat /tmp/lxw1234/000000_0
2015-05-10,url1
2015-05-10,url2
2015-06-14,url1
2015-06-14,url2
2015-06-15,url1
2015-06-15,url2

更多关于Hive数据加载和导出的介绍,请参考官方文档:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML#LanguageManualDML-Loadingfilesintotables

Hive相关文章(持续更新)

一起学Hive系列

—-Hive概述,Hive是什么

—-Hive函数大全-完整版

—-Hive中的数据库(Database)和表(Table)

—-Hive的安装配置

—-Hive的视图和分区

—-Hive的动态分区

Hive分析函数系列

Hive索引

hive优化之——控制hive任务中的map数和reduce数

如果觉得本博客对您有帮助,请 赞助作者

转载请注明:lxw的大数据田地 » [一起学Hive]之七-向Hive表中加载数据

上一篇下一篇

猜你喜欢

热点阅读