1.HBase如果加了列限定,如果该列不存在时返回的结果为empty.
看下面的代码:
|
1
2
|
Get
get = new
Get(Bytes.toBytes("100"));
get.addColumn(Bytes.toBytes("info"),
Bytes.toBytes("name"));
|
这里加入了列限定,也就是只返回列族info下面的name字段。但是如果name字段根本不存在,返回的Result在调用result.isEmpty()时则返回为true,也就是说就算其他字段存在,也什么都没返回来,包括rowkey也没有返回来。当然,如果是限定多个列,只要一个列存在就可以正常返回。所以需要注意。
2.HBase在scan时指定的StartRow里面不能加-
看下面的代码:
|
1
2
3
4
|
Scan
scan = new
Scan();
scan.setStartRow(Bytes.toBytes("3136947-"));
scan.setStopRow(Bytes.toBytes("3136947-"
+ 1));
|
我的本意是查询rowkey以 3136947- 开头的行,但是因为我的里面有一个-(“杠”),所以什么都没返回,去掉-后正常。这说明这里是不能使用-,-也并不是转义字符,转义后也还是scan不出来的。不知道其他字符是不是也不行,没有测试。
所以需要注意。
3.HBase在scan时过滤掉指定列不存在的记录
如果想返回某个字段必须存在的行,不存在该字段的记录过滤掉不返回,方法如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
Scan
scan = new
Scan();
scan.setStartRow(Bytes.toBytes("3136947"));
scan.setStopRow(Bytes.toBytes("3136947"
+ 1));
scan.addColumn(Bytes.toBytes("info"),
Bytes.toBytes("name"));
SingleColumnValueFilter
filter = new
SingleColumnValueFilter(Bytes.toBytes("info"),
Bytes.toBytes("name"),
CompareFilter.CompareOp.NOT_EQUAL,
Bytes.toBytes("0"));
filter.setFilterIfMissing(true);
scan.setFilter(filter);
|
注意:如果是判断某个列是否存在,必须在addColumn里面加上该列,也就是必须返回的字段里面必须包含该列,否则也不会返回,因为在处理的时候是调用addColumn然后才会调用过滤器。
这里的过滤器里面指定该列的字段值必须不等于0(当然,如果你的name里有等于0的当然不能使用0),并且设置setFilterIfMissing为true,也就是设置为如果该列不存在就过滤掉这条数据,默认为false。
4.利用MapReduce导出hbase数据
如果hbase作为数据的输出,job设置如下:
|
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
|
Configuration
conf = HBaseConfiguration.create();
Scan
scan = new
Scan();
scan.setStartRow(Bytes.toBytes("3136947"));
scan.setStopRow(Bytes.toBytes("3136947"
+ 1));
scan.addColumn(Bytes.toBytes("info"),
Bytes.toBytes("name"));
scan.addFamily(UserStoreHelper.FAMILY_INFO);
scan.addColumn(UserStoreHelper.FAMILY_INFO,
UserStoreHelper.USER_ID);
scan.addColumn(UserStoreHelper.FAMILY_INFO,
UserStoreHelper.FRIENDS);
scan.addColumn(UserStoreHelper.FAMILY_INFO,
UserStoreHelper.LEVEL_CODE);
final
Job job = new
Job(conf, "exportHBaseUser");
job.setJarByClass(TestJobCreator.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileOutputFormat.setOutputPath(job,
new
Path("test1"));
TableMapReduceUtil.initTableMapperJob(Bytes.toBytes("usertable"),
scan,
TestMapper.class,
Text.class,
NullWritable.class,
job);
|
在initTableMapperJob里面设置的map必须继承org.apache.hadoop.hbase.mapreduce.TableMapper,并且最后两个设置的参数是自己定义的map的输出时的key和value的类型。
5.利用mapReduce插入数据到HBase
如果hbase作为数据的输入。代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
final
Configuration conf = HBaseConfiguration.create();
final
Job job = new
Job(conf, "Sync-To-HBase");
job.setJarByClass(PostStoreExportHBaseJobCreator.class);
job.setInputFormatClass(MongoInputFormat.class);
TableMapReduceUtil.initTableReducerJob("usertable",
null,
job);
job.setMapperClass(TestMapper.class);
job.setNumReduceTasks(0);
|
这里map的输出必须是key为ImmutableBytesWritable,value为 Put