6 HBase java API访问HBase数据库
生活随笔
收集整理的這篇文章主要介紹了
6 HBase java API访问HBase数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
HBase java API訪問HBase數據庫
package com.hunan.hbase_options;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;/* DDL: 1、判斷表是否存在 2、創建命名空間 3、創建命名空間 4、刪除表 DML: 5、插入數據 6、查數據(get) 7、查數據(scan) 8、刪除數據*/ public class HbaseDemo {private static Connection connection = null;private static Admin admin = null;static {try {//1、獲取配置文件信息Configuration configuration = HBaseConfiguration.create();configuration.set("hbase.zookeeper.quorum", "master");//hbase-site.xml配置文件中 hbase.zookeeper.quorum 的值//2、創建連接對象connection = ConnectionFactory.createConnection(configuration);//2、獲取admin對象admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}//判斷表是否存在public static boolean isTableExist(String tableName) throws IOException { // //1、獲取配置文件信息 // //HBaseConfiguration configuration=new HBaseConfiguration(); // Configuration configuration = HBaseConfiguration.create(); // configuration.set("hbase.zookeeper.quorum","master"); // //2、獲取管理員對象 // //HBaseAdmin admin=new HBaseAdmin(configuration); // Connection connection = ConnectionFactory.createConnection(configuration); // Admin admin = connection.getAdmin();//第1步和第2步封裝到static方法中//3、判斷表是否存在boolean exists = admin.tableExists(TableName.valueOf(tableName)); // //4、關閉連接 // admin.close();//5、返回結果return exists;}//創建表public static void createTable(String tableName, String... cfs) throws IOException// ... 傳參數個數不確定,可以為0{//1、判斷列族信息是否存在if (cfs.length <= 0) {System.out.println("請設置列族信息");return;}//2、判斷表是否存在if (isTableExist(tableName)) {System.out.println(tableName + "表已經存在");return;}//3、創建表描述器HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));//4、循環添加列族信息for (String cf : cfs) {//5、創建列族描述器HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);//6、添加具體列族hTableDescriptor.addFamily(hColumnDescriptor);}//7、創建表admin.createTable(hTableDescriptor);}//刪除表public static void dropTable(String tableName) throws IOException {//1、判斷表是否存在if (!isTableExist(tableName)) {System.out.println(tableName + "表不存在");return;}//2、使表下線admin.disableTable(TableName.valueOf(tableName));//3、刪除表admin.deleteTable(TableName.valueOf(tableName));}//創建命名空間public static void createNameSpace(String ns) {//1、創建命名空間描述器NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(ns);NamespaceDescriptor namespaceDescriptor = builder.build();//2、創建命名空間try {admin.createNamespace(namespaceDescriptor);} catch (NamespaceExistException e){System.out.println(ns+"命名空間已經存在");} catch (IOException e) {e.printStackTrace();}}//向表插入數據public static void putData(String tableName, String rowKey, String cf,String cn,String value) throws IOException {//1.獲取表對象Table table = 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));//4.插入數據table.put(put);//5.關閉表,不要放在connection中一起關閉table.close();}//6.獲取數據(get)public static void getData(String tableName, String rowKey,String cf,String cn) throws IOException {//1、獲取表對象Table table = 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();//默認最大版本,即獲取所有版本數據//get.setMaxVersions(2);//獲取最新的兩個版本數據//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)));}//5.關閉表連接table.close();}//獲取數據(scan)public static void scanTable(String tableName) throws IOException {//1.獲取表對象Table table = connection.getTable(TableName.valueOf(tableName));//2.創建Scanner對象Scan scan = new Scan();//空參掃描全表//2.1 指定rowkey范圍//Scan scan=new Scan(Bytes.toBytes("002"),Bytes.toBytes("002"));//左閉右開//3.掃描表ResultScanner resultScanner = table.getScanner(scan);//4.解析表for (Result result : resultScanner) {//5.解析result并打印數據for (Cell cell : result.rawCells()) {System.out.println("CF:"+Bytes.toString(CellUtil.cloneFamily(cell))+",CN:"+Bytes.toString(CellUtil.cloneQualifier(cell))+",Value:"+Bytes.toString(CellUtil.cloneValue(cell)));}}//6.關閉表連接table.close();}//刪除數據public static void deleteData(String tableName,String rowKey,String cf,String cn) throws IOException {//1.獲取表Table table = connection.getTable(TableName.valueOf(tableName));//2.構建刪除對象Delete delete = new Delete(Bytes.toBytes(rowKey));//2.1 設置刪除的列//刪除最新版本,如果添加時間戳,刪除指定時間戳的版本,如果沒有則不刪除(不符合日常實際應用,少用)delete.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));//添加刪除標記//刪除所有版本 ,如果添加時間戳。刪除小于時間戳的版本delete.addColumns(Bytes.toBytes(cf),Bytes.toBytes(cn));//直接刪除//2.2 設置刪除的列族delete.addFamily(Bytes.toBytes(cf));//3.執行刪除操作table.delete(delete);//4.關閉連接table.close();}public static void close() throws IOException {if(admin!=null){admin.close();}if(connection!=null){connection.close();}}public static void main(String[] args) throws IOException {//1、測試表是否存在//System.out.println(isTableExist("student"));//2、創建表測試//createTable("student","info1","info2");//3、刪除表測試//dropTable("student");//4、創建命名空間測試//createNameSpace("0202");//createTable("0202:student","info1","info2");//(在命名空間0202上創建表)//5.表插入數據測試//putData("student","001","info1","school","whut");//6.獲取單行數據(根據RowKey)//getData("student","001","info1","school");//7.測試scan表scanTable("student");//8.測試刪除deleteData("student","001","info1","school");//關閉資源close();} }總結
以上是生活随笔為你收集整理的6 HBase java API访问HBase数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ string的使用
- 下一篇: 统计学习的分类