HBase还提供了API,我们可以通过编程的方式来进行对HBase的操作。
1. 环境准备
新建项目后在 pom.xml 中添加依赖:
org.apache.hbasehbase-server1.3.1org.apache.hbasehbase-client1.3.1
2. HBase API
2.1 获取 Configuration 对象
public static Connection connection = null;public static Admin admin = null;static {try {//1. 获取配置信息Configuration configuration = HBaseConfiguration.create();//2. 创建连接对象connection = ConnectionFactory.createConnection(configuration);//3. 创建admin对象admin = connection.getAdmin();}catch (Exception e){e.printStackTrace();}}
2.2 判断表是否存在
//2.判断表明是否存在public static boolean isTableExistByNewFun(String tableName) throws IOException {//3. 判断表是否存在return admin.tableExists(TableName.valueOf(tableName));}
2.3DDL
package com.atguigu.DDL;import com.atguigu.HbaseAPI;import com.atguigu.test;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.Admin;import java.io.IOException;public class createTable {public static void main(String[] args) throws IOException {createTable table = new createTable();System.out.println(HbaseAPI.isTableExistByNewFun("0408:stu5"));//1. 创建表测试,在命名空间0408下面创建表table.createTable("0408:stu5","info1","info2");System.out.println(HbaseAPI.isTableExistByNewFun("0408:stu5"));/*//2.删除表table.dropTable("stu5");*///4.创建命名空间 hbase(main):004:0> list_namespace//NAMESPACE//0408//default//hbase//3 row(s) in 0.3890 secondstable.createNameSpace("0408");//2.关闭资源HbaseAPI.close();}//1.创建表public void createTable (String tableName , String... cfs) throws IOException {//1.判断是否存在列族信息if (cfs.length <= 0){System.out.println("请设置列族信息");return;}//2. 判断表是否存在if (HbaseAPI.isTableExistByNewFun(tableName)){System.out.println(tableName + "表已存在");return;}//2.5 创建表描述器HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));//2.6 循环添加列族信息for (String cf : cfs) {//2.7 创建列族描述器HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);//2.8 添加具体的列族信息hTableDescriptor.addFamily(hColumnDescriptor);}//3.创建表HbaseAPI.admin.createTable(hTableDescriptor);}//2.删除表public void dropTable (String tableName) throws IOException {System.out.println("++++++++++++++++++++表的删除操作++++++++++++++++");//1.先判断表是否存在if (HbaseAPI.isTableExistByNewFun(tableName)){System.out.println(tableName+": 表存在");}else{System.out.println(tableName+":不存在");return;}//2. 使表下线HbaseAPI.admin.disableTable(TableName.valueOf(tableName));//3.删除表HbaseAPI.admin.deleteTable(TableName.valueOf(tableName));}//3. 创建命名空间public void createNameSpace(String ns){//1.创建命令空间描述器NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();//2.创建命名空间try {HbaseAPI.admin.createNamespace(namespaceDescriptor);}catch (NamespaceExistException e){System.out.println(ns+":命名空间已存在");}catch (Exception e){e.printStackTrace();}System.out.println("命名空间存在,方法运行结束");}}
续:
package com.atguigu.DML;import com.atguigu.HbaseAPI;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import java.util.List;public class Table_DML {public static void main(String[] args) throws IOException {Table_DML table = new Table_DML();/*//1.创建数据测试table.putData("stu","1002","info","name","TS");*///2.获取单行数据/*table.getData("stu","1001","info","name");*///3.查询表中的数据/*table.ScanData("stu");*///4.测试删除table.deleteData("stu","1006","info","name");HbaseAPI.close();}//1.向表内插入数据public void putData(String tableName,String rowKey , String cf , String cn , String value) throws IOException {//1.获取表对象Table table = HbaseAPI.connection.getTable(TableName.valueOf(tableName));//2.创建put对象Put put = new Put(Bytes.toBytes(rowKey));//3.给put对象赋值put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));//多个数据插入//table.put(new List);//4.插入数据table.put(put);}//2.获取数据(get)public void getData(String tableName , String rowKey , String cf , String cn) throws IOException {//1.获取表对象Table table = HbaseAPI.connection.getTable(TableName.valueOf(tableName));//2.创建Get对象Get get = new Get(Bytes.toBytes(rowKey));//2.1 指定获取的列族get.addFamily(Bytes.toBytes(cf));//2.2 指定列族和列get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));//2.3 设置获取数据的版本数get.setMaxVersions();//3.获取数据Result result = table.get(get);//4.解析result并打印for (Cell cell : result.rawCells()) {//5.打印数据System.out.println( "CF: " + Bytes.toString(CellUtil.cloneFamily(cell)) +" CN: "+ Bytes.toString(CellUtil.cloneQualifier(cell)) +" Value: "+ Bytes.toString(CellUtil.cloneValue(cell)));}table.close();}//3.获取数据(scan)public void ScanData (String tableName) throws IOException { //1.获取表的连接Table table = HbaseAPI.connection.getTable(TableName.valueOf(tableName));//2. 构建Scan对象Scan scan = new Scan();/*获取范围id的值*///Scan scan = new Scan(Bytes.toBytes("1001"),Bytes.toBytes("1003"));//3.扫描表ResultScanner resultScanner = table.getScanner(scan);//4.解析resultScannerfor (Result result : resultScanner) {//5.打印数据for (Cell cell : result.rawCells()) {//5.打印数据System.out.println( "CF: " + Bytes.toString(CellUtil.cloneFamily(cell)) +" CN: "+ Bytes.toString(CellUtil.cloneQualifier(cell)) +" Value: "+ Bytes.toString(CellUtil.cloneValue(cell)));}}//5.关闭资源table.close();}//4.删除数据/* * @author: 左先生 * @date: 2022-01-15 17:08 * @description:总结: */public void deleteData(String tableName , String rowKey , String cf , String cn) throws IOException {//1.获取表的连接Table table = HbaseAPI.connection.getTable(TableName.valueOf(tableName));//2.构建删除对象Delete delete = new Delete(Bytes.toBytes(rowKey));//2.1 设置删除的列,该设置可以删除指定的info以及列信息,进行单个删除,里面的timestamp参数可加可不加delete.addColumns(Bytes.toBytes(cf),Bytes.toBytes(cn),1642236880388L);//2.2 删除指定的列族//delete.addFamily(Bytes.toBytes(cf));//3.执行删除操作table.delete(delete);//4.关闭连接table.close();}}