1.HBase若是加了列限定,若是該列不存在時返回的結果爲empty. 看下面的代碼: java
Get get = new Get(Bytes.toBytes("100")); get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
這裏加入了列限定,也就是隻返回列族info下面的name字段。可是若是name字段根本不存在,返回的Result在調用 result.isEmpty()時則返回爲true,也就是說就算其餘字段存在,也什麼都沒返回來,包括rowkey也沒有返回來。固然,若是是限定多 個列,只要一個列存在就能夠正常返回。因此須要注意。mongodb
2.HBase在scan時指定的StartRow裏面不能加「-」 看下面的代碼:apache
Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes("3136947-")); scan.setStopRow(Bytes.toBytes("3136947-" + 1));
個人本意是查詢rowkey以 3136947- 開頭的行,可是由於個人裏面有一個-(「槓」),因此什麼都沒返回,去掉-後正常。這說明這裏是不能使用-,-也並非轉義字符,轉義後也仍是scan不出來的。不知道其餘字符是否是也不行,沒有測試。 因此須要注意。
app
3.HBase在scan時過濾掉指定列不存在的記錄ide
若是想返回某個字段必須存在的行,不存在該字段的記錄過濾掉不返回,方法以下:oop
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。orm
4.利用MapReduce導出hbase數據繼承
若是hbase做爲數據的輸出,job設置以下:hadoop
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做爲數據的輸入。代碼以下:
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