Hive命令行常用操作(数据库/表操作)

一、数据库操作;

1.1:查看所有的数据库: hive>show databases;

1.2:使用数据库default; hive>use default;

1.3:查看数据库信息: hive>describe database default;

1.4:显示的展示当前使用的数据库:hive>set hive.cli.print.current.db=true;

1.5:Hive显示表中标题行: hive>set hive.cli.print.header=true;

1.6:创建数据库命令: hive>create database test;

1.7:切换当前的数据库: hive>use test;

1.8:删除数据库: 删除数据库的时候,不允许删除有数据的数据库,如果数据库里面有数据则会报错。如果要忽略这些内容,则在后面增加CASCADE关键字,则忽略报错,删除数据库。Restrict关键字是默认情况,即如果有表存在,则不允许删除数据库。

hive> drop database dbname [CASCADE|RESTRICT] (可选); 
hive> drop database if exists dbname CASCADE;

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.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上的数据。

 

三、连接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 ""

 

转载于:https://my.oschina.net/u/3204727/blog/1608661

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值