##默认有一个default库 hive> show databases; OK default Time taken: 2.181 seconds, Fetched: 1 row(s) ##创建DB1数据库 hive> create database db1; OK Time taken: 2.84 seconds hive> show databases; OK db1 default Time taken: 0.157 seconds, Fetched: 2 row(s) hive> use db1; OK Time taken: 0.013 seconds ##在DB1里面创建一个user表 hive> create table user(id int,name string); OK Time taken: 0.24 seconds hive> insert into user values(1,'xiaoming'); Query ID = bigdata_20201206224646_549765c3-8cb8-4138-8d36-b2a7f6f639ee Total jobs = 3 Launching Job 1 out of 3 Number of reduce tasks is set to 0 since there's no reduce operator Starting Job = job_1606543264596_0001, Tracking URL = http://hadoop001:7776/proxy/application_1606543264596_0001/ Kill Command = /home/bigdata/app/hadoop/bin/hadoop job -kill job_1606543264596_0001 Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0 2020-12-06 22:46:39,228 Stage-1 map = 0%, reduce = 0% 2020-12-06 22:46:43,506 Stage-1 map = 100%, reduce = 0% MapReduce Total cumulative CPU time: 1 seconds 600 msec Ended Job = job_1606543264596_0001 Stage-4 is selected by condition resolver. Stage-3 is filtered out by condition resolver. Stage-5 is filtered out by condition resolver. Moving data to: hdfs://hadoop001:9000/user/hive/warehouse/db1.db/user/.hive-staging_hive_2020-12-06_22-46-29_182_5728351694700927030-1/-ext-10000 Loading data to table db1.user Table db1.user stats: [numFiles=1, numRows=1, totalSize=11, rawDataSize=10] MapReduce Jobs Launched: Stage-Stage-1: Map: 1 Cumulative CPU: 1.6 sec HDFS Read: 3753 HDFS Write: 75 SUCCESS Total MapReduce CPU Time Spent: 1 seconds 600 msec OK Time taken: 16.701 seconds hive> select * from user; OK 1 xiaoming Time taken: 0.059 seconds, Fetched: 1 row(s)
[bigdata@hadoop001 conf]$ vi hive-site.xml <property> <name>hive.cli.print.header</name> <value>true</value> </property> <property> <name>hive.cli.print.current.db</name> <value>true</value> </property> [bigdata@hadoop001 ~]$ hive 【重启hive】 hive (default)> show databases; OK database_name db1 default Time taken: 0.029 seconds, Fetched: 2 row(s) hive (default)> use db1; OK Time taken: 0.025 seconds hive (db1)> show tables; OK tab_name user Time taken: 0.018 seconds, Fetched: 1 row(s) hive (db1)> select * from user; OK user.id user.name 1 xiaoming Time taken: 0.339 seconds, Fetched: 1 row(s)
hive --hiveconf 设置
1 2 3 4 5 6 7
[bigdata@hadoop001 hive]$ hive --hiveconf hive.cli.print.current.db=false which: no hbase in (/home/bigdata/app/hive/bin:/home/bigdata/app/hadoop/bin:/home/bigdata/app/hadoop/sbin:/usr/java/jdk1.8.0_181/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/bigdata/.local/bin:/home/bigdata/bin) 20/12/13 17:55:20 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Logging initialized using configuration in jar:file:/home/bigdata/app/hive-1.1.0-cdh5.16.2/lib/hive-common-1.1.0-cdh5.16.2.jar!/hive-log4j.properties WARNING: Hive CLI is deprecated and migration to Beeline is recommended. hive>
set设置
1 2 3 4 5 6 7 8 9
[bigdata@hadoop001 ~]$ hivewhich: no hbase in (/home/bigdata/app/hive/bin:/home/bigdata/app/hadoop/bin:/home/bigdata/app/hadoop/sbin:/usr/java/jdk1.8.0_181/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/bigdata/.local/bin:/home/bigdata/bin) 20/12/16 18:57:56 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Logging initialized using configuration in jar:file:/home/bigdata/app/hive-1.1.0-cdh5.16.2/lib/hive-common-1.1.0-cdh5.16.2.jar!/hive-log4j.properties WARNING: Hive CLI is deprecated and migration to Beeline is recommended. hive (default)> set hive.cli.print.current.db; 【没有等号就是获取值】 hive.cli.print.current.db=true hive (default)> set hive.cli.print.current.db = false;【设置值】 hive> [bigdata@hadoop001 ~]$
优先级
hive-site.xml < hive --hiveconf < set a=b
hive命令
–hiveconf:设置参数
-i:定义UDF函数
exit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[bigdata@hadoop001 hive]$ hive -help which: no hbase in (/home/bigdata/app/hive/bin:/home/bigdata/app/hadoop/bin:/home/bigdata/app/hadoop/sbin:/usr/java/jdk1.8.0_181/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/bigdata/.local/bin:/home/bigdata/bin) 20/12/13 17:58:52 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable usage: hive -d,--define <key=value> Variable subsitution to apply to hive commands. e.g. -d A=B or --define A=B --database <databasename> Specify the database to use -e <quoted-query-string> SQL from command line -f <filename> SQL from files -H,--help Print help information --hiveconf <property=value> Use value for given property --hivevar <key=value> Variable subsitution to apply to hive commands. e.g. --hivevar A=B -i <filename> Initialization SQL file -S,--silent Silent mode in interactive shell -v,--verbose Verbose mode (echo executed SQL to the console)
-e:SQL语句[不用进入hive]
1 2 3 4 5 6 7 8 9
[bigdata@hadoop001 hive]$ hive -e "select * from db1.user" which: no hbase in (/home/bigdata/app/hive/bin:/home/bigdata/app/hadoop/bin:/home/bigdata/app/hadoop/sbin:/usr/java/jdk1.8.0_181/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/bigdata/.local/bin:/home/bigdata/bin) 20/12/13 18:03:57 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Logging initialized using configuration in jar:file:/home/bigdata/app/hive-1.1.0-cdh5.16.2/lib/hive-common-1.1.0-cdh5.16.2.jar!/hive-log4j.properties OK user.id user.name 1 xiaoming Time taken: 3.573 seconds, Fetched: 1 row(s)
-f:SQL文件
1 2 3 4 5 6 7 8 9 10 11
[bigdata@hadoop001 hive]$ vi hive.sql select * from db1.user [bigdata@hadoop001 hive]$ hive -f hive.sql which: no hbase in (/home/bigdata/app/hive/bin:/home/bigdata/app/hadoop/bin:/home/bigdata/app/hadoop/sbin:/usr/java/jdk1.8.0_181/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/bigdata/.local/bin:/home/bigdata/bin) 20/12/13 18:08:14 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Logging initialized using configuration in jar:file:/home/bigdata/app/hive-1.1.0-cdh5.16.2/lib/hive-common-1.1.0-cdh5.16.2.jar!/hive-log4j.properties OK user.id user.name 1 xiaoming Time taken: 3.384 seconds, Fetched: 1 row(s)
hive (default)> show databases; OK database_name db1 default Time taken: 0.236 seconds, Fetched: 2 row(s) hive (default)> create database db2; OK Time taken: 0.073 seconds hive (default)> show databases; OK database_name db1 db2 default Time taken: 0.018 seconds, Fetched: 3 row(s)
默认的default数据库:/user/hive/warehouse
自己创建的数据库:/user/hive/warehouse/db2.db
1 2 3 4 5 6
hive (default)> create database db2; FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Database db2 already exists 【数据库已经存在】 【加上IF NOT EXISTS】 hive (default)> create database if not exists db2; OK
加上语法location创建的数据库后缀无.db
1 2 3 4 5 6 7 8 9 10 11
hive (default)> create database if not exists db3 location '/user/hive/warehouse'/db3; 【hdfs的路径加上单引号】 OK Time taken: 0.016 seconds hive (default)> show databases; OK database_name db1 db2 db3 default Time taken: 0.016 seconds, Fetched: 4 row(s)
加上comment和dbproperties
1 2 3
hive (default)> CREATE DATABASE IF NOT EXISTS test2 COMMENT 'it is my hive database' WITH DBPROPERTIES('creator'='psyche','date'='2100-10-11'); OK Time taken: 0.027 seconds
这些元信息存在mysql中的数据库表中,dbs、database_params
查看数据库信息 :desc database extended + 数据库名
1 2 3 4
hive (default)> desc database extended test2; OK db_name comment location owner_name owner_type parameters test2 it is my hive database hdfs://hadoop001:9000/user/hive/warehouse/test2.db bigdata USER {date=2100-10-11, creator=psyche}
切换数据库:use + 数据库名
1 2 3 4
hive (default)> use test2; OK Time taken: 0.01 seconds hive (test2)>
ALTER 【生产上用得少】
语法:
ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, …); – (Note: SCHEMA added in Hive 0.14.0)
ALTER (DATABASE|SCHEMA) database_name SET OWNER [USER|ROLE] user_or_role; – (Note: Hive 0.13.0 and later; SCHEMA added in Hive 0.14.0)
ALTER (DATABASE|SCHEMA) database_name SET LOCATION hdfs_path; – (Note: Hive 2.2.1, 2.4.0 and later)
ALTER (DATABASE|SCHEMA) database_name SET MANAGEDLOCATION hdfs_path; – (Note: Hive 4.0.0 and later)
1
hive (default)> ALTER DATABASE test2 SET DBPROPERTIES('year'='2088');
hive (test2)> create table user(id int); OK Time taken: 0.07 seconds hive (test2)> drop database test2; FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database test2 is not empty. One or more tables exist.)
DDL表操作
Hive数据类型
int: 10 20 30
bigint: long
double:
float:
string
Hive分隔符
常用: \001 ^A
工作中列之间的分隔符:\t 空格
3|xiaohong|18
CREATE
语法:
CREATE [TEMPORARY][EXTERNAL] TABLE [IF NOT EXISTS][db_name.]table_name