HBase的CURD代码API实现(示例代码)

Hbase 的Java API实现

1.配置连接

public void createConnaction() {
//需要新建配置类config和连接类conn,然后用config作为参数去新建连接。
Configuration config = null;
    Connection conn = null;
    Table table = null;
    config = HBaseConfiguration.create();
    config.set("hbase.zookeeper.quorum", "192.168.31.239");
    config.set("hbase.zookeeper.property.clientPort", "2181");
    config.set("zookeeper.znode.parent", "/hbase-unsecure");//unsecure,zookeeper2.7.6之前连接不用验证,之后得加这个验证不然会有asal报错提示
    config.set("hbase.master", "192.168.31.239:60010");
    try {
        // 创建连接
        conn = ConnectionFactory.createConnection(config);
        System.out.println("===链接成功===");
        // 获取表
        table = conn.getTable(TableName.valueOf("default:xcw_test"));
        System.out.println("===获取表成功===");

    } catch (IOException e) {
        e.printStackTrace();
    }

}

2.关闭连接

public void closeConnaction() {
    try {
        if (conn != null) {
            conn.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        if (table != null) {
            table.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

3.输出

public void output(Result result) throws IOException {
    CellScanner cellScanner = result.cellScanner();
    while (cellScanner.advance()) {
        Cell cell = cellScanner.current();
        byte[] rowArray = cell.getRowArray();  //本kv所属的行键的字节数组
        byte[] familyArray = cell.getFamilyArray();  //列族名的字节数组
        byte[] qualifierArray = cell.getQualifierArray();  //列名的字节数据
        byte[] valueArray = cell.getValueArray(); // value的字节数组

        System.out.printf("|%10s|%10s|%10s|%10s|\n",
                new String(rowArray, cell.getRowOffset(), cell.getRowLength()),
                new String(familyArray, cell.getFamilyOffset(), cell.getFamilyLength()),
                new String(qualifierArray, cell.getQualifierOffset(), cell.getQualifierLength()),
                new String(valueArray, cell.getValueOffset(), cell.getValueLength()));
    }
}

该段代码还没看懂,主要疑惑是result的处理等等。

4.创建命名空间

public void createNamespace(Connection conn) throws IOException {
    Admin admin = conn.getAdmin();
    String createNameSpace;
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入新建表空间名");
    createNameSpace = scanner.nextLine();
    NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(createNameSpace);
    NamespaceDescriptor build = builder.build();

    admin.createNamespace(build);

    admin.close();
}

5.查询指定命名空间

public void queryOneNamespace() throws IOException {
    Admin admin = conn.getAdmin();
    String queryNameSpace;
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入待查询命名空间");
    queryNameSpace = scanner.nextLine();
    NamespaceDescriptor test = admin.getNamespaceDescriptor(queryNameSpace);
    System.out.println(test);

    Map<String, String> configuration = test.getConfiguration();
    System.out.println(configuration);

    String name = test.getName();
    System.out.println(name);

    admin.close();
}

5.1查看所有命名空间

public void queryAllNamespace() throws IOException {
    Admin admin = conn.getAdmin();

    NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();

    for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
        System.out.println(namespaceDescriptor);
    }
    admin.close();
}

6.查看表

6.1查看所有表信息

public void queryAllTable() throws IOException {
        Admin admin = conn.getAdmin();

        TableName[] tableNames = admin.listTableNames();

        for (TableName tableName : tableNames) {
            System.out.println(tableName);
        }

        admin.close();
    }

6.2查看指定表

public void queryOneTableMetadata() throws IOException {
    Admin admin = conn.getAdmin();
    String queryOneTableMetadata;
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入待查询命名空间:表名");
    queryOneTableMetadata = scanner.nextLine();
    HTableDescriptor fei_wen = admin.getTableDescriptor(TableName.valueOf(queryOneTableMetadata));

    System.out.println(fei_wen);

    String name = fei_wen.getNameAsString();
    System.out.println("\n命名空间+表名: " + name);

    HColumnDescriptor[] columnFamilies = fei_wen.getColumnFamilies();
    for (HColumnDescriptor columnFamily : columnFamilies) {
        System.out.println("\n列族: " + columnFamily);
    }

    Map<String, String> configuration = fei_wen.getConfiguration();
    System.out.println("\n表配置: " + configuration);

    TableName tableName = fei_wen.getTableName();
    System.out.println("\n命名空间+表名: " + tableName.getNameAsString());
    System.out.println("\n命名空间: " + tableName.getNamespaceAsString());
    System.out.println("\n表名: " + tableName.getQualifierAsString());

    admin.close();
}

7.创建表

/**
 * 创建表
 * <p>
 * 可以用多个HColumnDescriptor来定义多个列族,然后通过hTableDescriptor.addFamily添加,但是目前只建议一个表创建一个列族,
 * 防止对性能有影响
 *
 * @param
 */
public void createTable() {
    Admin admin = null;
    try {
        // 获取表管理器对象
        admin = conn.getAdmin();
        String createTable;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入新建表名,namespace:tablename");
        createTable = scanner.nextLine();
        // 创建一个表描述对象
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(createTable));
        System.out.println("请输入列族");
        String colFamily;
        colFamily = scanner.nextLine();
        // 创建一个列族描述对象
        HColumnDescriptor base_info = new HColumnDescriptor(colFamily);
        // 通过列族描述定义对象,可以设置列族的很多重要属性信息
        base_info.setMaxVersions(3); // 设置该列族中存储数据的最大版本数,默认是1


        hTableDescriptor.addFamily(base_info);

        admin.createTable(hTableDescriptor);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (admin != null) {
                admin.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

第二种实现
public void createTable() throws Exception {
		// 创建表管理类
		HBaseAdmin admin = new HBaseAdmin(config); // hbase表管理
		// 创建表描述类
		TableName tableName = TableName.valueOf("test3"); // 表名称
		HTableDescriptor desc = new HTableDescriptor(tableName);
		// 创建列族的描述类
		HColumnDescriptor family = new HColumnDescriptor("info"); // 列族
		// 将列族添加到表中
		desc.addFamily(family);
		HColumnDescriptor family2 = new HColumnDescriptor("info2"); // 列族
		// 将列族添加到表中
		desc.addFamily(family2);
		// 创建表
		admin.createTable(desc); // 创建表
	}

8.更新表

public void updateTable() {
    Admin admin = null;
    String updateTable;
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入更新的命名空间:表名");
    updateTable = scanner.nextLine();
    try {
        admin = conn.getAdmin();

        HTableDescriptor fei_class = admin.getTableDescriptor(TableName.valueOf(updateTable));

        // 增加列族
        HColumnDescriptor newFamily = new HColumnDescriptor("test_info".getBytes());
        newFamily.setBloomFilterType(BloomType.ROWCOL); // 设置布隆过滤器的类型

        fei_class.addFamily(newFamily);

        admin.modifyTable(TableName.valueOf(updateTable), fei_class);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (admin != null) {
                admin.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

9.删除表

/**
 * 删除表
 * <p>
 * 想删除表,必须先关闭表
 *
 * @param
 */
public void deleteTable(Connection conn) {
    Admin admin = null;
    String deleteTable;
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入删除的表名,namespace:tablename");
    deleteTable = scanner.nextLine();
    try {
        admin = conn.getAdmin();
        // 关闭表
        admin.disableTable(TableName.valueOf(deleteTable));
        // 删除表
        admin.deleteTable(TableName.valueOf(deleteTable));

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (admin != null) {
                admin.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

10.查询表数据

10.1查询表所有数据

public void queryAllTableData() throws IOException {
    String queryAllTableData;
    Scanner scanner2 = new Scanner(System.in);
    System.out.println("请输入待查询表,格式 nmspace:tablename");
    queryAllTableData = scanner2.nextLine();
    table = conn.getTable(TableName.valueOf(queryAllTableData));
    System.out.println("===获取表成功===");
    try {
        ResultScanner scanner = table.getScanner(new Scan());

        System.out.printf("|%10s|%10s|%10s|%10s|\n", "row key", "family", "qualifier", "value");
        for (Result result : scanner) {
            output(result);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

10.2查询表指定rowkey数据

public void queryRowKey() throws IOException {
    String queryRowKey ;
    String queryTable;
    Scanner scanner=new Scanner(System.in);
    System.out.println("请输入待查询表,格式 nmspace:tablename");
    queryTable = scanner.nextLine();
    table = conn.getTable(TableName.valueOf(queryTable));
    System.out.println("请输入带查询数据的rowkey");
    queryRowKey=scanner.nextLine();
    try {
        // get对象指定行键
        Get get = new Get(queryRowKey.getBytes(StandardCharsets.UTF_8));

        Result result = table.get(get);

        System.out.printf("|%10s|%10s|%10s|%10s|\n", "row key", "family", "qualifier", "value");

        output(result);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

10.3查询rowkey列数据

public void queryValueByRowKey() {
    try {
        // get对象指定行键
        Get get = new Get("1".getBytes(StandardCharsets.UTF_8));

        Result result = table.get(get);

        byte[] value = result.getValue("co".getBytes(), "name".getBytes());

        System.out.println(new String(value));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

11.插入单条数据

先建一个put的list,put创建时参数是rowkey,然后add列族,列名,值,从rowkey的参数开始,所有的参数都要Byte.tobytes(参数)转化或者“参数”.getbytes,然后把该条add到put list,最后table.put put的list。然后table.flushcommit//不写也可以,put会自动触发commit事件
public void insertOneData() {

    Put put = new Put("3".getBytes());//Put的参数是插入行的rowkey

    put.addColumn("no".getBytes(), "class".getBytes(), "一班".getBytes());

    try {
        table.put(put);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

第二种实现(多条数据)
public void insertData() throws Exception {
		table.setAutoFlushTo(false);
		table.setWriteBufferSize(534534534);
		ArrayList<Put> arrayList = new ArrayList<Put>();
		for (int i = 21; i < 50; i++) {
			Put put = new Put(Bytes.toBytes("1234"+i));
			put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"+i));
			put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234+i));
			arrayList.add(put);
		}
		
		//插入数据
		table.put(arrayList);
		//提交
		table.flushCommits();
	}
第三种实现(单条数据)
public void uodateData() throws Exception {
		Put put = new Put(Bytes.toBytes("1234"));
		put.add(Bytes.toBytes("info"), Bytes.toBytes("namessss"), Bytes.toBytes("lisi1234"));
		put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234));
		//插入数据
		table.put(put);
		//提交
		table.flushCommits();
	}



12.更新单条数据

public void updateData() throws IOException {
    Put put1 = new Put("9".getBytes());
    put1.addColumn("no".getBytes(), "name".getBytes(), "刘胡兰".getBytes());
    Put put2 = new Put("8".getBytes());
    put2.addColumn("no".getBytes(), "name".getBytes(), "王伟".getBytes());
    Put put3 = new Put("7".getBytes());
    put3.addColumn("no".getBytes(), "name".getBytes(), "金素荣".getBytes());
    Put put4 = new Put("6".getBytes());
    put4.addColumn("no".getBytes(), "name".getBytes(), "小日本".getBytes());

    List<Put> puts = new ArrayList<>();

    puts.add(put1);
    puts.add(put2);
    puts.add(put3);
    puts.add(put4);

    table.put(puts);
}
第二种实现
public void insertData() throws Exception {
		table.setAutoFlushTo(false);
		table.setWriteBufferSize(534534534);
		ArrayList<Put> arrayList = new ArrayList<Put>();
		for (int i = 21; i < 50; i++) {
			Put put = new Put(Bytes.toBytes("1234"+i));
			put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"+i));
			put.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes(1234+i));
			arrayList.add(put);
		}
		
		//插入数据
		table.put(arrayList);
		//提交
		table.flushCommits();
	}

13.删除数据

新建delete对象参数是rowkey也要转字节,然后table.DELETE(DELETE对象)
public void deleteData() throws IOException {
    Delete d1 = new Delete("1".getBytes());
    Delete d2 = new Delete("2".getBytes());
    Delete d3 = new Delete("3".getBytes());
    Delete d4 = new Delete("4".getBytes());

    List<Delete> deletes = new ArrayList<>();

    deletes.add(d1);
    deletes.add(d2);
    deletes.add(d3);
    deletes.add(d4);

    table.delete(deletes);

14.驱动程序

import java.io.IOException;
import java.util.Base64;

public class Driver {
    public static void main(String[] args) throws IOException {
        Hbase hbaseTest =new Hbase();
        hbaseTest.createConnaction();

        hbaseTest.queryOneNamespace();
        hbaseTest.queryAllTableData();
        hbaseTest.queryAllNamespace();
        hbaseTest.queryAllTable();
        hbaseTest.queryOneTableMetadata();
        hbaseTest.queryTableByNamespace();
        hbaseTest.queryRowKey();
        hbaseTest.createTable();
        hbaseTest.insertOneData();
        hbaseTest.updateTable();

        hbaseTest.queryAllTableData();


        hbaseTest.closeConnaction();
    }
}

15.总结

代码格式,基本就是哪个操作先新建哪个类(需要注意的是大多数类的初始参数是rowkey值,并且所有参数包括rowkey基本都得转bytes格式),比如更新和添加的put类要新建put对象,然后table对象用put函数提交put对象即可,删除先新建delete类,然后table.delete(delete类),所有和表名有关的操作都用TableName.valueof(‘tablename’)

admin类(因该是DDL数据定义语言涉及)涉及命名空间的创建,表的创建

可以Admin admin=conn.getAdmin获取,也可以Admin admin=new Admin(config)获取

table类(DML数据操作语言涉及)比如数据的上传删除修改等。

filter对象就是实现shell操作的参数功能,filterlist就是多个参数的shell,用add方法添加其他filter

包括正则过滤器,可以搭配查询指定类型的rowkey或者列值

rowkey筛选器rowfilter

列值筛选器singleColumnValuefilter

connection类用来获取表等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值