hive常用語法

一、表的操作

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默認分隔符\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目標集群路徑(可以查看表所在的集群路徑,分區(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])

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容