🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] 依赖的maven ~~~ <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase</artifactId> <version>1.4.3</version> <type>pom</type> </dependency> ~~~ 代码 ~~~ package com.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.regionserver.BloomType; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; public class HbaseDemo { private Configuration conf = null; private Connection conn = null; @Before public void init() throws IOException { //构建个配置 conf = HBaseConfiguration.create(); //对于hbase的客户端来说,只需要知道hbase所使用的zookeeper集群就可以了 //因为hbase的客户端找hbase读写数据完全不用经过hmaster conf.set("hbase.zookeeper.quorum", "master:2181,slave:2181"); conn = ConnectionFactory.createConnection(conf); } //建表 @Test public void testCreate() throws IOException { //获取一个表管理器 Admin admin = conn.getAdmin(); //构造一个表描述器,并指定表名 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("t_user_info")); //构造一个列族描述器,并指定列族名 HColumnDescriptor hcd1 = new HColumnDescriptor("base_info"); //为该列族设定一个布隆过滤器类型参数/版本数量 hcd1.setBloomFilterType(BloomType.ROW).setVersions(1, 3); //构造第二个列族描述器,并指定列族名 HColumnDescriptor hcd2 = new HColumnDescriptor("extra_info"); //为该列族设定一个布隆过滤器类型参数/版本数量 hcd2.setBloomFilterType(BloomType.ROW).setVersions(1, 3); //将列族描述器添加到表描述器中 htd.addFamily(hcd1).addFamily(hcd2); admin.createTable(htd); admin.close(); conn.close(); } //删除表 @Test public void testDrop() throws IOException { Admin admin = conn.getAdmin(); admin.disableTable(TableName.valueOf("t_user_info")); admin.deleteTable(TableName.valueOf("t_user_info")); admin.close(); conn.close(); } //修改表定义 @Test public void testModify() throws IOException { //这 Admin admin = conn.getAdmin(); // admin.disableTable(TableName.valueOf("t_user_info")); // 修改已有的ColumnFamily HTableDescriptor table = admin.getTableDescriptor(TableName.valueOf("t_user_info")); HColumnDescriptor f2 = table.getFamily("extra_info".getBytes()); f2.setBloomFilterType(BloomType.ROWCOL); f2.setVersions(1, 5); // 添加新的ColumnFamily111 table.addFamily(new HColumnDescriptor("other_info")); admin.modifyTable(TableName.valueOf("t_user_info"), table); admin.close(); conn.close(); } @Test public void testPut() throws IOException { Table table = conn.getTable(TableName.valueOf("t_user_info")); ArrayList<Put> puts = new ArrayList<Put>(); //构建一个put对象(kv),指定行键 Put put01 = new Put(Bytes.toBytes("user001")); put01.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhangsan")); Put put02 = new Put("user001".getBytes()); put02.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("password"), Bytes.toBytes("123456")); Put put03 = new Put("user002".getBytes()); put03.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("lisi")); put03.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); Put put04 = new Put("zhang_sh_01".getBytes()); put04.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang01")); put04.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); Put put05 = new Put("zhang_sh_02".getBytes()); put05.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang02")); put05.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); Put put06 = new Put("liu_sh_01".getBytes()); put06.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("liu01")); put06.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); Put put07 = new Put("zhang_bj_01".getBytes()); put07.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang03")); put07.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); Put put08 = new Put("zhang_bj_01".getBytes()); put08.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhang04")); put08.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false")); puts.add(put01); puts.add(put02); puts.add(put03); puts.add(put04); puts.add(put05); puts.add(put06); puts.add(put07); puts.add(put08); table.put(puts); table.close(); conn.close(); } //读取,get一次读取一行 @Test public void testGet() throws IOException { Table table = conn.getTable(TableName.valueOf("t_user_info")); //构造一个get查询对象.指定要get的是那一行 Get get = new Get("user001".getBytes()); Result result = table.get(get); CellScanner cellScanner = result.cellScanner(); //迭代 while (cellScanner.advance()) { Cell current = cellScanner.current(); //列族名 byte[] familyArray = current.getFamilyArray(); //列标识符的名称 byte[] qualifierArray = current.getQualifierArray(); //具体的值 byte[] valueArray = current.getValueArray(); //获取有用字符 System.out.printf(new String(familyArray, current.getFamilyOffset(), current.getFamilyLength())); System.out.printf(":" + new String(qualifierArray, current.getQualifierOffset(), current.getQualifierLength())); System.out.printf(" " + new String(valueArray, current.getValueOffset(), current.getValueLength())); System.out.println(); } table.close(); conn.close(); } //删除表中的数据 @Test public void testDel() throws IOException { Table t_user_info = conn.getTable(TableName.valueOf("t_user_info")); //row key为user001 Delete delete = new Delete("user001".getBytes()); //column=base_info:password 删除这一列 delete.addColumn("base_info".getBytes(), "password".getBytes()); t_user_info.delete(delete); t_user_info.close(); conn.close(); } //批量查询数据 @Test public void testScan() throws IOException { Table t_user_info = conn.getTable(TableName.valueOf("t_user_info")); //表是liu_sh_01,row key是zhang_bj_01 //数据(字典排序,从liu_sh_01到zhang_bj_01之间的row key全部遍历)("\000"不加这个是包头不包尾,加了是全部包,原因是这个字段排序是排在zhang_bj_01后面) Scan scan = new Scan(Bytes.toBytes("liu_sh_01"), Bytes.toBytes("zhang_bj_01" + "\000")); ResultScanner scanner = t_user_info.getScanner(scan); //迭代器 Iterator<Result> iter = scanner.iterator(); while (iter.hasNext()) { //获取一行记录 Result result = iter.next(); //获取到每一个cell CellScanner cellScanner = result.cellScanner(); //遍历cell while (cellScanner.advance()) { Cell current = cellScanner.current(); byte[] familyArray = current.getFamilyArray(); byte[] valueArray = current.getValueArray(); byte[] qualifierArray = current.getQualifierArray(); byte[] rowArray = current.getRowArray(); System.out.print(new String(rowArray, current.getRowOffset(), current.getRowLength())+" "); System.out.print(new String(familyArray, current.getFamilyOffset(), current.getFamilyLength())); System.out.print(":" + new String(qualifierArray, current.getQualifierOffset(), current.getQualifierLength())); System.out.print(" " + new String(valueArray, current.getValueOffset(), current.getValueLength())); System.out.println(); } System.out.println("-----------------------------"); } } } ~~~