日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java自定义 filter,HBase自定义Filter

發(fā)布時間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java自定义 filter,HBase自定义Filter 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

必需要提前說明下:不建議使用自定義的Filter。所有的Filter都是在服務端生效:就是說需要將自定義的Filter封裝為jar,上傳到HBase的類路徑下,并重啟HBase使之生效。對于生產(chǎn)環(huán)境的HBase來說,重啟通常是不能接受的。

Filter的設置是在客戶端完成的,而Filter的邏輯是在HBase的服務端完成的,中間需要一次序列化。我試過幾種序列化方案,不過protobuffer以外的其他幾種效果不算好。HBase自帶的Filter也是用protobuffer進行的序列化,因此使用protobuffer還可以少傳幾個包。

需要提前說明的已經(jīng)說完了,開始進入正題。這次從一個案例開始說起:在HBase中存儲著用戶行為記錄,行鍵設計為“uid(6位)+etime(時間戳/1000)+tid(7位)+順序號(8位)”。其中uid為用戶ID、etime為事件時間、tid為行為標簽。目標是檢索出某個用戶在指定時間范圍內(nèi)的幾種行為數(shù)據(jù)。

針對這個案例我們自定義一個CustomRowKeyFilter,并將一個用戶ID、事件起止時間以及多個行為ID作為CustomRowKeyFilter的成員變量。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

packagecom.zhyea.dev.hbase.filter;

importorg.apache.hadoop.hbase.Cell;

importorg.apache.hadoop.hbase.filter.FilterBase;

importorg.apache.hadoop.hbase.util.Bytes;

importjava.io.IOException;

publicclassCustomRowKeyFilterextendsFilterBase{

privatelongpid;

privatelongeventTime;

privateStringtids;

privatebooleanfilterOutRow=false;

publicCustomRowKeyFilter(long_pid,long_eventTime,String_tids){

this.pid=_pid;

this.eventTime=_eventTime;

this.tids=_tids;

}

@Override

publicbooleanfilterRowKey(byte[]data,intoffset,intlength){

StringrowKey=Bytes.toString(data,offset,length);

this.filterOutRow=check(rowKey);

returnthis.filterOutRow;

}

publicReturnCodefilterKeyValue(Cellv)throwsIOException{

if(this.filterOutRow){

returnReturnCode.NEXT_ROW;

}

returnReturnCode.INCLUDE;

}

privatebooleancheck(StringrowKey){

try{

if(rowKey.length()<7){

returntrue;

}

long_pid=Long.valueOf(rowKey.substring(0,6));

long_eTime=Long.valueOf(rowKey.substring(6,16));

long_tid=Long.valueOf(rowKey.substring(16,23));

if(this.pid!=_pid){

returntrue;

}

if(this.eventTime>_eTime){

returntrue;

}

if(!this.tids.contains(_tid+"")){

returntrue;

}

}catch(Exceptione){

returntrue;

}

returnfalse;

}

}

代碼中繼承了FilterBase類,可以減少一些結(jié)構(gòu)性的代碼工作。至于Filter是如何工作的,在網(wǎng)上找到的這張圖應該描述得很清楚了:

前面的代碼只是實現(xiàn)了Filter的處理邏輯。要想使用這個Filter還需要做一些序列化處理。如前面所說序列化方案選擇的是protobuffer,這里需要先定義一個描述文件CustomRowKeyFilterProto.proto,內(nèi)容如下:

1

2

3

4

5

6

7

8

9

10

packagefilter;

optionjava_package="com.zhyea.dev.hbase.filter.proto";

optionjava_outer_classname="CustomRowKeyFilterProto";

messageCustomRowKeyFilter{

requiredint64pid=1;

requiredint64eventTime=2;

requiredstringtids=3;

}

定義完成后,執(zhí)行protoc命令:

1

protoc-I=./--java_out=../src/main/javaCustomRowKeyFilterProto.proto

其中“-I”指定了proto描述文件的父目錄, “—java_out”指定了java類的類路徑,具體請根據(jù)自己的情況進行設置。執(zhí)行命令后會在包com.zhyea.dev.hbase.filter.proto下生成序列化工具類CustomRowKeyFilterProto.java。

接下來在CustomRowKeyFilter中重寫Filter類的toByteArray()方法和parseFrom()方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

publicbyte[]toByteArray()throwsIOException{

CustomRowKeyFilterProto.CustomRowKeyFilter.Builderbuilder=CustomRowKeyFilterProto.CustomRowKeyFilter.newBuilder();

builder.setPid(this.pid);

builder.setEventTime(this.eventTime);

builder.setCids(this.tids);

returnbuilder.build().toByteArray();

}

publicstaticFilterparseFrom(finalbyte[]pbBytes)throwsDeserializationException{

CustomRowKeyFilterProto.CustomRowKeyFilterproto;

try{

proto=CustomRowKeyFilterProto.CustomRowKeyFilter.parseFrom(pbBytes);

}catch(InvalidProtocolBufferExceptione){

thrownewDeserializationException(e);

}

long_pid=proto.getPid();

long_eventTime=proto.getEventTime();

String_tids=proto.getCids();

returnnewCustomRowKeyFilter(_pid,_eventTime,_tids);

}

這樣自定義Filter就完成了。剩下的事情就是將之打包并上傳到HBase(每個RegionServer)的類路徑下。然后就可以在程序中使用了。

現(xiàn)在再仔細想想這個程序,是否一定需要一個自定義Filter呢!我們已經(jīng)將查詢需要的所有元素都定義在行鍵里了。那么可以使用“uid+起始時間”作為startRow,“uid+結(jié)束時間”作為stopRow完成時間范圍的匹配,使用RegexStringComparator來處理tid的匹配,這樣直接使用HBase提供的RowFilter就能解決問題了。唯一需要注意的事情就是在設計表時多花些心思在行鍵上罷了。

就是這樣。

參考文檔

總結(jié)

以上是生活随笔為你收集整理的java自定义 filter,HBase自定义Filter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。