hive增量抽取方案

一、使用sqoop从mysql中抽取数据到hive,查看sqoop官方文档,有如下两种方案:
7.2.9. Incremental Imports
Sqoop provides an incremental import mode which can be used to retrieve only rows newer than some previously-imported set of rows.

The following arguments control incremental imports:

Table 5. Incremental import arguments:

Argument    Description
--check-column (col)    Specifies the column to be examined when determining which rows to import. (the column should not be of type CHAR/NCHAR/VARCHAR/VARNCHAR/ LONGVARCHAR/LONGNVARCHAR)
--incremental (mode)    Specifies how Sqoop determines which rows are new. Legal values for mode include append and lastmodified.
--last-value (value)    Specifies the maximum value of the check column from the previous import.

Sqoop supports two types of incremental imports: append and lastmodified. You can use the --incremental argument to specify the type of incremental import to perform.

You should specify append mode when importing a table where new rows are continually being added with increasing row id values. You specify the column containing the row’s id with --check-column. Sqoop imports rows where the check column has a value greater than the one specified with --last-value.

An alternate table update strategy supported by Sqoop is called lastmodified mode. You should use this when rows of the source table may be updated, and each such update will set the value of a last-modified column to the current timestamp. Rows where the check column holds a timestamp more recent than the timestamp specified with --last-value are imported.

At the end of an incremental import, the value which should be specified as --last-value for a subsequent import is printed to the screen. When running a subsequent import, you should specify --last-value in this way to ensure you import only the new or updated data. This is handled automatically by creating an incremental import as a saved job, which is the preferred mechanism for performing a recurring incremental import. See the section on saved jobs later in this document for more information.

示例:

简单说,sqoop支持两种增量MySql导入到hive的模式,
一种是 append,即通过指定一个递增的列,比如:
--incremental append --check-column id --last-value 0
导入id>0的数
另一种是可以根据时间戳的模式叫 lastmodified ,比如:
--incremental lastmodified --check-column createTime --last-value '2012-02-01 11:0:00'
就是只导入createTime 比'2012-02-01 11:0:00'更大的数据。

--check-column (col)        --检查的列
--incremental (mode)        --所选模式,append或者lastmodified 
--last-value (value)          -- 最后一次的值
```
本次采用的是时间戳方案,每天导入全量数据,在hive中抽取最新的数据



#####二、hive增量SQL实现。

数据:data.txt
```
1,mary,18,2017-06-26 10:00:00
2,lucy,29,2017-06-26 10:00:00
3,jack,18,2017-06-26 10:00:00
4,nick,25,2017-06-26 10:00:00
4,nick,18,2017-06-27 10:00:00
5,tom,26,2017-06-26 10:00:00
5,tom,26,2017-06-27 12:00:00
```
1. 建表语句:
```
create table mytable(id int,name string,age int,createTime string) partitioned by (dt string) row format delimited fields terminated by ',';
```

2. 导入数据:
```
load data local inpath '/home/ubuntu/data.txt' into table mytable partition(dt='20170626');
```

3. 查看数据
```
hive> select * from mytable where dt='20170626';
OK
1   mary    18  2017-06-26 10:00:00 20170626
2   lucy    29  2017-06-26 10:00:00 20170626
3   jack    18  2017-06-26 10:00:00 20170626
4   nick    25  2017-06-26 10:00:00 20170626
4   nick    18  2017-06-27 10:00:00 20170626
5   tom 26  2017-06-26 10:00:00 20170626
5   tom 26  2017-06-27 12:00:00 20170626
Time taken: 0.364 seconds, Fetched: 7 row(s)
hive> 
```
4. 我们发现20170626中有27号的增量数据,所以应该将数据更新到20160627分区,保留最新的数据
(注:hive中删除分区方法:alter table mytable drop partition(dt='20170627'))
#####查询语句如下:
```
SELECT id, name, age, createTime
FROM (SELECT id, name, age, createTime, row_number() OVER (PARTITION BY id ORDER BY createTime DESC) AS rn
    FROM mytable
    ) t
WHERE t.rn = 1;
```
其中:
```
select id,name,age,createTime,row_number() over (partition by id order by createTime DESC) AS rn from mytable
```
使用的hive的窗口函数row_number(),该函数作用是将原表按partition后面的字段分区后,并且按照createTime字段降序排列后,对分组内部的行记录进行标记行号,分别从1-n顺序标号,
该句的查询结果如下:
```
Total MapReduce CPU Time Spent: 2 seconds 250 msec
OK
1   mary    18  2017-06-26 10:00:00 1
2   lucy    29  2017-06-26 10:00:00 1
3   jack    18  2017-06-26 10:00:00 1
4   nick    18  2017-06-27 10:00:00 1
4   nick    25  2017-06-26 10:00:00 2
5   tom 26  2017-06-27 12:00:00 1
5   tom 26  2017-06-26 10:00:00 2
Time taken: 24.823 seconds, Fetched: 7 row(s)
hive> 
```

因此我们很容易得出20170627号有效的最新数据为行号rn为1的数据
#####三、更新数据
最后将数据更新到20170627分区,SQL如下
```
INSERT INTO TABLE mytable PARTITION(dt='20170627') 
SELECT id, name, age, createTime
FROM (SELECT id, name, age, createTime, row_number() OVER (PARTITION BY id ORDER BY createTime DESC) AS rn
    FROM mytable
    ) t
WHERE t.rn = 1;
```
查看数据
```
    > select * from mytable where dt='20170627';
OK
1   mary    18  2017-06-26 10:00:00 20170627
2   lucy    29  2017-06-26 10:00:00 20170627
3   jack    18  2017-06-26 10:00:00 20170627
4   nick    18  2017-06-27 10:00:00 20170627
5   tom 26  2017-06-27 12:00:00 20170627
Time taken: 0.121 seconds, Fetched: 5 row(s)
hive> 

```
对比后发现,数据确实是最新的。




最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容