您的位置:首页 > 运维架构

Hive数据导入、sqoop数据导入导出

2015-07-30 11:19 344 查看
在进行数据统计的时候经常会遇到把HIVE中的表数据进行导入导出处理,或者是将查询结果导入到另外一个地方,一般是通过Sqoop来进行Mysql和Hdfs进行数据交互。

1、通过一个sql把算出来的结果导入到一张数据表里面,一般的做法是把数据导入到Hdfs中,然后通过和目标表建立分区,把数据load到目标表中;

如:

beeline -u "jdbc:hive2://test.com:10000?mapreduce.job.queuename=test" -n media-sns -e "insert overwrite directory '/user/media-sns/hive/warehouse/test/oper/$time/ac'
select concat(count(a.id),'\t','qq')  
from (select id,collect_set(stat_time) as t from t_user_timeline where stat_time<='$time' group by passport_id) as a 
where size(a.t)=1 and a.t[0]='$time' and a.passport_id like '%@qq.com';" --silent=true --outputformat=tsv --showHeader=false


上面的sql可以把计算的记过导入到hdfs中,然后通过创建关联分区联系到目标表中:

<pre name="code" class="java">beeline -u "jdbc:hive2://test.com:10000?mapreduce.job.queuename=test" -n media-sns -e"
alter table t_report  add if not exists partition(stat_time =$time,termby='qq') location '/user/media-sns/hive/warehouse/sns/operationdate/$time/qq'
" --silent=true --outputformat=tsv --showHeader=false;



这样的话就可以将查出来的记过导入到目标表中。

2、通过sqoop将Mysql中的数据导入到Hive中

方式一:

/opt/sqoop-1.4.4.bin__hadoop-2.0.4-alpha/bin/sqoop-import -D mapred.job.queue.name=test --connect jdbc:mysql://10.16.41.127:3306/test --username root --password root --table users --columns "id,name,age" --where "time>='$timeotherformat 00:00:00' and time<='$timeotherformat 23:59:59' and age= 24" --target-dir /user/media-sns/hive/warehouse/user/data/ -m1 --fields-terminated-by '\t' ;
将数据从mysql中导入到HDFS中

或者:

<pre name="code" class="java">/opt/sqoop-1.4.4.bin__hadoop-2.0.4-alpha/bin/sqoop-import -D mapred.job.queue.name=test --connect jdbc:mysql://10.16.41.127:3306/test --username root --password root --query "select id,name,age from users where age= 24   and time >= '$timeotherformat 00:00:00' and time <= '$timeotherformat 23:59:59' and \$CONDITIONS" --target-dir <pre name="code" class="java">/user/media-sns/hive/warehouse/user/data/ -m1 --fields-terminated-by '\t' ;




这种事通过sql在mysql中查询得到的结果导入到Hdfs中,主要在wher后面要加上\$CONDITIONS,其中的\,是如果加""转义的。

在正常开放使用的过程中, 我们一般是建一个外表,然后指定导入数据的那个文件夹,即定时往该文件夹中导入数据,这样Hive表可以及时的查阅到最新的数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: