hive简单操作

Thrift是一种接口描述语言和二进制通讯协议,它被用来定义和创建跨语言的服务。它被当作一个远程过程调用(RPC)框架来使用,是由Facebook为“大规模跨语言服务开发”而开发的。
hive读时模式

1.传统的关系型数据库在进行数据加载时,必须验证数据格式是否符合表字段定义,如果不符合,数据将无法插入
   至数据库表中。这种模式称为“写时模式”。
2.hive中,数据加载过程采用“读时模式”,加载数据时不进行校验,读取数据时如果不合法的是NULL

Hive DDL数据定义语言

1.创建数据库
   hive>create database myhive;
   hive>create database if not exists myhive;
   hive>show databases;
   hive>show databases like '*t*';    //使用正则匹配
  说明:hive为创建的数据库生成了相对应的目录(*.db),目录在{hive.metastore.warehouse.dir}属性下,
        同时,数据库中的表将以目录中的子目录进行存储;default默认数据库除外!

  a.自定义修改数据库存放位置,需单独指定(*.db)目录
     hive> hadoop fs -mkdir -p /user/hive/test
     hive> hadoop fs -lsr /user

     自定义数据库路径需要指定数据库文件名 eg:/tesTabA 或 /tesTabA.db
	 hive (default)> create database tesTabA
			       > comment 'holds all finacial tables'
			       > location '/user/hive/test/tesTabA.db';   
	 OK
  
    创建文件夹.db
    hive> create database myhive3 location '/user/hive/myhive3.db';
	hive> dfs -lsr /user;
	lsr: DEPRECATED: Please use 'ls -R' instead.
	drwxrwxrwx   - hyxy supergroup          0 2018-08-08 09:15 /user/hive
	drwxrwxrwx   - hyxy supergroup          0 2018-08-08 09:15 /user/hive/myhive3.db
	drwxrwxrwx   - hyxy supergroup          0 2018-08-08 09:15 /user/hive/myhive3.db/tt3
	drwxrwxrwx   - hyxy supergroup          0 2018-08-08 09:13 /user/hive/tt
	drwxrwxrwx   - hyxy supergroup          0 2018-08-08 08:58 /user/hive/warehouse
	
	 


  b.DESC DATABASES语句可以显示出这个数据库所在的文件目录位置路径.:
     hive>create database if not exists myhive4 comment '创建hive测试库';
	 hive>describe database myhive4 ;
		OK
	myhive4	创建hive测试库	hdfs://mycluster/user/hive/warehouse/myhive4.db	hyxy	USER	
	Time taken: 0.069 seconds, Fetched: 1 row(s) 
     1)在数据库下创建表   2)查询数据

  c.对数据库添加属性信息:
    hive>create database myhive5 with dbproperties ('name'='zhangsan','data'='2020-12-22');
	hive>desc database extended myhive5;

  d.使用数据库:
    hive>use myhive1;

  e.删除数据库:
    hive>drop database if exists myhive5; 
    FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database myhive5 is not empty. One or more tables exist.)

	如果数据库不为空,可以采用 cascade 命令,强制删除(cascade级联)
	hive>drop database if exists myhive3 cascade;
	说明:cascade表示级联关系;restrict表示限制约束(默认值);


2.修改数据库:
  除数据库的属性以外,其他信息均不能修改:
    hive>alter database myhive5 set dbproperties ('name'='hyxy','data'='2019-07-14');

	hive>desc database extended myhive5;
    
3.创建表
   a.创建表:
       hive>create table student(sid int comment 'xuehao',name string comment 'mingzi') comment 'student table' tblproperties ('name'='zhangsan','data'='2018-08-08') ;
       hive>create table hive.student(sid int comment 'xuehao',name string comment 'mingzi')  comment 'student table' tblproperties ('name'='zhangsan','data'='2018-08-08') ;
       注意:增加tblproperties属性后, 插入数据会报错
   b.查看表描述:
       hive>desc hive.student;
       hive>desc extended student;

   c.拷贝表模型,创建新的表(数据不拷贝)
        hive> create table employee2 like employee;      
       
	    hive> select * from employee; 
       
		查询数据插入到表中
		hive> insert into employee2 select * from employee; 

		union查询
		hive>select eid from employee union select eid from  employee2;

		union all查询
		hive>select id from student union all select id from  student2;


4.管理表
   默认创建的表均为管理表,表达形式{MySQL:hive:TBLS}表的TBL_TYPE字段显示为MANAGER_TABLE;
   一般也把管理表称为“内部表”;
   内部表特性: hdfs下 将数据move(**移动**)至{hive.metastore.warehouse.dir}目录相关database下;
                local下 将数据copy(**复制**)至{hive.metastore.warehouse.dir}目录相关database下;


5.外部表
    create external table if not exists stocks (
    > exchange1 string,
    > symbol string,
    > ymd string,
    > price_open float,
    > price_high float,
    > price_low float,
    > price_close float,
    > volume int,
    > price_adj_close float)
    > ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
    > LOCATION '/data/stocks'; 

   $>hadoop fs -mkdir /data
   $>hadoop fs -mkdir -p /data/stocks
   $>hadoop fs -put /mnt/hgfs/2.安装环境/data/stocks/stocks.csv /data/stocks
   hive>create external table if not exists stocks (exchange1 string,symbol string,ymd string,price_open float,price_high float,price_low float,price_close float,volume int,price_adj_close float) row format delimited fields terminated by ',' location '/data/stocks';
   hive>select count(*) from stocks ;
     执行MR Job!!

   删除外部表
   drop table stocks 
   1)查看mysql元数据,无记录 
   2)hfds下查看外部表 ,原数据不会删除
	$> hadoop fs -lsr /
	lsr: DEPRECATED: Please use 'ls -R' instead. 
	drwxr-xr-x   - hyxy supergroup          0 2019-05-30 10:54 /data/stocks
	-rw-r--r--   2 hyxy supergroup       1095 2019-05-30 10:54 /data/stocks/stocks.csv 
	 
	 
	 
	 
   关于创建表的知识点(复制表结构而不复制数据)   编程指南P57
   create external table if not exists employees3
   like employees
   location '/path/to/data';

   1)如果create时不加external,那么创建的表employees3由源表employees来决定
     例如:源表是内部表,那么创建的新表也是内部表,如果源表是外部表,那么创建的新表也是外部表
   2)如果create时加external,代表创建的表是外部表,不管源表是内部表还是外部表,创建的表都是外部表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值