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
));
|
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);
|
这里的过滤器里面指定该列的字段值必须不等于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"
));
// job.setReducerClass(HbaseExportReduce.class);
// job.setPartitionerClass(UserPartitioner.class);
// job.setNumReduceTasks(14);
TableMapReduceUtil.initTableMapperJob(Bytes.toBytes(
"usertable"
),
scan,
TestMapper.
class
,
Text.
class
,
NullWritable.
class
,
job);
|
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
);
//我这里是以mongodb为输入
job.setInputFormatClass(MongoInputFormat.
class
);
TableMapReduceUtil.initTableReducerJob(
"usertable"
,
null
, job);
//把数据转换为hbase表格式的map
job.setMapperClass(TestMapper.
class
);
//直接入hbase库不需要reduce
job.setNumReduceTasks(
0
);
|
这里map的输出必须是key为ImmutableBytesWritable,value为 Put