-- 清空表中的数据,保留表结构
truncate table tmp_userid;
insert into tmp_userid values(''); -- 搜索库或表支持正则表达式
show tables 'sa*';
show tables in basename; -- 创建数据库时,默认位置是'/user/hive/warehouse/basename.db',可以创建表时指定物理位置
CREATE DATABASE BASENAME
LOCATION '/path/to/hdfs/';
-- 查看数据库信息,含hdfs信息
describe database ycappdata; -- 查看表结构和存储信息
show create table tablename;
describe extended ycappdata.tmp_userid_activity;
-- 给数据库增加附属信息
create database basename
with dbproperties ('creator'='wangbin','date'='2017-12-01');
-- 可以通过~查看
DESCRIBE DATABASE EXTENDED BASENAME; -- 递归删除数据库和数据库中的表
DROP DATABASE IF EXISTS BASENAME CASCADE; -- 查看表存储位置,并将数据put进hdfs
hdfs://data01.ycapp.yiche.com:8020/user/hive/warehouse/ycappdata.db/tmp_userid
hadoop fs -put /home/sa_cluster/wangbin/uid20180105.txt /user/hive/warehouse/ycappdata.db/tmp_userid/ -- 创建外部表,删除表并不会删除hdfs上的数据
create external table if not exists stocks(
*)
row format delimited fields terminated by ','
location '/path/to/hdfs/' -- 使用已有表创建外部表,管理表也可以这样复制
create external table if not exists stocks2
like stocks
location ''; -- 规定查询分区表必须指定分区以及相反的情况
set hive.mapred.mode=strict;
set hive.mapred.mode=nostrict; -- 查看表的分区,以及查看特定分区
show partitions tablename;
show partitions tablename partition(dt='2017-12-01'); -- 给表增加一个分区
alter table log_messages add partition(year=2017,month=12,day=2);
location '/path/to/hdfs'; -- 改变表的分区地址
alter table log_messages partition(year=2017,month=12,day=2);
set location '/newpath/to/hdfs'; -- 查看分区表的地址
describe extended ycappdata.sa_daydau_detail partition (ctl_dt='2017-12-01'); -- 表重命名
alter table log_messages rename to log_msgs;
-- 增加、修改、删除表分区
alter table log_messages drop if exists partition(year=2017,month=12,day=2);
-- 修改列信息
alter table log_messages change column hms1 hms2 int ;
alter table log_messages add column hms3 int;
-- 还可以修改表属性和列属性 -- 从一个表查询数据并插入到分区表中
insert overwrite table employees
partition(country='US',state='OR')
select * from tablename; -- 动态分区插入数据,hive 根据select 语句的最后两列来确定分区字段的值
insert overwrite table employees
partition(country,state)
select ...,se.cnty,se.st
from tablename se; set hive.exec.dynamic.partition=true;表示开启动态分区功能。还有一些其他的属性可以配置 -- 从表中导出数据
insert overwrite local directory '/dir/'
select * from ; -- 从表中查询集合数据类型,array[0],map['key'],struct.key
select subordinates[],deductions['key'],address.city from employees; -- 使用表生成函数
select explode(subordinates) as sub from employees;
-- 扫描一次全表,执行多次操作
from history
insert overwrite table1 select * where action='p1'
insert overwrite table2 select * where action='p2'
insert overwrite table3 select * where action='p3'; -- 创建视图
create view if not exists viewname(col1,col2)
as select * from tablename; -- 创建索引,仅对country建索引,一张表的索引数据存储在另外一张表中
create index employees_index
on table employees(country)
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
with deferred rebuild
in table employees_index_table; -- 显示索引
show formated index on employees; -- 删除索引
drop index employees_index on table employees; -- hive 数据分桶
create table weblog (user_id int,url string,source_ip string)
partition by (dt string)
cluster by (user_id) into 96 buckets; -- 设置hive为表分桶的默认reduce数,如果为false就需要手动指定buckets数,分桶时必须加cluster by
set hive.enforce.bucketing=true;
->
set mapred.reduce.tasks=96; -- 开启中间压缩,shuffle数据会减少
set hive.exec.compress.intermediate=true;
-- 开启输出结果压缩
set hive.exec.compress.output=true; -- 设置输出压缩格式为Gzip
set mapred.map.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; -- 旧文件访问频率很低,可以考虑进行归档,减少namenode的压力,缺点是查询效率会降低,也不会减少磁盘空间
-- 设置表为归档表,并将指定分区归档->.har,之后的语句可以进行反向操作,将数据从har文件提取出来重新放在hdfs
set hive.archive.enabled=true;
alter table hive_text archive partition(folder='docs');
alter table hive_text unarchive partition(folder='docs'); -- 使用表生成函数 select name,sub from employees
lateral view explode(subordinates) subView as sub ;

hadoop之hive基本操作的更多相关文章

  1. hive学习3(hive基本操作)

    hive基本操作 hive的数据类型 1)基本数据类型 TINYINT,SMALLINT,INT,BIGINT FLOAT/DOUBLE BOOLEAN STRING 2)复合类型 ARRAY:一组有 ...

  2. Hadoop之Hive详解

    1.什么是Hive hive是基于hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表 并提供类sql查询功能 2.为什么要用Hive 1.直接使用hadoop所面临的问题 人员学 ...

  3. 初识Hadoop、Hive

    2016.10.13 20:28 很久没有写随笔了,自打小宝出生后就没有写过新的文章.数次来到博客园,想开始新的学习历程,总是被各种琐事中断.一方面确实是最近的项目工作比较忙,各个集群频繁地上线加多版 ...

  4. 【hive】——Hive基本操作

    阅读本文章可以带着下面问题:1.与传统数据库对比,找出他们的区别2.熟练写出增删改查(面试必备) 创建表:hive> CREATE TABLE pokes (foo INT, bar STRIN ...

  5. 《Programming Hive》读书笔记(一)Hadoop和hive环境搭建

    <Programming Hive>读书笔记(一)Hadoop和Hive环境搭建             先把主要的技术和工具学好,才干更高效地思考和工作.   Chapter 1.Int ...

  6. [转]云计算之hadoop、hive、hue、oozie、sqoop、hbase、zookeeper环境搭建及配置文件

     云计算之hadoop.hive.hue.oozie.sqoop.hbase.zookeeper环境搭建及配置文件已经托管到githubhttps://github.com/sxyx2008/clou ...

  7. Hadoop之Hive篇

    想了解Hadoop整体结构及各框架角色建议飞入这篇文章,写的很好:http://www.open-open.com/lib/view/open1385685943484.html .以下文章是本人参考 ...

  8. 大数据技术生态圈形象比喻(Hadoop、Hive、Spark 关系)

    [摘要] 知乎上一篇很不错的科普文章,介绍大数据技术生态圈(Hadoop.Hive.Spark )的关系. 链接地址:https://www.zhihu.com/question/27974418 [ ...

  9. maven工程之pom模板(hadoop、hive、hbase)

    以下配置文件涵盖了hadoop.hive.hbase开发支持库的配置. 仅需针对maven工程pom.xml文件做相应更改就可以自动生成hadoop开发支持库. <properties>  ...

随机推荐

  1. 安卓手机可以连上wifi但无法上网的解决办法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 前晚我的安卓手机还可以连接wifi上网,昨晚显示已经连接,但是死活打不开网页.于是到网上查了下,发现要将原来的DHCP ...

  2. 我的MBTI职业性格测试

    背景 最近在看<程序员的思维训练--开发人之前能的九堂课>,其中讲到了 MBTI 职业性格测试的指导意义.记起来两年多以前在面试 ASES 的时候有做过这个测试,只可惜当时的测试结果在好几 ...

  3. C#颜色对照使用表

    这篇文章来来源于C# Color Table,这里是我翻译的中文版本,其中已经加上了我的一些理解和注释.翻译这篇文章的原因是我在写C#程序的时候发现,C#自带的颜色种类极多(详见下表),如果没有直观的 ...

  4. Method of Seamless Integration and Independent Evolution of Information-Centric Networking via Software Defined Networking

    A method of transferring data between a software defined network (SDN) and an information-centric ne ...

  5. uwp - RichEditBox 解决设置字体样式后滚动条自动回滚顶部的问题

    原文:uwp - RichEditBox 解决设置字体样式后滚动条自动回滚顶部的问题 开发中碰到一个问题,当RichEditBox输入的文本达到一定行数的时候,滚动条此时位于底部,改变文本样式(如字体 ...

  6. WPF透明窗体制作

    原文:WPF透明窗体制作 窗体的样式: <Grid Width="{Binding Width, ElementName=w}" Height="{Binding ...

  7. 神户制钢坑了500家企业 百年老店为何走上邪路?(企业经营再艰难,也不能降低产品质量,甚至偷工减料,同样适用于IT行业)

    神户制钢这颗烂萝卜,拔出它之后带出的泥越来越多.上周五社长川崎博也又开了记者会,再次道歉,而受到其数据造假影响的客户数量也从200家飙升到500家. 日本政府给神户制钢两周时间调查,还要在一个月内公布 ...

  8. NS2网络模拟(7)-homework03.tcl

    1: #NS2_有线部分\homework03.tcl 2: 3: #Create a simulator object 4: set ns [new Simulator] 5: 6: #Define ...

  9. WPF的Timer控件的使用

    原文:WPF的Timer控件的使用 通过System.Threaing.Timer控件来实现“初始加载页面时为DataGrid的模版列赋初始值” System.Threaing.Timer的用法: 步 ...

  10. Vhost Architecture

    在前面的文章中在介绍virtio机制中,能够看到在通常的应用中一般使用QEMU用户态程序来模拟I/O訪问,而Guest中的数据要通过Guest到Host Userspace的第一次拷贝,再经过Host ...