`
m635674608
  • 浏览: 5004885 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Hive四种数据导入方式

    博客分类:
  • Hive
 
阅读更多

Hive四种数据导入方式
文档参考:
http://blog.csdn.net/lifuxiangcaohui/article/details/40588929

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

1. 从本地文件系统中导入数据到Hive表
create table wyp
 (id int, name string,
 age int, tel string)
 ROW FORMAT DELIMITED
 FIELDS TERMINATED BY '\t'
 STORED AS TEXTFILE;

数据放在
[root@node0 hadoop-2.6.4]#cp data.txt /usr/local/apache-hive-2.0.0-bin/
加载数据语句
[root@node0 hadoop-2.6.4]#load data local inpath 'data.txt' into table test;
data.txt
1 9 0 7
Y 7 8 9
hive> desc test;
注意:以制表符作为分隔符
需要注意的是:
和我们熟悉的关系型数据库不一样,Hive现在还不支持在insert语句里面直接给出一组记录的文字形式,也就是说,Hive并不支持INSERT INTO …. VALUES形式的语句。

2. HDFS上导入数据到Hive表
从本地文件系统中将数据导入到Hive表的过程中,其实是先将数据复制到上传用户的HDFS home目录下,比如/usr/hive/warehouse/add.txt,具体的操作如下:

[root@node0 bin]# bin/hadoop fs –rm /usr/hive/warehouse/add.txt 
[root@node0 hadoop-2.6.4]# bin/hadoop fs -put /home/panqiong/Documents/add.txt /usr/hive/warehouse/
[root@node0 hadoop-2.6.4]# bin/hadoop fs -cat /usr/hive/warehouse/add.txt 
16/05/11 18:24:38 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
1 9 0 8 
8 7 8 9 
hive> load data inpath '/usr/hive/warehouse/add.txt' into table wyp;

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

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

假设Hive中有test1表,其建表语句如下所示:
create table test1(
id int, name string
,tel string)
partitioned by
(age int)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;

大体和wyp表的建表语句类似,只不过test1表里面用age作为了分区字段。对于分区,这里在做解释一下:
分区:在Hive中,表的每一个分区对应表下的相应目录,所有分区的数据都是存储在对应的目录中。比如wyp表有dt和city两个分区,则对应dt=20131218,city=BJ对应表的目录为/user/hive/warehouse/dt=20131218/city=BJ,所有属于这个分区的数据都存放在这个目录中。

下面语句就是将wyp表中的查询结果并插入到test表中:
insert into table test1
partition (age=25)
select id, name, tel
from wyp;
我们知道我们传统数据块的形式insert into table values(字段1,字段2),这种形式hive是不支持的。

通过上面的输出,我们可以看到从wyp表中查询出来的东西已经成功插入到test表中去了!如果目标表(test)中不存在分区字段,可以去掉partition (age=’25′)语句。当然,我们也可以在select语句里面通过使用分区值来动态指明分区:
hive> set hive.exec.dynamic.partition.mode=nonstrict;
insert into table test1
partition (age)
select id, name, 
tel, age
from wyp;
hive> select * from test1;
OK
1 0 8 25
9 9 7 25
1 0 8 25
9 9 7 25
9 9 7 8
1 0 8 9
Time taken: 0.689 seconds, Fetched: 6 row(s)

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

insert overwrite table test1
PARTITION (age)
select id, name, tel, age
from wyp;

insert overwrite table test3
PARTITION (age)
select id, name, tel, age
from wyp;

更可喜的是,Hive还支持多表插入,什么意思呢?在Hive中,我们可以把insert语句倒过来,把from放在最前面,它的执行效果和放在后面是一样的,如下:
hive> show create table test3;
OK
CREATE  TABLE test3(
  id int,
  name string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;
数据格式用制表符隔开

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

CREATE  TABLE test4(
  id int,
  name string);
hive 默认的字段分隔符为ascii码的控制符\001,建表的时候用fields terminated by '\001',如果要测试的话,
造数据在vi 打开文件里面,用ctrl+v然后再ctrl+a可以输入这个控制符\001。按顺序,\002的输入方式为ctrl+v,ctrl+b。以此类推。


from wyp
insert into table test1
partition(age)
select id, name, tel, age
insert into table test3
select id, name
where age>25;

hive> select * from test3;
OK
8       wyp4
2       test
3       zs
Time taken: 4.308 seconds, Fetched: 3 row(s)

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

[root@node0 bin]# hadoop fs -put /home/panqiong/file.txt /usr/hive/warehouse/
[root@node0 bin]# bin/hadoop fs -cat /usr/hive/warehouse/file.txt
hive> load data inpath '/usr/hive/warehouse/file.txt' into table add partition (age =30);
 
需要指定分区 
 
数据为空的问题:
文件中需要以制表符隔开,非空格键,否则数据为空
制表符 \t 就是tab键

4. 在创建表的时候通过从别的表中查询出相应的记录并插入到所创建的表中
在实际情况中,表的输出结果可能太多,不适于显示在控制台上,这时候,将Hive的查询输出结果直接存在一个新的表中是非常方便的,我们称这种情况为CTAS(create table .. as select)如下:

create table test4
as
select id, name, tel
from wyp;

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

5. 插入过程报错:
create table test4
    > as
    > select id, name, tel
    > from wyp;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. tez, spark) or using Hive 1.X releases.
Query ID = root_20160511030540_df6875cd-17b0-454e-bd39-5598c41bc592
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1462934737317_0010, Tracking URL = http://localhost:8088/proxy/application_1462934737317_0010/
Kill Command = /root/hadoop/hadoop-2.6.4/bin/hadoop job  -kill job_1462934737317_0010
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2016-05-11 19:26:56,145 Stage-1 map = 0%,  reduce = 0%
2016-05-11 19:27:09,513 Stage-1 map = 100%,  reduce = 0%
Ended Job = job_1462934737317_0010 with errors
Error during job, obtaining debugging information...
Examining task ID: task_1462934737317_0010_m_000000 (and more) from job job_1462934737317_0010

Task with the most failures(4): 
-----
Task ID:
  task_1462934737317_0010_m_000000


URL:
  http://0.0.0.0:8088/taskdetails.jsp?jobid=job_1462934737317_0010&tipid=task_1462934737317_0010_m_000000
-----
Diagnostic Messages for this Task:
Container launch failed for container_1462934737317_0010_01_000005 : org.apache.hadoop.yarn.exceptions.InvalidAuxServiceException: The auxService:mapreduce_shuffle does not exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.apache.hadoop.yarn.api.records.impl.pb.SerializedExceptionPBImpl.instantiateException(SerializedExceptionPBImpl.java:168)
at org.apache.hadoop.yarn.api.records.impl.pb.SerializedExceptionPBImpl.deSerialize(SerializedExceptionPBImpl.java:106)
at org.apache.hadoop.mapreduce.v2.app.launcher.ContainerLauncherImpl$Container.launch(ContainerLauncherImpl.java:155)
at org.apache.hadoop.mapreduce.v2.app.launcher.ContainerLauncherImpl$EventProcessor.run(ContainerLauncherImpl.java:369)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
MapReduce Jobs Launched: 
Stage-Stage-1: Map: 1   HDFS Read: 0 HDFS Write: 0 FAIL
Total MapReduce CPU Time Spent: 0 msec
解决方法:修改配置文件:

hive> insert into table test1
    > partition (age=25)
    > select id, name, tel
    > from wyp;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. tez, spark) or using Hive 1.X releases.
Query ID = root_20160511201903_22a557b4-bc47-4e08-8081-627e9ce5be4d
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1462934737317_0012, Tracking URL = http://localhost:8088/proxy/application_1462934737317_0012/
Kill Command = /root/hadoop/hadoop-2.6.4/bin/hadoop job  -kill job_1462934737317_0012
Interrupting... Be patient, this might take some time.
Press Ctrl+C again to kill JVM
killing job with: job_1462934737317_0012
Hadoop job information for Stage-1: number of mappers: 0; number of reducers: 0
2016-05-11 20:27:19,225 Stage-1 map = 0%,  reduce = 0%
Ended Job = job_1462934737317_0012 with errors
Error during job, obtaining debugging information...
FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
MapReduce Jobs Launched: 
Stage-Stage-1:  HDFS Read: 0 HDFS Write: 0 FAIL
Total MapReduce CPU Time Spent: 0 msec
解决方法:修改配置文件:
hive -hiveconf hive.root.logger=DEBUG,console 

 

http://blog.itpub.net/29050044/viewspace-2098563/

分享到:
评论

相关推荐

    Hive几种数据导入方式

    通过以上四种方式,我们可以根据不同场景灵活选择最合适的方法来完成数据导入任务。需要注意的是,在进行数据导入时,确保数据格式与表结构相匹配是非常重要的。此外,在从本地文件系统或 HDFS 导入数据时,还需关注...

    datax数据从hive导入mysql数据缺失解决

    3. **性能监控**:持续监控数据导入过程中的性能指标,确保调整后的参数没有引入新的瓶颈。 #### 技术细节解析 DataX 在读取 Hive 中的数据时,实际上是按照 HDFS 文件中的行进行读取的。默认情况下,DataX 处理的...

    建立Hive和Hbase的映射关系,通过Spark将Hive表中数据导入ClickHouse

    接下来,Spark作为一个分布式计算框架,提供了一种灵活且高性能的方式来处理数据。在Hive和ClickHouse之间传输数据时,Spark可以作为一个中间层,利用其强大的数据处理能力,将Hive表的数据转换为适合ClickHouse存储...

    Hive数据导入HBase的方法.docx

    Hive 数据导入 HBase 的方法 Hive 是一个基于 Hadoop 的数据仓库工具,而 HBase 是一个基于 Hadoop 的 NoSQL 数据库。它们都是大数据处理的重要组件。在数据处理过程中,经常需要将数据从 Hive 导入到 HBase 中。...

    sqoop导入数据到hive中,数据不一致

    当使用Sqoop将数据导入Hive时,有时可能会遇到数据不一致的问题,这可能是由于多种原因引起的。本文将深入探讨这个问题,并提供可能的解决方案。 Sqoop是一个用于在关系数据库和Hadoop之间传输数据的工具,它可以...

    java解决hive快速导数据到Hbase代码

    2. **获取Hive数据**:使用Hive的Java API(如Hive Metastore Thrift Client)连接到Hive服务,查询并获取所需的数据。这通常涉及到解析HQL(Hive SQL)查询,获取查询结果集。 3. **预处理数据**:根据HBase的存储...

    Hive+经纬度+数据导入ES

    在将Hive中的数据导入到Elasticsearch时,如果Hive表中存在一个表示地理坐标的字段(如`location`),且该字段的类型为`array<double>`,那么直接导入到Elasticsearch后可能会导致该坐标数据无法正常被识别和使用。...

    项目实战——Spark将Hive表的数据写入ElasticSearch(Java版本)

    4. **ElasticSearch数据导入**: Spark提供了`org.elasticsearch.spark.sql.ElasticsearchSpark`库,使得可以直接将DataFrame写入ElasticSearch。我们需要配置ElasticSearch的URL、索引名和映射等信息,然后调用`...

    hive元数据导入sql生成工具

    Hive元数据导入SQL生成工具是针对CDH4.7.0版本设计的一款实用软件,主要用于帮助用户方便地管理和操作Hive中的元数据。Hive是一个分布式数据仓库系统,它允许用户使用类SQL语言(HQL)来处理存储在Hadoop集群上的大...

    sqoop从mysql中导入数据到parquet格式的hive中

    sqoop导入数据到hive

    hive数据怎么导入.docx

    本篇文章将详细解释如何将数据导入Hive,主要涉及两种方法:通过外部表导入和从本地导入。 1. 通过外部表导入: 外部表导入是将已经存在于HDFS上的数据链接到Hive表的一种方式。这种方式适用于已有数据需要在Hive...

    kettle 从oracle数据库导数据到hive 表

    ### Kettle 从 Oracle 数据库导数据到 Hive 表 #### 背景与目的 在企业级数据处理场景中,随着大数据技术的发展,越来越多的企业选择将原有的关系型数据库(如 Oracle)中的数据迁移到基于 Hadoop 生态系统的数据...

    csv 文件 导入hive

    数据文件

    ES-HIVE数据互通

    ### ES-HIVE数据互通知识点详解 #### 环境配置 在进行Elasticsearch与Hive的数据互通之前,首先需要确保环境配置正确无误。本文档提到的环境为实验性的单节点集群,具体配置如下: - **操作系统**:Vagrant + ...

    DataX数据的迁移(MySQL、HDFS,Hive)

    1.将Mysql中的数据迁移到Hdfs文件系统中,然后通过Hive加载HDFS文件系统中的数据值 2.将Hive中的数据迁移到指定Mysql数据库中 注意点: 1.数据迁移的过程中,由于hive的Null值存储为"\N",Mysql存储为NULL值,二者...

    项目实战——钉钉报警校验ElasticSearch和Hive数据仓库内的数据质量(Java版本)

    因为你不知道将Hive的数据导入到了ElasticSearch后,数据量是否准确,所以需要钉钉报警校验ElasticSearch和Hive数据仓库内的数据质量,注意,这个项目打包后,最好另起一个进程调用,并且开始时间为文章1或者2最大...

    hive分区导入

    2. **OOZIE应用部署手册.docx**:OOZIE是Hadoop生态系统中的工作流调度器,可以用于协调Hive、Pig、MapReduce等任务,可能在Hive数据导入流程中起到调度作用,确保任务按照预定顺序执行。 3. **新建 Microsoft ...

    基于hadoop平台hive数据库处理电影数据(8965字数32页).doc

    然后,系统总体设计部分提出了总体方案和基础数据架构,可能涵盖了Hadoop集群的配置、节点设置、网络拓扑以及Hive的表设计和数据导入流程。 在“开发背景”中,可能提到了随着电影行业的快速发展,数据量激增,传统...

    hive 操作相关的测试数据集

    这个测试数据集“hive操作相关的测试数据集hive”显然是为了帮助用户理解和实践Hive的各种操作,包括数据导入、查询、分析和数据导出等。 1. **Hive架构**:Hive的核心组件包括元数据存储、驱动器和编译器。元数据...

    Atlas2.2.0编译、安装及使用(集成ElasticSearch,导入Hive数据).doc

    Atlas2.2.0 编译、安装及使用(集成 ElasticSearch,导入 Hive 数据) Atlas2.2.0 是一个强大的元数据管理工具,它提供了多种数据源的集成和管理功能。在本文中,我们将详细介绍如何编译、安装和使用 Atlas2.2.0,...

Global site tag (gtag.js) - Google Analytics