1、数据库操做;正则表达式
1.1:查看全部的数据库: hive>show databases;sql
1.2:使用数据库default; hive>use default;数据库
1.3:查看数据库信息: hive>describe database default;测试
1.4:显示的展现当前使用的数据库:hive>set hive.cli.print.current.db=true;spa
1.5:Hive显示表中标题行: hive>set hive.cli.print.header=true;code
1.6:建立数据库命令: hive>create database test;orm
1.7:切换当前的数据库: hive>use test;xml
1.8:删除数据库: 删除数据库的时候,不容许删除有数据的数据库,若是数据库里面有数据则会报错。若是要忽略这些内容,则在后面增长CASCADE关键字,则忽略报错,删除数据库。Restrict关键字是默认状况,即若是有表存在,则不容许删除数据库。string
hive> drop database dbname [CASCADE|RESTRICT] (可选);
hive> drop database if exists dbname CASCADE;it
1.9:hive在HDFS上的默认存储路径: Hive的数据都是存储在HDFS上的,默认有一个根目录,在hive-site.xml中,由参数hive.metastore.warehouse.dir指定。默认值为/user/hive/warehouse.
2.0:Hive中的数据库在HDFS上的存储路径为:${hive.metastore.warehouse.dir}/dbname.db;
其中,hive.metastore.warehouse.dir的默认值是:/user/hive/warehouse。好比,数据库ethan的存储路径为:/user/hive/warehouse/ethan.db; ".db"是数据库的扩展名。
2.1:建立数据库时指定存储路径:hive>create database test location '/user/hive/mytest';
2、表操做;
2.1:查看当前DB有哪些表:hive>show tables in dbname;
也可使用正则表达式:hive>show tables like 'e*';
2.2:建立内部(Managed_table)表:create table tablename (); 适用场景:hive中间表,结果表,不需从外部上传数据的状况。
建立外部(External_table)表:create external table tablename();适用场景:源表,须要按期将外部数据映射到表中。
建表案例:
create external table test1 (id int, name string )comment '测试用表' partitioned by (day string) row format delimited fields terminated by ',' stored as textfile location 'hdfs://cdh5/tmp/**';
关键字解释:
External:表示该表为外部表,若是不指定External关键字,则表示内部表;
Comment:为表和列添加注释;
Partitioned by:表示该表为分区表,分区字段为day,类型为string;
Row format delimited:指定表的分隔符,一般后面要与一下关键字连用:
Fields terminated by ',':指定每行中字段分隔符为逗号;
Lines terminated by '\n':指定行分隔符;
Collection items terminated by ',':指定集合中元素之间的分隔符;
Map keys terminated by ':':指定数据中Map类型的key和value之间的分隔符;
Stored as:指定表在HDFS上的文件存储格式,可选的文件存储格式有:
Textfile:文本,默认值;
Sequencefile:二进制序列文件;
Rcfile:列式存储格式文件,Hive0.6之后开始支持;
Orc:列式存储格式文件,比Rcfile具备更高的压缩比和读写效率,Hive0.11之后开始支持。
Parquet:列式存储格式文件,Hive0.13之后开始支持。
Location:指定表在HDFS上的存储位置。
其它建表方式:
根据一个已存在的表建立另外一个表:
hive>create table test2 like test1; (只复制了表结构,不会复制内容);不须要执行mapreduce;
hive>create table test2 as select id,name from test1;(既复制表结构又复制表内容),需执行mapreduce;
2.3:获取表的建表语句:hive>show create table tablename;
2.4:获取表信息:hive>desc extended tablename;或者 hive>desc formatted tablename;
2.5:加载数据到表: hive>load data local inpath '/c/xxx/hivedata/data' overwrite into table tablename;
若是没有加overwrite,则会再copy一份数据,不会覆盖掉原来的数据。
2.6:删除表: hive>drop table tablename;
内部表删除,会连同hdfs存储的数据一同删除,而外部表删除,只会删除外部表的元数据信息,不会删除HDFS上的数据。
3、链接HiveServe;
3.1:使用beeline链接HiveServe; 启动beeline: bin/beeline --color=true --fastConnect=true;
链接到服务端: !connect jdbc:hive2//ctrl:10000;
例如:
[root@cheyo hive]# bin/beeline --color=true --fastConnect=true Beeline version 1.0.0 by Apache Hive beeline> !connect jdbc:hive2://ctrl:10000 scan complete in 13ms Connecting to jdbc:hive2://ctrl:10000 Enter username for jdbc:hive2://ctrl:10000: root Enter password for jdbc:hive2://ctrl:10000:
一步到位启动beeline直接链接:
bin/beeline --color=true --fastConnect=true -u jdbc:hive2://ctrl:10000 # 指定用户名登陆 bin/beeline --color=true --fastConnect=true -u jdbc:hive2://ctrl:10000 -n root -p ""