来源:http://gaoxianwei.iteye.com/blog/2158469
1.从本地文件系统中导入数据到hive表
(1)数据准备(/home/sopdm/test.dat):1,wyp,25,13188888888
2,test,30,13899999999
3,zs,34,89931412
(2)首先创建表
use sopdm;
drop table if exists sopdm.wyp;
create table if not exists sopdm.wyp(id int,name string,age int,tel string)
row format delimited
fields terminated by ','
stored as textfile;
(3)从本地文件系统中导入数据到Hive表
load data local inpath ‘/home/sopdm/test.dat’ into table sopdm.wyp;
如果遇到中文乱码问题
分析:直接检查终端配置,已经是utf-8显示;
继续检查linux上的本地导入文件,导入文件内容,发现文件中的中文已经是乱码。
回到windows查看导入文件(txt),发现编码是ANSI的编码,另存为UTF-8
重新上传, load data ,查询hive,中文显示正常。
以上只是乱码的一种最简单问题,仅供参考
subline file ----save wite encoding ---utf-8
dfs -ls /user/sopdm/hive/warehouse/sopdm.db/wyp;
2.从HDFS上导入数据到Hive表
(1)现在hdfs中创建一个input目录存放HDFS文件
hadoop fs -mkdir input; 或 hadoop fs -mkdir /user/sopdm/input;
(2)把本地文件上传到HDFS中,并重命名为test_hdfs.dat
hadoop fs -put /home/sopdm/test.dat /user/sopdm/input/test_hdfs.dat;
(3)查看文件
dfs -cat /user/sopdm/input/test_hdfs.dat;
(4)将内容导入hive表中
--拷贝“本地数据”到“hive”使用:load data local…
--转移“HDFS”到“hive”(必须同一个集群)使用:load data…
load data inpath ‘/user/sopdm/input/test_hdfs.dat’ into table sopdm.wyp;
3.从别的Hive表中导入数据到Hive表中
create table if not exists sopdm.wyp2(id int,name string,tel string)
row format delimited
fields terminated by ','
stored as textfile;
--overwrite是覆盖,into是追加
insert into table sopdm.wyp2
select id,name,tel from sopdm.wyp;
--多表插入
--高效方式-查询语句插入多个分区
from sopdm.wyp w
insert overwrite table sopdm.wyp2
select w.id,w.name,w.tel where w.age=25
insert overwrite table sopdm.wyp2
select w.id,w.name,w.tel where w.age=27;
4.创建Hive表的同时导入查询数据
create table sopdm.wyp3
as select id,name,tel,age from sopdm.wyp where age=25;
从mysql导入到hive
有partition的情况下增加partition
hive -e "alter table st.fs_flow_clssfd_stats add partition(day='20161022') location '/dw/st/fs_flow_clssfd_stats/day=20161022';"
然后再导入
sqoop import --connect jdbc:mysql://10.130.3.218:3306/FSpider --username damadmin --password Abcd.1234 --delete-target-dir --target-dir /dw/st/fs_flow_clssfd_stats/day='20161022' --e "select dy,bu,stats_obj,stats_typ,pv,uv,updatetime from fs_flow_clssfd_stats where dy='20161022' and \$CONDITIONS" --fields-terminated-by '\001' --lines-terminated-by '\n' --m 1;