1、表复制(五分区表复制)
create table t1 as select name,age from t2 where ds>'2022-06-04'
2、表覆盖(先清空表中的原有数据,再向表中插入数据)
insert overwrite TABLE t1 select name,age from t2 where ds>'2022-06-04'
3、表分区覆盖(先清空表分区中的原有数据,再向表中插入数据)
insert
overwrite
table t1
partition(ds='
2019-08-08'
) select name,age from t2 where
ds>'2022-06-04'4、表创建:
CREATE TABLE if not exists mydb.t1 (name string,age int) PARTITIONED BY ( `ds` string) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'=',', 'serialization.format'=',') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' TBLPROPERTIES ( 'bucketing_version'='2', 'transient_lastDdlTime'='1635410204');
5、表删除(慎用):DROP TABLE table_name;
6、表分区删除:ALTER TABLE example_table DROP PARTITION (date='2022-01-01');
7、表查询聚类(先聚类-再查询-再排序):select a,count(*) as frequency from mydb.t1 group by name order by frequency desc;
8、表内容和结构展示:DESCRIBE mydb.t1;
9、表详细内容和结构展示:DESCRIBE EXTENDED mydb.t1;
10、给已经存在的表插入新列:alter table mydb.t1 add columns (name string);
11、给已经存在的表级联插入新列:alter table mydb.t1 add columns (name string) cascade;
12、批量执行sql命令:hive -f mydb.sql -d mydb;
13、对表的某个字段的值进行分割后分析(爆炸分析),exploded_table表示作为扩展字段使用,explode表示将一行分解为多行
select category1,count(1) as cnt from mytable LATERAL VIEW explode(split(category, ',')) exploded_table AS category1 group by category1
14、对某个字段按照特定的条件进行分组:
select * from
f1,
case
when cast(substr(createtime,11,3) as float) <6 THEN '早上'
when cast(substr(createtime,11,3) as float) <12 THEN '上午'
when cast(substr(createtime,11,3) as float) <18 THEN '下午'
when cast(substr(createtime,11,3) as float) <24 THEN '晚上'
END AS action_section,
count(1) as cnt
from mytable
group by f1,action_section
14、两个字段拼接:select concat(f1,'-',f2) from tablename;
15、 对某个字段聚合值进行字符串合并:select f1, concat_ws('###',collect_list(f2)) from mytable group by f1。 其中,collect_list是将中间结果转换为列表,collect_set是将中间结果转换为集合
大数据平台磁盘空间占用分析:
hdfs dfs -du path | awk '{SUM += $1} END {print SUM/1024*1024*1024}'
大数据路径强制删除(跳过垃圾回收):
hdfs dfs -rm -r -skipTRash path
大数据清空回收站命令(可能由于系统时间不一致问题无法自动清空):hdfs dfs -expunge
使用sqoop从mysql同步数据到hive
sqoop import \
--connect 'jdbc:mysql://IP:PORT/database' \
--username 'username' \
--password 'pwd' \
--table myoracle.table \ -- table和--query二选一
--query "select * from myoracle.table where 1=1 and \$CONDITIONS " \-boundary-query "select min(id),max(id) from myoracle.table where innercondition " \
--target-dir hive库名 \ --如果使用--table则不需要指定
--compress \
--compression-codec org.apache.hadoop.io.compress.SnappyCodec \--delete-target-dir \
--fetch-size=10000 \--hive-import \
--hive-database hivedatabase \--hive-table userlog \
--hive-partition-key ds \
--hive-partition--value $d1 \--hive-drop-import-delims \
--lines-terminated-by '\n' \--fields-terminated-by '\t' \
--hive-overwrite \ --从mysql同步到hive中时,尤其是分区更新时,使用覆盖方式避免数据重复
--split-by id \
-m 128 --不建议太大,很费资源