hbase客户端API操作
<dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
2.hbase表的增删改查操作
具体代码如下:
1 package com.lowi.hadoop.hbase; 2 3 import org.apache.hadoop.conf.Configuration; 4 import org.apache.hadoop.hbase.*; 5 import org.apache.hadoop.hbase.client.*; 6 import org.apache.hadoop.hbase.filter.*; 7 import org.apache.hadoop.hbase.util.Bytes; 8 import org.junit.Before; 9 import org.junit.Test; 10 import java.io.IOException; 11 import java.util.ArrayList; 12 import java.util.Iterator; 13 import java.util.List; 14 15 public class TestHBase { 16 //声明一个configuration对象 17 private Configuration conf = null; 18 //声明一个Hbase数据库连接 19 private Connection conn = null; 20 21 @Before 22 public void init() throws IOException { 23 conf = HBaseConfiguration.create(); 24 //对于hbase的客户端,只需要知道hbase所使用的zookeeper集群地址就可以了 25 conf.set("hbase.zookeeper.quorum","node1:2181,node2:2181,node3:2181"); 26 //获取链接 27 conn = ConnectionFactory.createConnection(conf); 28 } 29 30 /** 31 * 创建表 32 * @throws IOException 33 */ 34 @Test 35 public void createTable() throws IOException { 36 //获取一个表的管理器 37 Admin admin = conn.getAdmin(); 38 //表描述器,并指定表名 39 HTableDescriptor table = new HTableDescriptor(TableName.valueOf("t_user_info".getBytes())); 40 //列族描述器,指定列族名 41 HColumnDescriptor hcd1 = new HColumnDescriptor("base_info"); 42 //指定第二个描述器,指定列名 43 HColumnDescriptor hcd2 = new HColumnDescriptor("extra_info"); 44 //版本shul 45 hcd2.setVersions(1, 3); 46 47 //将列族描述器添加到表描述器中 48 table.addFamily(hcd1).addFamily(hcd2); 49 //利用表的管理器创建表 50 admin.createTable(table); 51 52 //关闭 53 admin.close(); 54 conn.close(); 55 } 56 57 /** 58 * 修改表 59 * @throws IOException 60 */ 61 @Test 62 public void modifyTable() throws IOException { 63 //获取表的管理器 64 Admin admin = conn.getAdmin(); 65 //获取表的描述器 66 HTableDescriptor table = admin.getTableDescriptor(TableName.valueOf("t_user_info")); 67 68 HColumnDescriptor hcd1 = table.getFamily("extra_info".getBytes()); 69 hcd1.setVersions(2,5); 70 //添加新的列族 71 table.addFamily(new HColumnDescriptor("other_info")); 72 //表管理器admin 修改表 73 admin.modifyTable(TableName.valueOf("t_user_info"), table); 74 //关闭 75 admin.close(); 76 conn.close(); 77 } 78 79 /** 80 * put插入数据 81 * @throws IOException 82 */ 83 @Test 84 public void testPut() throws IOException { 85 //构建一个table对象,通过table对象来添加数据 86 Table table = conn.getTable(TableName.valueOf("t_user_info")); 87 //构建一个集合,用于存放put对象 88 ArrayList<Put> puts = new ArrayList<Put>(); 89 //构建一个put对象kv,指定其行键 例如hbase shell:put '表名','rowkey','列族:列名称','值' 90 Put put01 = new Put(Bytes.toBytes("user001")); 91 put01.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhangsan")); 92 93 Put put02 = new Put("user001".getBytes()); 94 put02.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("password"),Bytes.toBytes("123456")); 95 96 Put put03 = new Put("user002".getBytes()); 97 put03.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("lisi")); 98 put03.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("password"), Bytes.toBytes("23123")); 99 put03.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); 100 101 Put put04 = new Put("zhang_sh_01".getBytes()); 102 put04.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang01")); 103 put04.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); 104 105 Put put05 = new Put("zhang_sh_02".getBytes()); 106 put05.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang02")); 107 put05.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); 108 109 Put put06 = new Put("liu_sh_01".getBytes()); 110 put06.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("liu01")); 111 put06.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); 112 113 Put put07 = new Put("zhang_bj_01".getBytes()); 114 put07.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang03")); 115 put07.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); 116 117 Put put08 = new Put("zhang_bj_01".getBytes()); 118 put08.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang04")); 119 put08.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); 120 put08.setDurability(Durability.SYNC_WAL); 121 122 puts.add(put01); 123 puts.add(put02); 124 puts.add(put03); 125 puts.add(put04); 126 puts.add(put05); 127 puts.add(put06); 128 puts.add(put07); 129 puts.add(put08); 130 131 table.put(puts); 132 table.close(); 133 conn.close(); 134 } 135 136 /** 137 * 查询单条数据 138 * 读取数据 get:一次读一行 139 * hbase shell : get 't_user_info','rowkey' 140 * @throws IOException 141 */ 142 @Test 143 public void testGet() throws IOException { 144 Table table = conn.getTable(TableName.valueOf("t_user_info")); 145 //构建一个get查询参数对象,指定要get的哪一行 146 Get get = new Get("user001".getBytes()); 147 //返回查询结果数据 148 Result result = table.get(get); 149 //获取结果中的所有cell 150 List<Cell> cells = result.listCells(); 151 for (Cell c:cells) { 152 //获取行键 153 byte[] rowBytes = CellUtil.cloneRow(c); 154 //获取列族 155 byte[] familyBytes = CellUtil.cloneFamily(c); 156 //获取列族下的列名称 157 byte[] qualifierBytes = CellUtil.cloneQualifier(c); 158 byte[] valueBytes = CellUtil.cloneValue(c); 159 160 System.out.print(new String(rowBytes) + " "); 161 System.out.print(new String(familyBytes) + ":"); 162 System.out.print(new String(qualifierBytes) + " "); 163 System.out.println(new String(valueBytes)); 164 } 165 //关闭 166 table.close(); 167 conn.close(); 168 } 169 170 /** 171 * scan批量查询数据 172 * @throws IOException 173 */ 174 @Test 175 public void testScan() throws IOException { 176 //获取table对象 177 Table table = conn.getTable(TableName.valueOf("t_user_info")); 178 //获取scan对象 179 Scan scan = new Scan(); 180 //获取查询的数据 181 ResultScanner scanner = table.getScanner(scan); 182 //获取resultscanner所有数据,返回迭代器 183 Iterator<Result> iter = scanner.iterator(); 184 //遍历迭代器 185 while (iter.hasNext()){ 186 //获取当前每一行结果数据 187 Result result = iter.next(); 188 //获取当前每一行所有cell对象 189 List<Cell> cells = result.listCells(); 190 //迭代所有cell 191 for (Cell c:cells) { 192 //获取行键 193 byte[] rowBytes = CellUtil.cloneRow(c); 194 //获取列族 195 byte[] familyBytes = CellUtil.cloneFamily(c); 196 //获取列族下的列名称 197 byte[] qualifierBytes = CellUtil.cloneQualifier(c); 198 //列字段的值 199 byte[] valueBytes = CellUtil.cloneValue(c); 200 201 System.out.print(new String(rowBytes) + " "); 202 System.out.print(new String(familyBytes) + ":"); 203 System.out.print(new String(qualifierBytes) + " "); 204 System.out.println(new String(valueBytes)); 205 } 206 System.out.println("--------------------------------------------------------"); 207 } 208 //关闭 209 table.close(); 210 conn.close(); 211 } 212 213 /** 214 * 删除表中的列数据 215 * @throws IOException 216 */ 217 @Test 218 public void testDelData() throws IOException { 219 //获取table对象 220 Table table = conn.getTable(TableName.valueOf("t_user_info")); 221 //获取delete对象,需要一个rowkey 222 Delete delete = new Delete("user001".getBytes()); 223 224 //在delete对象中指定要删除的列族-列名称 225 delete.addColumn("base_info".getBytes(),"password".getBytes()); 226 //执行删除 227 table.delete(delete); 228 //关闭 229 table.close(); 230 conn.close(); 231 } 232 233 /** 234 * 删除表 235 * disable 't_user_info' 236 * drop 't_user_info' 237 * @throws IOException 238 */ 239 @Test 240 public void testDropTable() throws IOException { 241 Admin admin = conn.getAdmin(); 242 //删除表时先需要disable,将表置为不可用,然后delete 243 admin.disableTable(TableName.valueOf("t_user_info")); 244 admin.deleteTable(TableName.valueOf("t_user_ifo")); 245 admin.close(); 246 conn.close(); 247 } 248 249 /** 250 * 行键 前缀过滤器 251 * @throws IOException 252 */ 253 @Test 254 public void testPreFixFilter() throws IOException { 255 //针对于行键的前缀过滤器 256 Filter filter = new PrefixFilter(Bytes.toBytes("liu")); 257 testScan(filter); 258 } 259 260 /** 261 * 行过滤器 262 * 行键值 < user002 的所有行 263 * @throws IOException 264 */ 265 @Test 266 public void testRowFilter() throws IOException { 267 //行过滤器 需要一个比较运算符和比较器 268 // 行键值 < user002 的行值 269 RowFilter rf1 = new RowFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("user002"))); 270 testScan(rf1); 271 } 272 273 /** 274 * 列族过滤器 275 * 满足列族的所有行的数据,但是只是返回行中指定列族的所有列 276 * @throws IOException 277 */ 278 @Test 279 public void testFamilyFilter() throws IOException { 280 //列族名称的过滤器 返回结果中只会包含满足条件的列族中的数据 281 FamilyFilter ff1 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("base_info"))); 282 283 FamilyFilter ff2 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("base"))); 284 testScan(ff2); 285 } 286 287 /** 288 * 列名过滤器 289 * 满足列的所有行的数据,但是只是返回行中指定列 290 * @throws IOException 291 */ 292 @Test 293 public void testQualifierFilter() throws IOException { 294 //针对列名的过滤器 返回结果中只包含满足条件的列的数据 295 QualifierFilter qf1 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("password"))); 296 297 QualifierFilter qf2 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("user"))); 298 testScan(qf1); 299 } 300 301 /** 302 * 列值过滤器 303 * 满足列值的所有行的,所有列数据 304 * @throws IOException 305 */ 306 @Test 307 public void testColunmValueFilter() throws IOException { 308 //针对指定一个列的value的比较器过滤 309 ByteArrayComparable comparator1 = new RegexStringComparator("^zhang"); //以zhang开头 310 311 ByteArrayComparable comparator2 = new SubstringComparator("si"); //包含“si”字串 312 //列族,列名 = 列值 313 SingleColumnValueFilter scvf = new SingleColumnValueFilter("base_info".getBytes(), "username".getBytes(), 314 CompareFilter.CompareOp.EQUAL, comparator1); 315 testScan(scvf); 316 } 317 318 /** 319 * 多个过滤器同时使用 320 * @throws IOException 321 */ 322 @Test 323 public void testFilterList() throws IOException { 324 //构建一个列族的过滤器 325 FamilyFilter ff1 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("base"))); 326 //构建一个列的前缀过滤器 327 ColumnPrefixFilter cpf1 = new ColumnPrefixFilter("password".getBytes()); 328 //指定多个过滤器是否同时满足条件 329 // FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE); //只满足一个过滤条件即可 330 FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); //满足所有过滤条件 331 332 filterList.addFilter(ff1); 333 filterList.addFilter(cpf1); 334 testScan(filterList); 335 } 336 337 338 public void testScan(Filter filter) throws IOException { 339 Table table = conn.getTable(TableName.valueOf("t_user_info")); 340 341 Scan scan = new Scan(); 342 //设置过滤器 343 scan.setFilter(filter); 344 345 ResultScanner resultScanner = table.getScanner(scan); 346 Iterator<Result> iterator = resultScanner.iterator(); 347 //遍历所有result对象,获取结果 348 while (iterator.hasNext()){ 349 Result result = iterator.next(); 350 List<Cell> cells = result.listCells(); 351 for (Cell c: cells){ 352 //获取行键 353 byte[] rowBytes = CellUtil.cloneRow(c); 354 //获取列族 355 byte[] familyBytes = CellUtil.cloneFamily(c); 356 //获取列族下的列名称 357 byte[] qualifierBytes = CellUtil.cloneQualifier(c); 358 //列字段值 359 byte[] valueBytes = CellUtil.cloneValue(c); 360 361 System.out.print(new String(rowBytes) + " "); 362 System.out.print(new String(familyBytes) + ":"); 363 System.out.print(new String(qualifierBytes) + " "); 364 System.out.println(new String(valueBytes)); 365 } 366 System.out.println("-------------------------------------------------------"); 367 } 368 } 370 }
-
过滤器的类型很多,但是可以分为两大类——比较过滤器,专用过滤器
-
过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端;
3.1 hbase过滤器的比较运算符
LESS < LESS_OR_EQUAL <= EQUAL = NOT_EQUAL <> GREATER_OR_EQUAL >= GREATER >
3.2 hbase过滤器的比较器(指定比较机制)
BinaryComparator 按字节索引顺序比较指定字节数组
BinaryPrefixComparator 跟前面相同,只是比较左端的数据是否相同
NullComparator 判断给定的是否为空
BitComparator 按位比较
RegexStringComparator 提供一个正则的比较器,仅支持 EQUAL 和非EQUAL
SubstringComparator 判断提供的子串是否出现在value中。
3.3 过滤器使用实战
具体demo见如上代码