Hive知识点(持续更新中)

集群安装详细步骤见我的博客。

Hive是基于Hadoop的一个数据仓库处理工具,是一种数据库技术,可以定义数据库和表来分析结构化数据,

可以将结构化的数据映射为一张数据库表,提供简单的SQL的查询功能,

将SQL语句转化为MapReduce任务提交到Hadoop集群运行,十分适合数据仓库的统计分析。

Hive并不提供实时的查询和基于行级的数据更新操作,Hive在加载数据的过程中不会对数据进行任何修改,只是将数据

移动到Hive设定(hive-site.xml)的HDFS目录中,因此,Hive不支持对数据的改写和添加。

Hive的最佳使用场合就是大数据集的批处理操作。

=====================================================================

数据库的操作:

CREATE DATABASE yexin; 或者 CREATE SCHEMA yexin;                 --------------------创建数据库

扫描二维码关注公众号,回复: 2292995 查看本文章

DROP DATABASE IF EXISTS yexin;或者 DROP SCHEMA yexin--------------------------删除数据库

  1. --创建数据库

  2. create database if not exists sopdm

  3. comment ‘this is test database’

  4. with dbproperties(‘creator’=’gxw’,’date’=’2014-11-12’) --数据库键值对属性信息

  5. location ‘/my/preferred/directory’;

  6.  
  7.  
  8. --查看数据库的描述信息和文件目录位置路径信息

  9. describe database sopdm;

  10. --查看数据库的描述信息和文件目录位置路径信息(加上数据库键值对的属性信息)

  11. describe database extended sopdm;

  12.  
  13. --删除数据库

  14. drop database if exists sopdm;

  15. --级联删除数据库(当数据库还有表时,级联删除表后在删除数据库),默认是restrict

  16. drop database if exists sopdm cascade;

  17.  
  18. --修改数据库

  19. --只能修改数据库的键值对属性值。数据库名和数据库所在的目录位置不能修改

  20. alter database sopdm set dmproperties(‘edited-by’=’gaoxianwei’);

  21.  
  22. --创建表

  23. --其中tblproperties作用:按照键值对的格式为表增加额外的文档说明,也可用来表示数据库连接的必要的元数据信息

  24. --hive会自动增加二个表属性:last_modified_by(最后修改表的用户名),last_modified_time(最后一次修改的时间)

  25. create table if not exists sopdm.test1(name string comment ‘姓名’,salary float comment ‘薪水’)

  26. comment ‘这是一个测试的表’

  27. tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’)

  28. location ‘/user/hive/warehouse/sopdm.db/test1’

  29.  
  30. --查看和列举表的tblproperties属性信息

  31. show tblproperties table_name;

  32.  
  33.  
  34. --使用like在创建表的时候,拷贝表模式(而无需拷贝数据)

  35. create table if not exists sopdm.test2 like sopdm.test1;

  36.  
  37.  
  38. --查看表的详细结构信息(也可以显示表是管理表,还是外部表。还有分区信息)

  39. describe extended sopdm.test1;

  40. --使用formatted信息更多些,可读性更强

  41. describe formatted sopdm.test1;

  42.  
  43.  
  44. --创建外部表

  45. --删除表时,表的元数据会被删除掉,但是数据不会被删除

  46. --如果数据被多个工具(如pig等)共享,可以创建外部表

  47. create external table if not exists sopdm.test1(

  48. name string comment ‘姓名’,

  49. salary float comment ‘薪水’)

  50. comment ‘这是一个测试的表’

  51. tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’)

  52. location ‘/user/hive/warehouse/sopdm.db/test1’

  53.  
  54.  
  55. --分区表

  56. create table if not exists sopdm.test1(

  57. name string comment ‘姓名’,

  58. salary float comment ‘薪水’)

  59. comment ‘这是一个测试的表’

  60. partitioned by(country string,state string)

  61. STORED AS rcfile

  62. tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’)

  63. location ‘/user/hive/warehouse/sopdm.db/test1’

  64.  
  65.  
  66. --查看表中存在的所有分区

  67. show partitions table_name;

  68. --查看表中特定分区

  69. show partitions table_name partition(country=’US’);

  70.  
  71.  
  72. --可以在表载入数据的时候创建分区

  73. load data local inpath ‘${env:HOME/employees}’

  74. into table employees

  75. partition(country=’US’,state=’CA’);

  76.  
  77.  
  78. --删除表

  79. drop table if exists table_name;

  80.  
  81.  
  82. --修改表-表重命名

  83. alter table old_table_name rename to new_table_name;

  84.  
  85. --增加分区

  86. alter table table_name add if not exists partition(year=2011,month=1,day=1)

  87. location ‘/logs/2011/01/01’;

  88.  
  89. --修改分区存储路径

  90. alter table table_name partition(year=2011,month=1,day=2)

  91. set location ‘/logs/2011/01/02’;

  92.  
  93. --删除某个分区

  94. alter table table_name drop if exists partition(year=2011,month=1,day=2);

  95.  
  96. --修改列信息

  97. alter table table_name

  98. change column old_name new_name int

  99. comment ‘this is comment’

  100. after severity; --字段移到severity字段之后(移动到第一个位置,使用first关键字)

  101.  
  102. --增加列

  103. alter table table_name add columns(app_name string comment ‘application name’);

  104.  
  105. --删除或者替换列

  106. alter table table_name replace columns(hms int comment ‘hhh’);

  107.  
  108. --修改表属性

  109. alter table table_name set tblproperties(‘notes’=’this is a notes’);

  110.  
  111. --修改存储属性

  112. alter table table_name partition(year=2011,month=1,day=1) set fileformat sequencefile;

  113.  
  114. --指定新的SerDe,并指定SerDe属性

  115. alter table table_name

  116. set serde “com.example.JSONSerDe”

  117. with serdeproperties(‘prop1’=‘value1’, ‘prop2’=‘value2’);

  118.  
  119. --增加执行“钩子”——当表中存储的文在hive之外被修改了,就会触发钩子的执行

  120. alter table table_name touch partition(year=2012,month=1,day=1);

  121.  
  122. --将分区内的文件打成hadoop压缩包文件,只会降低文件系统中的文件数,减轻NameNode的压力,而不会减少任何的存储空间

  123. --使用unarchive替换archive起到反向操作

  124. alter table table_name archive partition(year=2012,month=1,day=1);

  125.  
  126. --防止分区被删除和被查询(使用enable替代disable可以起到反向的操作目的)

  127. alter table table_name partition(year=2012,month=1,day=1) disable no_drop;

  128. alter table table_name partition(year=2012,month=1,day=1) disable offline;

  129.  
  130.  
  131. --向管理表中装载数据

  132. -- inpath为一个目录,而且这个路径下不可以包含任何文件夹

  133. load data local inpath ‘${env:HOME}/table_name’

  134. overwrite into table table_name

  135. partition(country=’US’);

  136.  
  137.  
  138. --通过查询语句向表中插入数据

  139. --overwrite是覆盖,into是追加

  140. insert overwrite table table_name

  141. partition(country=’US’)

  142. select * from table_name2 tn where tn.cnty=’US’

  143.  
  144.  
  145. --高效方式-查询语句插入多个分区

  146. from table_name2 tn

  147. insert overwrite table table_name

  148. partition(country=’US’,state=’OR’)

  149. select * where tn.cnty=’US’ and tn.st=’OR’

  150. insert overwrite table table_name

  151. partition(country=’US’,state=’CA’)

  152. select * where tn.cnty=’US’ and tn.st=’CA’

  153.  
  154.  
  155. --动态插入分区

  156. --hive根据select语句最后2列确定分区字段country和state的值(根据位置)

  157. insert overwrite table table_name

  158. partition(country,state)

  159. select …,se.cnty,se.st

  160. from employees se;

  161.  
  162. --动态和静态分区结合

  163. --country为静态分区,state为动态分区(静态分区必须在动态分区之前)

  164. insert overwrite table table_name

  165. partition(country=‘US’,state)

  166. select …,se.cnty,se.st

  167. from employees se

  168. where se.cnty=’US’;

  169.  
  170.  
  171. --单个查询语句中创建表并加载数据

  172. create table table_name1

  173. as select name,salary,address from table_name2 where state=’CA’;

  174.  
  175.  
  176. --导出数据——拷贝文件

  177. --如果数据文件恰好是用户需要的格式,那么只需要简单的拷贝文件或文件夹就可以。

  178. hadoop fs –cp source_path target_path

  179.  
  180.  
  181. --导出数据

  182. insert overwrite local directory ‘/tmp/employees’

  183. select name,salary,address from employees se where se.state=’CA’

  184.  
  185. --导出数据到多个输出文件夹

  186. from employees se

  187. insert overwrite local directory ‘/tmp/or_employees’

  188. select * se where se.cty=’US’ and se.st=’OR’

  189. insert overwrite local directory ‘/tmp/ca_employees’

  190. select * se where se.cty=’US’ and se.st=’CA’

创建表:

create table if not exists yexin.yexin(name string comment 'name',salary float comment 'money') comment 'this is test table';

网页中可以看到数据库表的成功:

利用文件导入数据到表:

txt文件

上传到hdfs的/hive/input,再导入:

LOAD DATA INPATH '/hive/input/hive.txt' OVERWRITE INTO TABLE yexin;

如果是本文件就是:LOAD DATA LOCAL INPATH '/hive/input/hive.txt' OVERWRITE INTO TABLE yexin;

注意:导入之后/hive/input/下就没有hive.txt文件了,因为上传到/hive/warehouse/yexin.db/了:

猜你喜欢

转载自blog.csdn.net/qq_25948717/article/details/81062994