Hbase 相关
HBase Java Api 基础操作
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.*; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.List; /** * http://hbase.org.cn/docs/32.html 官网地址 * */ / public class TestHbase { //创建变量 方便方法使用 Connection conn=null; /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:35 * @param * @return void * 用于连接hbase */ @Before public void init() throws IOException { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","hdp1,hdp2,hdp3"); conn = ConnectionFactory.createConnection(conf); } /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:35 * @param * @return void * 创建表空间,名称为ns0507 */ @Test public void createNS() throws IOException { //获取空间操作权限 Admin admin = conn.getAdmin(); admin.createNamespace(NamespaceDescriptor.create("ns0507").build()); admin.close(); } /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:36 * @param * @return void * 创建Hbase表,表名列族自定义 */ @Test public void createTable() throws IOException { Admin admin = conn.getAdmin(); HTableDescriptor homework0507 = new HTableDescriptor(TableName.valueOf("ns0507:homework0507")); //列族名称 可以多个列族 HColumnDescriptor info = new HColumnDescriptor("info"); homework0507.addFamily(info); admin.createTable(homework0507); admin.close(); } /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:37 * @param * @return void * 查看指定表中的数据 */ @Test public void scan() throws IOException { //获取表的操作权限 Table table = conn.getTable(TableName.valueOf("ns0507:homework0507")); ResultScanner scanner = table.getScanner(new Scan()); Result next = scanner.next(); while(next!=null){ List<Cell> cells = next.listCells(); for (Cell cell : cells) { byte[] row = CellUtil.cloneRow(cell); byte[] family = CellUtil.cloneFamily(cell); byte[] qualifier = CellUtil.cloneQualifier(cell); byte[] value = CellUtil.cloneValue(cell); System.out.println(Bytes.toString(row)+","+Bytes.toString(family)+","+Bytes.toString(qualifier)+","+Bytes.toInt(value)); } next=scanner.next(); table.close(); } } //打印方法 简便查询方法 美观简洁 public void prints(Scan scan) throws IOException { Table table = conn.getTable(TableName.valueOf("ns0507:homework0507")); ResultScanner scanner = table.getScanner(scan); Result next = scanner.next(); while(next!=null){ List<Cell> cells = next.listCells(); for (Cell cell : cells) { byte[] row = CellUtil.cloneRow(cell); byte[] family = CellUtil.cloneFamily(cell); byte[] qualifier = CellUtil.cloneQualifier(cell); byte[] value = CellUtil.cloneValue(cell); System.out.println(Bytes.toString(row)+","+Bytes.toString(family)+","+Bytes.toString(qualifier)+","+Bytes.toInt(value)); } next=scanner.next(); } table.close(); } /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:38 * @param * @return void * 查询行键为user10的数据 */ @Test public void get1() throws IOException { Table table = conn.getTable(TableName.valueOf("ns0507:homework0507")); Scan scan = new Scan(); //创建行键实体类 进行行键查询 RowFilter user10 = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("user10"))); scan.setFilter(user10); ResultScanner scanner = table.getScanner(scan); Result next = scanner.next(); while(next!=null){ List<Cell> cells = next.listCells(); for (Cell cell : cells) { byte[] row = CellUtil.cloneRow(cell); byte[] family = CellUtil.cloneFamily(cell); byte[] qualifier = CellUtil.cloneQualifier(cell); byte[] value = CellUtil.cloneValue(cell); System.out.println(Bytes.toString(row)+","+Bytes.toString(family)+","+Bytes.toString(qualifier)+","+Bytes.toInt(value)); } next=scanner.next(); } table.close(); } /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:40 * @param * @return void * 查询姓名包含“小”的数据 条件查询列族为name的数据中包含小的 */ @Test public void get2() throws IOException { Table table = conn.getTable(TableName.valueOf("ns0507:homework0507")); Scan scan = new Scan(); scan.setFilter(new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL,new SubstringComparator("小"))); ResultScanner scanner = table.getScanner(scan); Result next = scanner.next(); while(next!=null){ List<Cell> cells = next.listCells(); for (Cell cell : cells) { byte[] row = CellUtil.cloneRow(cell); byte[] family = CellUtil.cloneFamily(cell); byte[] qualifier = CellUtil.cloneQualifier(cell); byte[] value = CellUtil.cloneValue(cell); System.out.println(Bytes.toString(row)+","+Bytes.toString(family)+","+Bytes.toString(qualifier)+","+Bytes.toInt(value)); } next=scanner.next(); } table.close(); } /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:42 * @param * @return void * 查询年龄大于20的数据 条件查询列族 age中年龄大于20岁的 */ @Test public void get3() throws IOException { Table table = conn.getTable(TableName.valueOf("ns0507:homework0507")); Scan scan = new Scan(); SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"), CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(20))); scan.setFilter(singleColumnValueFilter); prints(scan); } /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:43 * @param * @return void * 查询年龄大于20且性别为女的数据 多条件查询列族 age大于20 并且 列族sex 为女的 */ @Test public void get4() throws IOException { Table table = conn.getTable(TableName.valueOf("ns0507:homework0507")); Scan scan = new Scan(); SingleColumnValueFilter age = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"), CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(20))); age.setFilterIfMissing(true); SingleColumnValueFilter sex = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("sex"), CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("女"))); sex.setFilterIfMissing(true); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); filterList.addFilter(age); filterList.addFilter(sex); scan.setFilter(filterList); prints(scan); table.close(); } /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:44 * @param * @return void * 删除年龄大于30的数据 删除列族age大于30的 */ @Test public void get5() throws IOException { Table table = conn.getTable(TableName.valueOf("ns0507:homework0507")); Scan scan = new Scan(); SingleColumnValueFilter age = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"), CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(30))); scan.setFilter(age); ResultScanner scanner = table.getScanner(scan); Result next = scanner.next(); while(next!=null){ List<Cell> cells = next.listCells();//获取list集合 for (Cell cell : cells) {//遍历拿值 byte[] row = CellUtil.cloneRow(cell);//获取行键 table.delete(new Delete(row));//根据行键删除 } next=scanner.next(); } table.close(); } //10、删除表所有数据 /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:44 * @param * @return void * 删除表中的数据 删除方式不止一种 */ @Test public void deleteAll() throws IOException { Table table = conn.getTable(TableName.valueOf("ns0507:homework0507")); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); Result next = scanner.next(); while(next!=null){ List<Cell> cells = next.listCells();//获取list集合 for (Cell cell : cells) {//遍历拿值 byte[] row = CellUtil.cloneRow(cell);//获取行键 table.delete(new Delete(row));//根据行键删除 } next=scanner.next(); } table.close(); } /** * @author: zcb * @description: TODO * @date: 2022-06-09 10:47 * @param * @return void * 删除表 需要注意 删除之前需要先进行禁用 */ @Test public void deleteTable() throws IOException { Admin admin = conn.getAdmin(); admin.disableTable(TableName.valueOf("ns0507:homework0507")); admin.deleteTable(TableName.valueOf("ns0507:homework0507")); admin.close(); } //删除表空间 @Test public void DeleteNS() throws IOException { Admin admin = conn.getAdmin(); admin.deleteNamespace("ns0507"); admin.close(); } }
我有一杯酒,足以慰风尘。