简单的word-count操作:

[root@master test-map]# head -10 The_Man_of_Property.txt    #先看看数据
Preface
“The Forsyte Saga” was the title originally destined for that part of it which is called “The Man of Property”; and to adopt it for the collected chronicles of the Forsyte family has indulged the Forsytean tenacity that is in all of us. The word Saga might be objected to on the ground that it connotes the heroic and that there is little heroism in these pages. But it is used with a suitable irony; and, after all, this long tale, though it may deal with folk in frock coats, furbelows, and a gilt-edged period, is not devoid of the essential heat of conflict. Discounting for the gigantic stature and blood-thirstiness of old days, as they have come down to us in fairy-tale and legend, the folk of the old Sagas were Forsytes, assuredly, in their possessive instincts, and as little proof against the inroads of beauty and passion as Swithin, Soames, or even Young Jolyon. And if hero

hive> create table article2(sentence string) row format delimited fields terminated by '\n';   --直接用行号为分隔符导进来。

hive> load data inpath '/The_Man_of_Property.txt' overwrite into table article2;   --这个不加local的情况下是在本地导入的,即从hsfs中的/下导入,但是导入进去之后The_Man_of_Property.txt文件会消失。

hive> select  explode(split(sentence,' ')) from article2 limit 10;   --这个是用explode函数将sentence列,用空格分开。
OK
Preface
“The
Forsyte

hive> select word ,count(*) from (select explode(split(sentence,' ')) as word from article2 ) t group by word limit 10;
Query ID = root_20180504132418_41a4e570-f3d5-4dd8-8a86-3c93256c4063
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks not specified. Estimated from input data size: 1
In order to change the average load for a reducer (in bytes):
  set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
  set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
  set mapreduce.job.reduces=<number>
Starting Job = job_1525332220382_0102, Tracking URL = http://master:8088/proxy/application_1525332220382_0102/
Kill Command = /usr/local/src/hadoop-2.6.1/bin/hadoop job  -kill job_1525332220382_0102
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
2018-05-04 13:24:24,802 Stage-1 map = 0%,  reduce = 0%
2018-05-04 13:24:32,114 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 3.91 sec
2018-05-04 13:24:40,501 Stage-1 map = 100%,  reduce = 100%, Cumulative CPU 6.59 sec
MapReduce Total cumulative CPU time: 6 seconds 590 msec
Ended Job = job_1525332220382_0102
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1  Reduce: 1   Cumulative CPU: 6.59 sec   HDFS Read: 640084 HDFS Write: 116 SUCCESS
Total MapReduce CPU Time Spent: 6 seconds 590 msec
OK
    35
(Baynes    1
(Dartie    1
(Dartie’s    1
(Down-by-the-starn)    2
(Down-by-the-starn),    1
(He    1
(I    1
(James)    1
(L500)    1
Time taken: 23.514 seconds, Fetched: 10 row(s)

分桶的操作:

hive> set hive.enforce.bucketing = true; --不设置的话可能不生效。
hive> create table user_bucket(id int) clustered by (id) into 4 buckets; --人为的分成的4个reduce,即四个桶,使用id分桶。
hive> insert overwrite table user_bucket select cast(user_id as int) from badou.udata;

采样:

• 查看sampling数据:• 查看sampling数据:
– hive> select * from student tablesample(bucket 1 out of 32 on id); 2bucket=1y 32bucket=16y
– tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y)
– y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了64份,当y=32
时,抽取(64/32=)2个bucket的数据,当y=128时,抽取(64/128=)1/2个bucket的数据。x表示从哪个bucket开始抽
取。例如,table总bucket数为32,tablesample(bucket 1 out of 16),表示总共抽取(32/16=)2个bucket的数据,
分3第三个bucket,和第(3+16)19个bucket的数据。这个3是随机的,只要间隔是16就好

采样的例子:

之后我们来查看数据量:

采样的数据量

总数据量:

之后对比一下数量:

结果差不多一致。

创建内外部表:

然后将数据插入表中

查看hdfs上的路径,

之后删除内部表:

在HIve中查看表

再去hdfs上查看路径,发现没有这个路径了。

之后删除外部表:

表的内容消失了

之后查看hdfs上的路径:

发现外部表的hdfs上的数据并没消失,之后可以导入进去就可以重新,创建了。所以外部表比较安全。

分区表:

内部表:

hive>

create table partition_test(
order_id string,                                      
user_id string,                                      
eval_set string,                                      
order_number string,  
order_hourofday string,                                      
order_since_last string
)partitioned by(order_week string)
row format delimited fields terminated by '\t';

在hdfs上查找一下:

hive>insert overwrite table partition_test partition (order_week='3')
select order_id,user_id,eval_set,order_number,order_since_last,order_since_last from orders where order_week='3' ;

将order_week=1,2,3分别作为一个分区:然后查看这个表;(这里只截取一部分)

然后器hdfs上查看路径,分成了三个区;

在大量数据的场景中,即(表中的数据以及每个分区的个数都非常大的话),一个高度的安全建议的就是将HIve设置为“strict(严格模式)”,这样如果对分区表进行查询而WHERE子句没有加分区过滤的话,将会禁止提交这个任务。

hive> set hive.mapred.mode=strict; --设置为严格模式;

hive> set hive.mapred.mode=nostrict --设置为非严格模式;

可以使用如下命令查看所有的分区表;

查看指定的分区键的分区;

使用如下命令查看分区表的信息:

hive>alter table partition_test add partition (order_week=0) location '/usr/hive2/warehouse/badou.db/partition_test/order_week=0'; --添加分区的操作。

外部表:

和内部表差不多,但外部表的优势就是删除表不会删除hdfs上的数据,而且可以共享数据。

hive,分桶,内外部表,分区的更多相关文章

  1. 二 Hive分桶

    二.Hive分桶 1.创建分桶表 create table t_buck (id string ,name string) clustered by (id) //根据id分桶 sorted by ( ...

  2. hive分桶 与保存数据的方式

    创建分桶的表 create table t_buck(id int ,name string) clustered by (id ) sorted by (id) into 4 buckets  ; ...

  3. hive 分桶及抽样调查

    1.分桶的概述 分区提供了一个隔离数据和优化查询的遍历方式.不是所有的数据集都可形成合力的分区 对于一张表或者分区,hive可以进一步组织成桶,也就是更为细粒度的数据范围 分区针对的是数据的存储路径( ...

  4. hive分桶表bucketed table分桶字段选择与个数确定

    为什么分桶 (1)获得更高的查询处理效率.桶为表加上了额外的结构,Hive 在处理有些查询时能利用这个结构.具体而言,连接两个在(包含连接列的)相同列上划分了桶的表,可以使用 Map 端连接 (Map ...

  5. Hive分桶

    1.简介 分桶表是对列值取哈希值的方式将不同数据放到不同文件中进行存储.对于hive中每一个表,分区都可以进一步进行分桶.由列的哈希值除以桶的个数来决定数据划分到哪个桶里. 2.适用场景 1.数据抽样 ...

  6. HIVE—索引、分区和分桶的区别

    一.索引 简介 Hive支持索引,但是Hive的索引与关系型数据库中的索引并不相同,比如,Hive不支持主键或者外键. Hive索引可以建立在表中的某些列上,以提升一些操作的效率,例如减少MapRed ...

  7. hive -- 分区,分桶(创建,修改,删除)

    hive -- 分区,分桶(创建,修改,删除) 分区: 静态创建分区: 1. 数据: john doe 10000.0 mary smith 8000.0 todd jones 7000.0 boss ...

  8. Hive动态分区和分桶(八)

    Hive动态分区和分桶 1.Hive动态分区 1.hive的动态分区介绍 ​ hive的静态分区需要用户在插入数据的时候必须手动指定hive的分区字段值,但是这样的话会导致用户的操作复杂度提高,而且在 ...

  9. Hive SQL之分区表与分桶表

    Hive sql是Hive 用户使用Hive的主要工具.Hive SQL是类似于ANSI SQL标准的SQL语言,但是两者有不完全相同.Hive SQL和Mysql的SQL方言最为接近,但是两者之间也 ...

  10. 【HIVE】(2)分区表、二级分区、动态分区、分桶、抽样

    分区表: 建表语句中添加:partitioned by (col1 string, col2 string) create table emp_pt(id int, name string, job ...

随机推荐

  1. 新建本地仓库,同步远程仓场景,出现git branch --set-upstream-to=origin/master master 解决方法

    1.本地创建一个本地仓库 2.关联远程端:git remote add origin git@github.com:用户名/远程库名.git3.同步远程仓库到本地git pull这个时候会报错If y ...

  2. 学习笔记之DevOps

    DevOps - Wikipedia https://en.wikipedia.org/wiki/DevOps DevOps (a clipped compound of "developm ...

  3. 网站优化URL需要注意的几个细节

     原文地址:http://www.douban.com/note/474016612/   一个好的URL结构无论是对搜索引擎,还是用户,都具有非常重要的作用,那么什么样的URL才是既能面向搜索引擎, ...

  4. 关于oracle的sqlplus显示不完全的修改方法

    这样的显示看起来很痛苦 需要换行的时候没有进行换行,不需要换行的时候却进行了换行 参考的博客地址 https://blog.csdn.net/pan_tian/article/details/8059 ...

  5. centos7部署openvpn-2.4.6

    一.环境说明 返回主机的IP地址 # ip a | grep "scope global" | awk -F'[ /]+' '{print $3}' | head -1 [root ...

  6. [UE4]位移和形变 Render Transform

      任何UI控件都有Render Transform属性. 一.Transform,对应游戏场景中的Transform 1.Translation:位置,平移.对应游戏场景的Transform中的Lo ...

  7. [UE4]RetainerBox,控制UI更新频率,把渲染后的UI当成Texture

    RetainerBox是一个容器,只会影响其容器内的UI,RetainerBox的作用: 一.控制UI更新频率(可能是为有优化性能) 1.在UserWidget中添加Retainer Box容器,并在 ...

  8. xdcms_3.0.1 | 代码审计

    这周的审计任务,这次审计 xdcms . 下面就开始审计之旅.                                                                     ...

  9. Redis进阶实践之十四 Redis-cli命令行工具使用详解

    转载来源:http://www.cnblogs.com/PatrickLiu/p/8508975.html 一.介绍 redis学了有一段时间了,以前都是看视频,看教程,很少看官方的东西.现在redi ...

  10. maven入门安装及HelloWorld实现

    一.安装maven 1.下载    https://maven.apache.org/download.cgi     官网进行下载 2.安装 2.1  解压 本人在D盘建立一个maven文件夹,然后 ...