hive命令行
1 2 3 4 5
| 1--config hive配置文件路径
2--service hive 服务
3--auxpath 添加jar包(多个jar包用:分割)
4
5 |
1 2 3 4 5 6 7 8 9 10 11 12 13
| 1Service List: beeline cli help hiveburninclient hiveserver2 hiveserver hwi jar lineage metastore metatool orcfiledump rcfilecat schemaTool version
2Parameters parsed:
3 --auxpath : Auxillary jars
4 --config : Hive configuration directory
5 --service : Starts specific service/component. cli is default
6Parameters used:
7 HADOOP_HOME or HADOOP_PREFIX : Hadoop install directory
8 HIVE_OPT : Hive options
9For help on a particular service:
10 ./hive --service serviceName --help
11Debug help: ./hive --debug --help
12
13 |
1 2 3 4 5 6 7 8 9 10
| 1hive --service cli --help (相当于直接执行hive -h)
2 -e 直接执行hql语句
3 -v 输出hql语句
4 -S 屏蔽执行过程
5 -f 执行hql文件
6 -i 执行一个文件
7 -d 用户自定义变量
8 --hivevar 用户自定义变量
9
10 |
-
hive cli更多功能
-
直接执行shell 命令
1 2 3
| 1hive> set hive.cli.print.current.db=true;
2
3 |
1 2 3
| 1hive> set hive.cli.print.header=true;
2
3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| 1启动web GUI
2nohup hive --service hwi > /dev/null 2>&1 &
3http://master:9999/hwi
4
5启动元数据
6nohup hive --service metastore > /dev/null 2>&1 &
7
8 启动远程服务接口
9nohup hive --service hiveserver2 > /dev/null 2>&1 &
10
11启动hcatlog
12先关闭matastore
13nohup hcat_server.sh start > /dev/null 2>&1 &
14
15启动webHcat
16webhcat_server.sh start &
17http://192.168.28.161:50111/templeton/v1/status
18
19 |
1 2 3 4 5 6 7 8 9 10 11 12
| 1/home/hadoop/soft/spark2/bin/beeline
2beeline> !connect jdbc:hive://artemis-02:10000 hadoop hadoop
3
4/home/hadoop/soft/spark2/bin/beeline -u jdbc:hive://artemis-02:10000 -n hadoop -p hadoop --silent=true -e "show databases"
5
6
7/home/hadoop/soft/spark2/bin/beeline -u jdbc:hive://artemis-02:10000 -n hadoop -p hadoop --silent=true --showHeader=false -e "show databases"
8
9
10/home/hadoop/soft/spark2/bin/beeline -u jdbc:hive://artemis-02:10000 -n hadoop -p hadoop --silent=true --showHeader=false --outputformat=csv2 -e "select * from lvxw.manage_partition"
11
12 |
数据库操作
1 2 3 4 5 6 7
| 1show databases;
2show databases like 'lv*'
3set hive.metastore.warehouse.dir; 查看数据库所在hdfs根目录
4describe database lvxw; 查看数据库信息
5describe database EXTENDED lvxw; 查看数据库详细信息
6
7 |
1 2 3 4 5 6 7
| 1create database lvxw;
2create database if not exists lvxw;
3create database lvxw LOCATION 'newpath'; 创建数据库,并指定新的HDFS目录
4create database lvxw2 comment 'this is my database named lvxw2';
5create database lvxw with DBPROPERTIES('created-by'='lvxw','carete-date'='2018-09-10');
6
7 |
1 2 3 4 5
| 1drop database lvxw;
2drop database if exists lvxw;
3drop database if exists lvxw CASCADE; 删除数据库和其中的表
4
5 |
1 2 3
| 1alter database lvxw set DBPROPERTIES('edited-by'='lvxw');
2
3 |
表操作
1 2 3 4 5 6 7 8 9 10 11
| 1create table if not exists test(
2 id INT comment '编号',
3 component STRING comment '组件'
4)
5comment '这只一张用于测试的表'
6row format delimited fields
7terminated by ','
8LOCATION 'hdfs://artemis-02:9000/tmp/lvxw/hive'
9TBLPROPERTIES('carete-date'='2018-09-10');
10
11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 1create table if not exists manage_partition(
2 id INT comment '编号',
3 component STRING comment '组件'
4)
5comment '这只一张内部分区表'
6PARTITIONED BY (
7 year string,
8 month string,
9 day string
10)
11row format delimited fields
12terminated by ','
13LOCATION 'hdfs://artemis-02:9000/tmp/lvxw/hive_manage'
14TBLPROPERTIES('carete-date'='2018-09-10');
15
16alter table manage_partition add if not exists partition(year="2018", month="01", day="01") LOCATION "/tmp/lvxw/hive_manage/2018/01/01";
17
18 |
1 2 3 4
| 1show partitions manage_partition
2show partitions manage_partition partition(year=2018,month=01)
3
4 |
1 2 3
| 1 create table if not exists test2 like test;
2
3 |
1 2 3 4 5 6 7 8 9 10 11
| 1create external table if not exists test2(
2 id INT comment '编号',
3 component STRING comment '组件'
4)
5comment '这只一张用于测试的表'
6row format delimited fields
7terminated by ','
8LOCATION 'hdfs://artemis-02:9000/tmp/lvxw/hive'
9TBLPROPERTIES('carete-date'='2018-09-10');
10
11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 1create external table if not exists external_partition(
2 id INT comment '编号',
3 component STRING comment '组件'
4)
5comment '这只一张外部分区表'
6PARTITIONED BY (
7 year string,
8 month string,
9 day string
10)
11row format delimited fields
12terminated by ','
13LOCATION 'hdfs://artemis-02:9000/tmp/lvxw/hive_external'
14TBLPROPERTIES('carete-date'='2018-09-10');
15
16alter table external_partition add if not exists partition(year="2018", month="01", day="01") LOCATION "/tmp/lvxw/hive_external/2018/01/01";
17
18 |
1 2 3 4 5 6 7 8 9 10 11 12
| 1show tables;
2show tables in test;
3show tables 'te*'
4show create table test;
5show TBLPROPERTIES test; 查看表的属性信息
6describe test;
7describe extended test;
8describe formatted test;
9describe extended test.id; 具体某一列
10describe formatted test.id; 具体某一列
11
12 |
1 2 3
| 1drop table if exists test;
2
3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 1alter table test rename to test_test; 修改表明
2
3alter table external_partition add if not exists partition(year="2018", month="01", day="02") LOCATION "/tmp/lvxw/hive_external/2018/01/02" partition(year="2018", month="01", day="03") LOCATION "/tmp/lvxw/hive_external/2018/01/03"; 增加分区
4
5alter table external_partition drop if exists partition(year="2018", month="01", day="02"); 删除分区
6
7alter table external_partition CHANGE COLUMN id ids INT AFTER component;
8alter table external_partition CHANGE COLUMN ids id INT;
9alter table external_partition CHANGE COLUMN component component STRING AFTER id; 修改列名、位置
10
11alter table external_partition ADD COLUMNS(other STRING); 增加字段
12
13alter table external_partition REPLACE COLUMNS(
14 ids INT comment '编号',
15 component STRING comment '组件'
16); 修改或删除字段
17
18 alter table external_partition SET TBLPROPERTIES('edit-date'='2018-09-10'); 修改、增加表属性
19
20 |
线上如何创建分区表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 1 use temp_tasks;
2 CREATE EXTERNAL TABLE `monitor_tip`(
3 `ip` string,
4 `ad` string,
5 `playtime` string,
6 `imp_chance_id` string,
7 `miaozhen_ip` string,
8 `admaster_ip` string
9 )
10 PARTITIONED BY (
11 `year` string,
12 `month` string,
13 `day` string
14 )
15 ROW FORMAT SERDE
16 'com.bizo.hive.serde.csv.CSVSerde'
17 STORED AS INPUTFORMAT
18 'org.apache.hadoop.mapred.TextInputFormat'
19 OUTPUTFORMAT
20 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
21 LOCATION
22 'hdfs://hdfsnewcluster/tmp/ad_tmp'
23
24
25
26 alter table monitor_tip add if not exists partition(year="2018", month="07", day="09") LOCATION "/tmp/ad_tmp/2018/07/09";
27 |