您的位置:首页 > 大数据

hive中数据的几种加载方式

2017-04-19 23:05 190 查看
1、从linux fs和hdfs中加载

load data [local] inpath 'path' [overwrite] into table tblName [partition_sepc];

[local]:如果加上表示本地地址,如果没有表示HDFS上的地址。

[overwrite]:如果加上表示覆盖之前的数据,如果没有表示追加之前的数据。

[partition_sepc]:如果加上表示加载进相应的分区。

 

2、从其他表中装载

insert table tblName select columns... from otherTblName;

关于overwrite的含义和上述是一致的。

需要注意的是:

tblName表中的字段要和后面select查询的字段要保持一致!!!

eg:hive> from history

> insert overwrite sales select * where dt=’2016-01-16’

> insert overwrite credits select * where dt=’2016-01-17’;

 

3、通过动态分区装载数据【以下字段要搞清楚】

INSERT overwrite TABLE t3 PARTITION(province='gs', city) 

SELECT t.province, t.city FROM temp t WHERE t.province='gs';

注意:静态分区必须出现在动态分区之前!如果要都用动态分区需要开启动态分区的支持。

sethive.exec.dynamic.partition=true;
sethive.exec.dynamic.partition.mode=nonstrict;
sethive.exec.max.dynamic.partitions=1000;

在创建分区的时候,最好不要创建过多的分区,如果分区过多的话,查询也是非常的慢的,就像在window下一个文件夹下面的文件过多会对我们的使用造成非常的不便的。
 
hive能支持多大的分区数呢,可以使用命令sethive.exec.max.dynamic.partitions获取

在这里可能会遇到的问题:
1)Dynamic partition cannot be the parentof a static partition 'year'
原因:加载数据的时候和分区的顺序有关联,动态分区不能作为静态的父目录
2)Cannot insert into target tablebecause column number/types are different 'school': Tableinsclause-0 has 3 columns, but query has 4 columns.
原因:两张表的结构是相似的,插入的时候字段发生冲突了,需要加表的别名来区分。
 

4、创建表的同时装载数据

和之前创建视图的时候的语法非常相似,把view改成table就可以

eg:create table newTblName as select columns from other_tblName;

 

5、使用Import命令导入hdfs上的数据

eg:hive> import table tblName from 'hdfs_uri';

 

6、同一份数据多种处理

hive有一个特性是,hive中可以对同一份数据进行多种处理。Hive本身提供了一个独特的语法,她可以从一个数据源产生多个数据聚合,而无需每次聚合都要重新扫描一次。对于大的数据输入集来说,这个优化可以节约非常可观的时间。

eg:hive> from history

> insert overwrite sales select * where dt=’2016-01-16’

> insert overwrite credits select * where dt=’2016-01-17’;

 

7、Hive和MySQL之间利用sqoop的数据传输

查看mysql 中的数据库:

sqoop list-databases --connect jdbc:mysql://localhost:3306 --username root --password root

 

一、MySQL向Hive进行数据迁移

1°、Sqoop 在 hive 上创建与 mysql 相同的表结构

sqoop create-hive-table --connect jdbc:mysql:///test --table test --username root --password root --hive-table mydb1.test

 

2°、将 mysql 中的数据导入到 hive 中

sqoop import --connect jdbc:mysql:///test --table test --username root --password root --hive-import --hive-table mydb1.test

 

二、Hive 向 向 MySQL

sqoop export --connect jdbc:mysql:///test --username root --password root --table test1 --export-dir /user/hive/warehouse/mydb1.db/test --input-fields-terminated-by '\t'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hive 大数据 load data