一罩句、表的操作
1樟结、創(chuàng)建表、分區(qū)伺糠、分隔符
create table table_name(
? name string,
? age string)
partitioned by (
dt string)
row format delimited fields terminated by '\t';
dt為分區(qū)檀轨,并且指定分隔符為\t? hive默認(rèn)分隔符\001
2胸竞、刪除表數(shù)據(jù)、刪除表和sql相同?
truncate table ,drop table;
3参萄、刪除表的分區(qū)
alter table table_name drop if exists partition? (dt = 20180808);
4卫枝、表添加字段
alter table?table_name?add?columns(c1 int,?c2 int);
5、分區(qū)表表結(jié)構(gòu)的復(fù)制和包含數(shù)據(jù)的復(fù)制
二讹挎、數(shù)據(jù)的導(dǎo)入和導(dǎo)出
1校赤、從本地文件加載
load data local inpath '/home/coco/demo/*' into table test_table
2、hive查詢數(shù)據(jù)寫入到本地文件 并且指定分隔符
insert overwrite local directory '/home/coco/output' row format delimited fields terminated by '\t' select age,count(*) from test_name group by age order by age;
3筒溃、sqoop導(dǎo)入
sqoop import --connect jdbc:mysql://10.1.1.1:3306/user_center --username root --password 123456 --query "select * from user where? age=18 and \$CONDITIONS " --hive-import --target-dir /user/coco/user_all/city=beijing --hive-database user_center --hive-table test_table --split-by age --hive-partition-key city --hive-partition-value beijing?--fields-terminated-by '\t'
\CONDITIONS為必填的??
?jdbc:mysql:// mysql地址/端口/庫名?--username mysql用戶名??--password mysql密碼?--query 設(shè)為指定sql查詢??--target-dir目標(biāo)集群路徑(可以查看表所在的集群路徑,分區(qū))??--hive-database hive的庫名?--hive-table hive的表名??--split-by 分割字段(盡量選取均勻分布的字段)?--hive-partition-key 如果有是分區(qū)表分區(qū)的key??--hive-partition-value 分區(qū)的值?--fields-terminated-by 分隔符
sqoop導(dǎo)入的防止只支持單分區(qū)表马篮,如果需要導(dǎo)入多分區(qū)表,需要復(fù)雜些提供兩種思路
直接把數(shù)據(jù)導(dǎo)入到表所在的路徑怜奖,然后加載分區(qū)
或者建一個臨時的不分區(qū)表浑测,通過動態(tài)分區(qū)加載到分區(qū)表中
sqoop導(dǎo)入出錯 報java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@5f058f00 is still active. No statements may be...
--driver com.mysql.jdbc.Driver
接著出現(xiàn)The last packet sent successfully to the server was 0 milliseconds a
jdbc:mysql://10.1.1.1:3306/user_center?autoReconnect=true
4、sqoop導(dǎo)出
sqoop export --connect "jdbc:mysql://10.1.1.1:3306:3306/user_center?useUnicode=true&characterEncoding=utf-8" --username root --password 123456 --table user --export-dir /user/hive/warehouse/user_center.db/user_center --input-fields-terminated-by '\001' --input-null-string '\\N' --input-null-non-string '\\N'
三歪玲、數(shù)據(jù)處理
1迁央、hive的去重
select * from( select *,row_number() over (partition by source_url order by phone asc) num from spider) t where t.num=1
partition by 后是需要去重的字段,可以添加多個滥崩,逗號分割岖圈。order by根據(jù)什么字段排序,決定著去重的時候保留去重哪些條數(shù)據(jù)钙皮,保留那一條數(shù)據(jù)
2蜂科、hive動態(tài)分區(qū)
hive -v -e "
? use test_db;
? set hive.exec.dynamic.partition=true;
? set hive.exec.dynamic.partition.mode=nonstrict;
? set hive.cli.print.header=true;
? insert overwrite table user_table partition(city,dt) select a,b,c,city,dt from user_temp where age>18;
"
主要設(shè)置
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
查詢的最后字段就是分區(qū)的字段
3、hive引用python腳本處理數(shù)據(jù)
hive -v -e"
use test_db;
set hive.cli.print.header=false;
set hive.exec.dynamic.partition.mode=nonstrict;
set mapreduce.map.memory.mb=1025;
set mapreduce.reduce.memory.mb=1025;
add file /data/coco/house_.py;
drop table if exists test_table;
create table test_table as
SELECT TRANSFORM(a,b,c,d,e,f) USING 'house_.py'
AS a,b,c FROM user_table;"
python腳本中
for line in sys.stdin:
? ? (a,b,c) = line.split('\t')
? ? d=a+b
? ? e=1
? ? f=2
????print '\t'.join([a,b,c,d,e,f])