美文网首页
hive 操作

hive 操作

作者: Zero兴 | 来源:发表于2018-09-20 18:04 被阅读0次

第一节:

hive作为大数据中重要组成的一部分,它是基于Hadoop的映射数据库表,其提供的Hue可以将简单的HQL语句转化为MapReduce任务执行,大大简化了学习成本;HQL大体上和SQL相同,只是有少部分sql语句在hql上兼容不好,具体出现的错误日后再行补上。

基础:

创建数据库:Create database  test;

创建数据表:Create table if not exists test(id int comment ‘序列号', name string comment ‘姓名',  age string comment ‘年龄', sex string comment '性别'….) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘,’  #  设置列分割符为’,’,为后面导入hdfs的csv文件作准备;  

    3.修改数据表结构: alter table test addcolumns(address  string); # 增加表列

修改表名称:alter table test rename to tt ;

修改列:ALTER TABLE test change name new_name string; 

删除列 : alter table test drop name;

删除表:drop table test ;

去除导入数据的第一行或最后一行:create table test(id int, name string) TBLPROPERTIES('skip.header.line.count’=‘*’/ ‘skip.footer.line.count'=‘*') # 去除首/尾 *行;

上传文件到HDFS:

    1. 将本地数据push到hdfs, hdfs dfs -put localfile_path remote_filepath;

2. 在hue上执行LOAD DATA INPATH ‘remote_filepath’[overwrite]INTO TABLE text; # 将hdfs的文件导入到hive中;

 注:如果导入数据出现Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive, 可能是hdfs文件权限问题; 解决方案:hdfs dfs -chomd 777  filename (文件在hdfs的路径);load data local inpath指的直接从本地文件中加载,先将本地文件拷贝到hdfs,然后再使用;overwrite 是将原先表的数据进行覆盖   3. select count(*) from test;  # 查询记录,验证数据是否导入成功;

    4.Select * from test limit 10;

5.SELECT DISTINCT('columns_name') from test;

在有了全量表之后,需要将全量表根据业务拆分为多张子表的需求时,可以根据字表的字段从全表中的数据选择性导入相应的数据,例如:

create table if not exists test1(id int comment ‘ID号’, name string comment '姓名’);

Insert overwrite table test1 select id, name from test; <一条一条数据插入效率很慢>

Create table if not exists test2 as select name from test; <复制非分区表>

如果是分区表,先要复制表结构: create table test3 like test; 之后将分区数据拷贝到对应的目录下:dfs -cp -f /user/hive/warehouse/test/* /user/hive/warehouse/test3/; 最后同步信息 msck repair  table test3;

上面所讲的所谓的内部表,还有一种叫做外部表,例如:

create external table if not exists ex_test(id int, name string, sex string) partitioned by(birth string) row format delimited fields terminated by ‘\t’ location ‘file_path’<文件在hdfs路径>

注:如果没有指定location,则会在/usr/hive/warehouse/建立一个表目录,之后将hdfs的数据移动到该目录下;

外部表和内部表的区别:内部表是将hdfs移动,当内部表删除之后,对应的数据也会被删除,而外部表则是删除之后,其数据还在指定的目录中;

Hive数据存储方式:1.Txtfile<默认,不压缩>; 2.sequencefile<二进制文件支持,压缩类型None,Record,Block>; 3.RcFile(直接对数据进行分开,保证同一个record在一个块上,加载开销大,但是压缩比大,查询响应快,推荐使用);4.Orcfile<后出来的>

第二节:

hive的分区操作,因为hive在大规模的数据中进行查询相关操作,适当的分区有助于提升查询数据的效率;

例如创建一个test表:

Create table if not exists test(id int, name string) partitioned by( sex string) row format delimited fields terminated by ‘,’ ;

根据性别sex进行分区,分区类似于将数据放在不同的文件夹下,因而在hive进行数据查找时可以根据where条件分区查找而不用进行全表扫描,从而提高效率;但也不是分区越多越好;由于分区对应的是hdfs上一个个小文件,当小文件过多,会导致mapper任务越多,从而导致初始化任务消耗资源过多,影响性能;而且hive的创建文件数有限制,太多分区可能超出限制导致出错;总之要根据实际的需求创建分区;

添加一个或多个分区:alter table test add partition(brith=’1992’), partition(birth=‘1995’),..;

删除分区:alter table test drop partition(birth=’1992’);

查看分区:show partitions test;

重命名分区: alter table test partition(birth=’1992’) rename to partition(birth=‘1995’);

同步hdfs的分区信息:msck repairtable test;

分桶表 == MapReduce分区:

将表按照字段列进行划分并排序;

Create table if not exists test(id int, name string, sex string) clustered by(id) sorted by (name string) into 4 buckets row format delimited fields terminated by ‘\t’ stored as textfile;

插入数据:

insert table 桶表名 select * from table **;

常用的内置方法:

Case when ‘’ then ‘’ else '' end ;

Select id, case when num>100 then ‘0-99’ when num<1000 then ‘100-1000’ else ‘>1000’ end from table_name ;

等同于: select id from table_name  where num<100 ; && select id from table_name  where num>100 and num<1000; && select id from table_name  where num>1000;

If (单个条件判断):

SELECT title, if(num>100,0,1) as n,num from car_back LIMIT 10;

相关文章

  • hive基础语法

    目录 Hive安装和启动 Hive表操作-分区表 Hive表操作-复杂类型操作 Hive 查询语句 Zepplin...

  • 数据查询-Hive基础

    outline 什么是Hive 为什么需要Hive Hive的架构 Hive的常用操作 什么是Hive Hive由...

  • Hive基本操作

    在所有操作前先启动hdfs、yarn、historyserver。 hive操作 启动hivebin/hive 创...

  • hive

    hive官网:https://hive.apache.org/ hive操作指南:https://cwiki.ap...

  • 记录一次 hadoop+tornado 简单实践(二) -- h

    hive 数据库操作 hive 支持大部分的 sql 语法,因此熟悉 sql 可以很方便的上手 hive 操作 h...

  • HIVE

    基本操作 hive进入hive模式 exit; 退出hive模式 展示正则匹配表名hive> use ad_s...

  • Hive编程指南笔记整理

    hive编程指南阅读笔记 1.基础操作 1.1.启动hive HIVE_HOME$ bin/hive 1.2.1....

  • Hive sql常见操作

    基本sql操作 hive表操作 分区操作 Hive内置函数 (1)数学函数 常用的数学函数都有:round、flo...

  • 002 Impala | 安装要求

    操作系统 linux only Hive Metastore Impala能够交互操作hive中存储的数据,使用相...

  • 使用Python操作Hive

    Python操作Hive 注意:想要使用hive,必须要有一个可用的hive集群,同时为了保证可用使用API操作h...

网友评论

      本文标题:hive 操作

      本文链接:https://www.haomeiwen.com/subject/cfinnftx.html