Java操作HbaseAPI大全(增删改查等操作)
作者:互联网
博主直接放代码,需要的小伙伴自行选择学习哈
maven为:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.4.13</version>
</dependency>
import org.apache.hadoop.hbase.*; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.client.*; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.client.Table; /** * @Author 海龟 * @Date 2020/10/26 11:17 * @Desc HbaseAPI操作 */ public class Hbase_test { static Configuration configuration = new CompoundConfiguration(); static { configuration = HBaseConfiguration.create(); //创建HBase配置对象 configuration.set("hbase.rootdir", "hdfs://master:9000/hbase"); //hbase 根目录 configuration.set("hbase.master", "hdfs://master:60000"); //hbase 主节点端口 configuration.set("hbase.zookeeper.property.clientPort", "2181"); //zookeeper端口 configuration.set("hbase.zookeeper.quorum", "master,slave1,slave2"); //zookeeper服务 } /* * 创建表 * create 'tablename','cf1','cf2' */ public static int creatTable(String tableName, String[] family) throws Exception { //1.初始化连接对象 Admin //2.初始化表信息对象 HTableDescripto //3.循环创建列 设置最大版本数 表添加列addFamily //4.判断表是否存在 tableExists //5.创建表对象并把表信息对象添加到表对象里 createTable //6.关闭连接 //Admin admin = new HbaseAdmin(configuration); Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName)); for (String str : family) { HColumnDescriptor column = new HColumnDescriptor(str); column.setMaxVersions(3); //设置最大版本数 column.setTimeToLive(1000); //设置存活时间 table.addFamily(column); //添加列族 } //判断表是否存在,存在返回-1 if (admin.tableExists(TableName.valueOf(tableName))) { System.out.println(tableName + "已存在"); return -1; } admin.createTable(table); admin.close(); System.out.println("create success 成功"); return 1; } /* * 查询所有表(list) */ public static void catTable() throws Exception { //1.初始化连接对象 Admin //2.初始化表对象列表 listTables //3.循环输出下标值 //4.关闭连接 Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); HTableDescriptor[] hTableDescriptors = admin.listTables(); for (int i = 0; i <= hTableDescriptors.length; i++) { System.out.println(hTableDescriptors[i].toString()); } admin.close(); } /* 增加一个列族 */ public static void addColumnOne(String tableName, String ColumnFamily) throws Exception { //1.创建连接对象 //2.创建列信息对象 //3.启用表 //4.添加列族 //5.关闭连接 Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); HColumnDescriptor column = new HColumnDescriptor(Bytes.toBytes(ColumnFamily)); if (admin.isTableEnabled(TableName.valueOf(tableName)) == false) { admin.enableTable(TableName.valueOf(tableName)); } admin.addColumn(TableName.valueOf(tableName), column); column.setTimeToLive(3); column.setMaxVersions(2); System.out.println("创建列族成功!!"); admin.close(); } /* * 删除表 */ public static int deleteTable(String tableName) throws Exception { //1.初始化连接对象 Admin //2.禁用表 disableTable //3.删除表 deleteTable //4.判断是否删除 //5.关闭连接 //HBaseAdmin admin = new HBaseAdmin(configuration); Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); if (admin.isTableEnabled(TableName.valueOf(tableName)) == true) { admin.disableTable(TableName.valueOf(tableName)); } admin.deleteTable(TableName.valueOf(tableName)); if (admin.tableExists(TableName.valueOf(tableName)) == false) { System.out.println(tableName + "已删除"); return 1; } admin.close(); return -1; } /* 删除一个列族,列值(第一种方法) */ public static void deleteOreations1(String tableName, String ColumnFamily) throws Exception { Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); if (admin.isTableEnabled(TableName.valueOf(tableName)) == true) { admin.disableTable(TableName.valueOf(tableName)); } admin.deleteColumn(TableName.valueOf(tableName), Bytes.toBytes(ColumnFamily)); admin.close(); System.out.println("删除成功!!"); } /* 删除一个列族,列值(第二种方法) */ public static void deleteOreations2(String tableName,String rowKey, String ColumnFamily,String ColumnName,String value) throws Exception { Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); if(admin.isTableEnabled(TableName.valueOf(tableName))==false){ admin.enableTable(TableName.valueOf(tableName)); } Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumn(Bytes.toBytes(ColumnFamily),Bytes.toBytes(ColumnName)); //无版本数(时间戳) // // // delete.addColumn(Bytes.toBytes(ColumnFamily),Bytes.toBytes(ColumnName),);//有版本数(时间戳) boolean b = table.checkAndDelete(Bytes.toBytes(rowKey),Bytes.toBytes(ColumnFamily) ,Bytes.toBytes(ColumnName),Bytes.toBytes(value),delete); table.delete(delete); System.out.println("Delete STATUS:" + b); } /* 插入数据(put 'tablename','rowkey','familyname:key','value') */ public static void putTable_Data(String tableName, String rowKey, String ColumnsFamily, String ColumnName, String value) throws Exception { //1.初始化连接对象 Admin //2.初始化表对象指定表 Table //3.初始化put操作指定行键 Put //4.put方法添加数据 addColumn //5.table对象提交Put对象 //6.关闭连接 Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); if(admin.isTableDisabled(TableName.valueOf(tableName)) == true){ admin.enableTable(TableName.valueOf(tableName)); } Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(ColumnsFamily), Bytes.toBytes(ColumnName), Bytes.toBytes(value)); table.put(put); table.close(); admin.close(); } /* * 追加某列的值 */ public static void append(String tableName, String rowkey , String ColumnFamily, String ColumnName, String value) throws Exception { Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); if (admin.isTableEnabled(TableName.valueOf(tableName)) == false) { admin.enableTable(TableName.valueOf(tableName)); } Append append = new Append(Bytes.toBytes(rowkey)); append.add(Bytes.toBytes(ColumnFamily), Bytes.toBytes(ColumnName), Bytes.toBytes(value)); table.append(append); admin.close(); table.close(); } /* * 插入批量插入 Put<list> * */ public static void putTables_Date(String tableName, String rowKey, String ColumnFamile) throws Exception { //1.初始化连接对象 Admin //2.通过Arraylist构造List<Put>容器 //3.调用tableExists 判断表是否存在,存在就循环插入数据 //4.put方法添加数据 //5.关闭连接 Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); List<Put> putList = new ArrayList<Put>(); if (admin.tableExists(TableName.valueOf(tableName)) == true) { for (int i = 0; i <= 10; i++) { Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(ColumnFamile), Bytes.toBytes("ColumnName" + i), Bytes.toBytes("haigui" + i)); putList.add(put); } } else { System.out.println("该表不存在"); } table.put(putList); table.close(); admin.close(); } /* 检查和插入数据 */ public static void checkAndPut(String tableName, String rowKey, String ColumnFamile, String ColumnName, String value) throws Exception { //1.初始化连接对象 Admin //2.判断表是否存在,存在就创建表对象,初始化表对象和Put对象,put添加数据 //3.调用checkAndPut方法,check一下,如果改列有没有值,则执行put操作 Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); if (admin.tableExists(TableName.valueOf(tableName)) == true) { Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(ColumnFamile), Bytes.toBytes(ColumnName), Bytes.toBytes(value)); //put boolean test1 = table.checkAndPut(Bytes.toBytes(rowKey), Bytes.toBytes(ColumnFamile) , Bytes.toBytes(ColumnName), Bytes.toBytes(value), put); System.out.println("Put STATUS:" + test1); } } /* 根据rowkey查询 get 'tablename','rowkey' */ public static void RowskeysCat(String tableName, String rowsKey) throws Exception { //1.初始化表对象指定表 Table //2.初始化Get对象 Get //3.调用get方法 get //4.输出返回值 Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowsKey)); System.out.println("---------------旧方法(已弃用)---------------"); Result result1 = table.get(get); List<KeyValue> values = result1.list(); for (KeyValue value : values ) { //列族 byte[] columsFamily = value.getFamily(); String familyStr = Bytes.toString(columsFamily); //列限定符 byte[] colums = value.getQualifier(); String columsStr = Bytes.toString(colums); //值 byte[] valueValue = value.getFamily(); String valueStr = Bytes.toString(valueValue); //时间戳 Long TimeStampValue = value.getTimestamp(); String ts = String.valueOf(TimeStampValue); System.out.print(familyStr + ":"); System.out.print(columsStr + "\t\t"); System.out.print("value= " + valueStr + " ,"); System.out.println("TimeStamp = " + ts); } System.out.println("---------------新方法-------------------"); Result result2 = table.get(get); Cell[] cells = result2.rawCells(); for (Cell cell : cells ) { //列族 byte[] columsFamily = CellUtil.cloneFamily(cell); String familyStr = Bytes.toString(columsFamily); //列限定符 byte[] colums = CellUtil.cloneQualifier(cell); String columsStr = Bytes.toString(colums); //值 byte[] valueValue = CellUtil.cloneValue(cell); String valueStr = Bytes.toString(valueValue); //时间戳 Long TimeStampValue = cell.getTimestamp(); String ts = String.valueOf(TimeStampValue); System.out.print(familyStr + ":"); System.out.print(columsStr + "\t\t"); System.out.print("value= " + valueStr + " ,"); System.out.println("TimeStamp = " + ts); } table.close(); } /* * 查询指定的列 get 'person','tableName','cf1:name' */ public static void getResule_Column(String tableName, String rowKey, String ColumsFamily, String ColumnName) throws Exception { Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(ColumsFamily), Bytes.toBytes(ColumnName)); Result result = table.get(get); // byte[] columnsfamily = result.getFamilyMap(Bytes.toBytes(rowKey)); byte[] data1 = result.getValue(Bytes.toBytes(ColumsFamily), Bytes.toBytes(ColumnName)); // List<KeyValue> column = result.getColumn(Bytes.toBytes(ColumsFamily), Bytes.toBytes(ColumnName)); // System.out.println(Bytes.toString(k.get)); //System.out.println("" + Bytes.toString(data1)); // for (KeyValue columns:column // ) { // System.out.println( Bytes.toString(columns.getValue())); // } System.out.println("" + Bytes.toString(data1)); } /* * 修改列族的属性 */ public static void AlterColumnsFamily(String tableName, String ColumnsFamily) throws Exception { Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); if (admin.isTableEnabled(TableName.valueOf(tableName)) == true) { admin.disableTable(TableName.valueOf(tableName)); } HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(ColumnsFamily); //数据保存的最大版本数 hColumnDescriptor.setMaxVersions(3); //数据保存的最长时间 // hColumnDescriptor.setTimeToLive(1000); //将修改后的描述对象应用到表 admin.modifyColumn(TableName.valueOf(tableName),hColumnDescriptor); admin.close(); } /* 让某列的数字加1 */ public static void IncreamentColumn(String tableName, String rowKey, String ColumsFamily, String ColumnName, Long value) throws Exception { Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); if (admin.isTableEnabled(TableName.valueOf(tableName)) == false) { admin.enableTable(TableName.valueOf(tableName)); } //添加一个列 Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(ColumsFamily), Bytes.toBytes(ColumnName), Bytes.toBytes(value)); table.put(put); //给列的值值 Increment increment = new Increment(Bytes.toBytes(rowKey)); increment.addColumn(Bytes.toBytes(ColumsFamily), Bytes.toBytes(ColumnName), 10L); table.increment(increment); table.close(); admin.close(); } /* 判断数据是否存在 */ public static void isValueExist(String tableName, String rowkey,String ColumnsFamily,String ColumnsName ) throws Exception { Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); // boolean result = table.exists(get); if(admin.isTableEnabled(TableName.valueOf(tableName)) == false){ admin.enableTable(TableName.valueOf(tableName)); } Get get = new Get(Bytes.toBytes(rowkey)); Result result = table.get(get); byte[] value = result.getValue(Bytes.toBytes(ColumnsFamily), Bytes.toBytes(ColumnsName)); // System.out.println(Bytes.toString(value)); if( value != null){ System.out.println("删除成功!!"); } else { System.out.println("该表的值不存在"); } } /* 遍历表输出数据 */ public static void ScanTable(String tableName,String rowkey, String ColumnsFamily,String ColumnsName)throws Exception { Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); Scan scan = new Scan(Bytes.toBytes(rowkey)); //查询指定到列的值 //scan.addColumn(Bytes.toBytes(ColumnsFamily), Bytes.toBytes(ColumnsName)); scan.addFamily(Bytes.toBytes(ColumnsFamily)); ResultScanner scanner = table.getScanner(scan); for (Result results : scanner ) { Cell[] cell = results.rawCells(); for (Cell cells : cell ) { //列族 byte[] columsFamily = CellUtil.cloneFamily(cells); String familyStr = Bytes.toString(columsFamily); //列限定符 byte[] colums = CellUtil.cloneQualifier(cells); String columsStr = Bytes.toString(colums); //值 byte[] valueValue = CellUtil.cloneValue(cells); String valueStr = Bytes.toString(valueValue); //时间戳 Long TimeStampValue = cells.getTimestamp(); String ts = String.valueOf(TimeStampValue); System.out.print(familyStr + ":"); System.out.print(columsStr + "\t\t"); System.out.print("value= " + valueStr + " ,"); System.out.println("TimeStamp = " + ts); } } } /* 遍历操作 */ public static void ScanOreations(String tableName,String rowKey,String ColumnsFamily, String ColumnsName)throws Exception { Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); if(admin.isTableDisabled(TableName.valueOf(tableName))==true){ admin.enableTable(TableName.valueOf(tableName)); } //扫描整个表 // Scan scan = new Scan(); // ResultScanner tableInfo = table.getScanner(scan); // for (Result result:tableInfo // ) { // System.out.println(result.toString()); // } // tableInfo.close(); //扫描表的某个列族 // Scan rkscan = new Scan(Bytes.toBytes(rowKey)); // Scan cfscan = rkscan.addFamily(Bytes.toBytes(ColumnsFamily)); // ResultScanner colfamilyInfos = table.getScanner(cfscan); // for (Result colfamilyInfo:colfamilyInfos // ) { // System.out.println(colfamilyInfo.toString()); // } // colfamilyInfos.close(); //扫描某个列 // Scan scan = new Scan(Bytes.toBytes(rowKey)); // Scan cnscan = scan.addColumn(Bytes.toBytes(ColumnsFamily), Bytes.toBytes(ColumnsName)); // ResultScanner cnInfos = table.getScanner(cnscan); // for (Result cnInfo:cnInfos // ) { // System.out.println(cnInfo); // } // } //扫描值 // Scan scan = new Scan(Bytes.toBytes(rowKey)); // Scan scan1 = scan.addFamily(Bytes.toBytes(ColumnsFamily)); // ResultScanner scanner = table.getScanner(scan1); // for (Result scand : scanner // ) { // Cell[] cells = scand.rawCells(); // for (Cell cell : cells // ) { // byte[] cf = CellUtil.cloneFamily(cell); // byte[] cn = CellUtil.cloneQualifier(cell); // byte[] bytes = CellUtil.cloneValue(cell); // long timestamp = cell.getTimestamp(); // // System.out.print(Bytes.toString(cf) + ":"); // System.out.print(Bytes.toString(cn) + "\t\t"); // System.out.print("value= " + Bytes.toString(bytes) + " ,"); // System.out.println("TimeStamp = " + timestamp); // } // } /* 扫描全表历史版本数据 */ Scan scan = new Scan(); Scan scan1 = scan.setMaxVersions(); ResultScanner scanner = table.getScanner(scan1); for (Result data:scanner ) { String[] line = data.toString().split(","); for (String i:line ) { System.out.println(i); } } } /* 扫描全表并设置起始结束行键 */ public static void scanStart_Stop(String tableName,String start_rowkey,String stop_rowkey)throws Exception{ Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); if(admin.isTableDisabled(TableName.valueOf(tableName))==true){ admin.enableTable(TableName.valueOf(tableName)); } Scan scan = new Scan(); scan.withStartRow(Bytes.toBytes(start_rowkey),false); scan.withStopRow(Bytes.toBytes(stop_rowkey),true); ResultScanner scanner = table.getScanner(scan); for (Result s:scanner ) { String[] split = s.toString().split(","); for (String line:split ) { System.out.println(line); } } } /* 查询多版本数 */ public static void getVersions(String tableName,String rowkey)throws Exception{ Get get = new Get(rowkey.getBytes()); get.setMaxVersions(3); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); Result result = table.get(get); Cell[] cell = result.rawCells(); for (Cell cells : cell ) { //列族 byte[] columsFamily = CellUtil.cloneFamily(cells); String familyStr = Bytes.toString(columsFamily); //列限定符 byte[] colums = CellUtil.cloneQualifier(cells); String columsStr = Bytes.toString(colums); //值 byte[] valueValue = CellUtil.cloneValue(cells); String valueStr = Bytes.toString(valueValue); //时间戳 Long TimeStampValue = cells.getTimestamp(); String ts = String.valueOf(TimeStampValue); System.out.print(familyStr + ":"); System.out.print(columsStr + "\t\t"); System.out.print("value= " + valueStr + " ,"); System.out.println("TimeStamp = " + ts); } } /* 指定条件查询 */ public static void getWhere(String tableName,String rowKey,String ColumnsFamily, String ColumnsName)throws Exception{ Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("rw001"))); scan.setFilter(rowFilter); ResultScanner scanner = table.getScanner(scan); for (Result result:scanner ) { for (Cell cells:result.rawCells() ) { //列族 byte[] columsFamily = CellUtil.cloneFamily(cells); String familyStr = Bytes.toString(columsFamily); //列限定符 byte[] colums = CellUtil.cloneQualifier(cells); String columsStr = Bytes.toString(colums); //值 byte[] valueValue = CellUtil.cloneValue(cells); String valueStr = Bytes.toString(valueValue); //时间戳 Long TimeStampValue = cells.getTimestamp(); String ts = String.valueOf(TimeStampValue); System.out.print(familyStr + ":"); System.out.print(columsStr + "\t\t"); System.out.print("value= " + valueStr + " ,"); System.out.println("TimeStamp = " + ts); } } } /* 统计行 */ public static void rowCount(String tableName)throws IOException{ int rowCount = 0; Table table = ConnectionFactory.createConnection(configuration).getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); scan.setFilter(new FirstKeyOnlyFilter()); ResultScanner scanner = table.getScanner(scan); for (Result result:scanner ) { rowCount += result.size(); } System.out.println(rowCount); } public static void main(String[] args) throws Exception { //创建表 //creatTable("WordCount", new String[]{"cf"}); //查看所有表 //catTable(); //删除表 //deleteTable("stu2018135204"); //插入数据 //putTable_Data("WordCount", "1", "cf","word", "hadoop,storm,spark"); //插入多行表 //putTables_Date("haigui","rk001", "cf1"); //putTables_Date("haigui", "rk001"); //按行查询表 //RowskeysCat("haigui", "rk001"); getResule_Column("score", "Lucy", "score", "English"); //检查并插入 //checkAndPut("jjl","rk001","cf", "name","haigui"); //添加列 //append("haigui","rk001","cf2","age","1"); //添加一个列族 //addColumnOne("haigui","cf2"); //删除一个列族 //deleteOreations1("haigui","age"); //deleteOreations2("score", "Lilly", "score","English", "86"); //修改列族属性 //AlterColumnsFamily("score","score"); //让值加1 //IncreamentColumn("haigui","rk001","cf3","age",1L); //判断数据是否存在 //isValueExist("haigui","rk001","cf1","name"); //遍历操作 //ScanTable("haigui","rk001","cf2","age"); //ScanOreations("haigui","rk001","cf1","name"); //scanStart_Stop("student1","1001","1002"); //getVersions("haigui","cf2"); //rowCount("score"); } }
标签:tableName,Java,String,admin,改查,valueOf,Bytes,HbaseAPI,toBytes 来源: https://blog.csdn.net/guihua55/article/details/112634205