Hive的几种常见的数据导入方式
这里介绍四种:
(1)、从本地文件系统中导入数据到Hive表;
(2)、从HDFS上导入数据到Hive表;
(3)、从别的表中查询出相应的数据并导入到Hive表中;
(4)、在创建表的时候通过从别的表中查询出相应的记录并插入到所创建的表中。

一、从本地文件系统中导入数据到Hive表

先在Hive里面创建好表,如下:

1. hive> create table wyp

2.     > (id int, name string,

3.     > age int, tel string)

4.     > ROW FORMAT DELIMITED

5.     > FIELDS TERMINATED BY '\t'

6.     > STORED AS TEXTFILE;

7. OK

8. Time taken: 2.832 seconds

这个表很简单,只有四个字段,具体含义我就不解释了。本地文件系统里面有个/home/wyp/wyp.txt文件,内容如下:

1. [wyp@master ~]$ cat wyp.txt

2. 1       wyp    25      13188888888888

3. 2       test    30     13888888888888

4. 3       zs     34      899314121

wyp.txt文件中的数据列之间是使用\t分割的,可以通过下面的语句将这个文件里面的数据导入到wyp表里面,操作如下:

1. hive> load data local inpath 'wyp.txt' into table wyp;

2. Copying data from file:/home/wyp/wyp.txt

3. Copying file: file:/home/wyp/wyp.txt

4. Loading data to table default.wyp

5. Table default.wyp stats:

6. [num_partitions: 0, num_files: 1, num_rows: 0,total_size: 67]

7. OK

8. Time taken: 5.967 seconds

这样就将wyp.txt里面的内容导入到wyp表里面去了,可以到wyp表的数据目录下查看,如下命令:

1. hive> dfs -ls /user/hive/warehouse/wyp ;

2. Found 1 items

3. -rw-r--r--3 wyp supergroup 67 2014-02-19 18:23/hive/warehouse/wyp/wyp.txt

需要注意的是:

和我们熟悉的关系型数据库不一样,Hive现在还不支持在insert语句里面直接给出一组记录的文字形式,也就是说,Hive并不支持INSERT INTO …. VALUES形式的语句。

二、HDFS上导入数据到Hive表

  从本地文件系统中将数据导入到Hive表的过程中,其实是先将数据临时复制到HDFS的一个目录下(典型的情况是复制到上传用户的HDFS home目录下,比如/home/wyp/),然后再将数据从那个临时目录下移动(注意,这里说的是移动,不是复制!)到对应的Hive表的数据目录里面。既然如此,那么Hive肯定支持将数据直接从HDFS上的一个目录移动到相应Hive表的数据目录下,假设有下面这个文件/home/wyp/add.txt,具体的操作如下:

1. [wyp@master /home/q/hadoop-2.2.0]$bin/hadoop fs -cat /home/wyp/add.txt

2. 5       wyp1    23     131212121212

3. 6       wyp2    24     134535353535

4. 7       wyp3    25     132453535353

5. 8       wyp4    26     154243434355

上面是需要插入数据的内容,这个文件是存放在HDFS上/home/wyp目录(和一中提到的不同,一中提到的文件是存放在本地文件系统上)里面,我们可以通过下面的命令将这个文件里面的内容导入到Hive表中,具体操作如下:

1. hive> load data inpath '/home/wyp/add.txt' into tablewyp;

2. Loading data to table default.wyp

3. Table default.wyp stats:

4. [num_partitions: 0, num_files: 2, num_rows: 0,total_size: 215]

5. OK

6. Time taken: 0.47 seconds

7.

8. hive> select * from wyp;

9. OK

10.5       wyp1    23     131212121212

11.6       wyp2    24     134535353535

12.7       wyp3    25     132453535353

13.8       wyp4    26     154243434355

14.1       wyp    25      13188888888888

15.2       test    30     13888888888888

16.3       zs     34      899314121

17.Time taken: 0.096 seconds, Fetched: 7 row(s)

从上面的执行结果我们可以看到,数据的确导入到wyp表中了!请注意load data inpath ‘/home/wyp/add.txt’ intotable wyp;里面是没有local这个单词的,这个是和一中的区别。

三、从别的表中查询出相应的数据并导入到Hive表中

假设Hive中有test表,其建表语句如下所示:

1. hive> create table test(

2.     > id int, name string

3.     > ,tel string)

4.     > partitioned by

5.     > (age int)

6.     > ROW FORMAT DELIMITED

7.     > FIELDS TERMINATED BY '\t'

8.     > STORED AS TEXTFILE;

9. OK

10.Time taken: 0.261 second

大体和wyp表的建表语句类似,只不过test表里面用age作为了分区字段。对于分区,这里在做解释一下:

分区:在Hive中,表的每一个分区对应表下的相应目录,所有分区的数据都是存储在对应的目录中。比如wyp表有dt和city两个分区,则对应dt=20131218,city=BJ对应表的目录为/user/hive/warehouse/dt=20131218/city=BJ,所有属于这个分区的数据都存放在这个目录中。

下面语句就是将wyp表中的查询结果并插入到test表中:

1. hive> insert into table test

2.     > partition (age='25')

3.     > select id, name, tel

4.     > from wyp;

5. #####################################################################

6.            这里输出了一堆Mapreduce任务信息,这里省略

7. #####################################################################

8. Total MapReduce CPU Time Spent: 1 seconds 310 msec

9. OK

10.Time taken: 19.125 seconds

11.

12.hive> select * from test;

13.OK

14.5       wyp1   131212121212    25

15.6       wyp2   134535353535    25

16.7       wyp3   132453535353    25

17.8       wyp4   154243434355    25

18.1       wyp    13188888888888  25

19.2       test   13888888888888  25

20.3       zs     899314121       25

21.Time taken: 0.126 seconds, Fetched: 7 row(s)

这里做一下说明:
我们知道我们传统数据块的形式insertinto table values(字段1,字段2),这种形式hive是不支持的。

通过上面的输出,我们可以看到从wyp表中查询出来的东西已经成功插入到test表中去了!如果目标表(test)中不存在分区字段,可以去掉partition(age=’25′)语句。当然,我们也可以在select语句里面通过使用分区值来动态指明分区:

1. hive> set hive.exec.dynamic.partition.mode=nonstrict;

2. hive> insert into table test

3.     > partition (age)

4.     > select id, name,

5.     > tel, age

6.     > from wyp;

7. #####################################################################

8.            这里输出了一堆Mapreduce任务信息,这里省略

9. #####################################################################

10.Total MapReduce CPU Time Spent: 1 seconds 510 msec

11.OK

12.Time taken: 17.712 seconds

13.

14.

15.hive> select * from test;

16.OK

17.5       wyp1   131212121212    23

18.6       wyp2   134535353535    24

19.7       wyp3   132453535353    25

20.1       wyp    13188888888888  25

21.8       wyp4   154243434355    26

22.2       test   13888888888888  30

23.3       zs     899314121       34

24.Time taken: 0.399 seconds, Fetched: 7 row(s)

这种方法叫做动态分区插入,但是Hive中默认是关闭的,所以在使用前需要先把hive.exec.dynamic.partition.mode设置为nonstrict。当然,Hive也支持insertoverwrite方式来插入数据,从字面我们就可以看出,overwrite是覆盖的意思,是的,执行完这条语句的时候,相应数据目录下的数据将会被覆盖!而insert into则不会,注意两者之间的区别。例子如下:

1. hive> insert overwrite table test

2.     > PARTITION (age)

3.     > select id, name, tel, age

4.     > from wyp;

更可喜的是,Hive还支持多表插入,什么意思呢?在Hive中,我们可以把insert语句倒过来,把from放在最前面,它的执行效果和放在后面是一样的,如下:

1. hive> show create table test3;

2. OK

3. CREATE  TABLE test3(

4.   id int,

5.   name string)

6. Time taken: 0.277 seconds, Fetched: 18 row(s)

7.

8. hive> from wyp

9.     > insert into table test

10.    > partition(age)

11.    > select id, name, tel, age

12.    > insert into table test3

13.    > select id, name

14.    > where age>25;

15.

16.hive> select * from test3;

17.OK

18.8       wyp4

19.2       test

20.3       zs

21.Time taken: 4.308 seconds, Fetched: 3 row(s)

可以在同一个查询中使用多个insert子句,这样的好处是我们只需要扫描一遍源表就可以生成多个不相交的输出。这个很酷吧!

四、在创建表的时候通过从别的表中查询出相应的记录并插入到所创建的表中

在实际情况中,表的输出结果可能太多,不适于显示在控制台上,这时候,将Hive的查询输出结果直接存在一个新的表中是非常方便的,我们称这种情况为CTAS(create table .. as select)如下:

1. hive> create table test4

2.     > as

3.     > select id, name, tel

4.     > from wyp;

5.

6. hive> select * from test4;

7. OK

8. 5       wyp1   131212121212

9. 6       wyp2   134535353535

10.7       wyp3   132453535353

11.8       wyp4   154243434355

12.1       wyp    13188888888888

13.2       test   13888888888888

14.3       zs     899314121

15.Time taken: 0.089 seconds, Fetched: 7 row(s)

数据就插入到test4表中去了,CTAS操作是原子的,因此如果select查询由于某种原因而失败,新表是不会创建的!

Hive导入数据的四种方法的更多相关文章

  1. 小白鼠排队(map容器插入数据的四种方法)

    题目描述 N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色.帽子的颜色用“red”,“ ...

  2. 【AS3】Flash与后台数据交换四种方法整理

    随着Flash Player 9的普及,AS3编程也越来越多了,所以这次重新整理AS3下几种与后台数据交换方法.1.URLLoader(URLStream)2.FlashRemoting3.XMLSo ...

  3. SQLSERVER数据库中批量导入数据的几种方法

    第一:使用Select Into 语句 如果企业数据库都是采用SQL Server数据库的话,则可以利用select into语句实现数据的导入. select into语句的作用是把数据从另外一个数 ...

  4. http之post方法 提交数据的四种方法

    http协议中,post方法用来向服务端提交数据, 这里介绍四种方式: application/x-www-form-urlencoded multipart/form-data applicatio ...

  5. python导入数据的几种方法

    以下是在我学习过程中常用的两种导入数据的方式 方法一: c = open('ML2017Data/testTarget.csv',"r") file = csv.reader(c) ...

  6. 8bit数据 转换为 16bit数据的四种方法

    [转]玩转嵌入式(公众号) 在入门单片机时,想必大家都都会遇到一下这种情况 unsigned char a = 0x12; unsigned char b = 0x34; unsigned int c ...

  7. 通过InputStream访问文件中的数据的四种方法

    //方法一(每次只读取一个字节) public static void getFile() throws IOException { File file = new File("D:\\a. ...

  8. Java中List集合去除重复数据的四种方法

    1. 循环list中的所有元素然后删除重复   public static List removeDuplicate(List list) { for ( int i = 0 ; i < lis ...

  9. neo4j批量导入数据的两种解决方案

    neo4j批量导入数据有两种方法,第一种是使用cypher语法中的LOAD CSV,第二种是使用neo4j自带的工具neo4j-admin import. LOAD CSV 导入的文件必须是csv文件 ...

随机推荐

  1. Node.js-Usage & Example

    Usage# node [options] [v8 options] [script.js | -e "script"] [arguments] Please see the Co ...

  2. 解决微服务网关Ocelot使用AddStoreOcelotConfigurationInConsul后请求404问题

    一个小插曲,最近研究 netcore 微服务网关,在使用AddStoreOcelotConfigurationInConsul将配置存到consul后,任何经过网关的请求都出现404,并且没有任何有用 ...

  3. 利用ligerUI隐藏某列,并不产生空白列的方法

    var grid;//声明变量 $(function () { //grid初始化 grid = $("#maingrid4").ligerGrid({ columns: [ { ...

  4. P3175 [HAOI2015]按位或

    传送门 一如既往膜拜shadowice巨巨 前置姿势我就没一个会的-- //minamoto #include<bits/stdc++.h> #define R register #def ...

  5. jzoj5906

    我們首先發現,每一條邊都至少走1次,因為我們必須走到每一個節點按按鈕 如果我們不走一個節點,說明這個節點已經有傳送門了,但是必須走到這個節點開傳送門,矛盾 然後我們發現,每一條邊至多經過2次 如果我們 ...

  6. Design-341. Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  7. (2)特征点匹配,并求旋转矩阵R和位移向量t

    include头文件中有slamBase.h # pragma once // 各种头文件 // C++标准库 #include <fstream> #include <vector ...

  8. python基础知识input到while循环

    j周笔记 输入与输出 1.输入 input ('请输入内容')= 字符串 2.输出 print(输出到控制台) 变量vairable               变量就是相当于我们人的名字 1.名字 ...

  9. Swift里字符串(五)Native strings

    Native strings have tail-allocated storage, which begins at an offset of nativeBias from the storage ...

  10. Python小白学习之路(十一)—【局部变量和全局变量】【关键字global】【关键字nonlocal】

    写在前面: 几乎有一周没有更新学习笔记了吧 上周一周身体都不怎么舒服 然后还得写开题报告 然后还得看文献 天天就是写写写写写写~~~~~~改改改改改改~~~~~~~~~ 今天又开始学习了 希望之前的没 ...