全網最全Hbase入門的增加,查詢,刪除數據詳細解讀和代碼展示

原文鏈接:https://blog.csdn.net/young_so_nice/article/details/51405329

以下是Hbase的入門詳細解讀:
一,從外部的連接上hbase:
1,首先獲取hbase的配置信息—-Configuration。
2,給Configuration設置你的zookeeper以及虛擬機的地址。
3,通過連接工廠ConnectionFactory,創建連接。
4,通過連接拿到管理員即admin用來後面操作hbase。

二,建立表:
1,首先可以通過TableName創建表名對象。
2,通過admin判斷改表是否存在,再做其他操作。
3,如果不存在就創建表結構對象,HTableDescriptor。
4,創建列族,並將列族添加到表結構對象,HColumnDescriptor。
5,使用admin將表創建。

三,向表中添加數據:
1,創建Put,並指定對應的行健。
2,給put指定列族,還可以指定修飾名,如果不指定列族就默認成了修飾名。
3,通過連接拿到表,再通過表將數據插入。

四,scan讀取數據:
1,創建一個scan。
2,通過鏈接拿到表,通過表和scan,取到一個ResultScanner結果集。
3,循環這個結果集,轉換成單個的結果。Result。
4,將這個Result轉換成一個List。
5,循環這個List就可以拿到對應的值了。

五,get讀取數據:
get其實和scan還是很像的,不過通過get是拿到的單個結果,少了一次循環,其他就大致相同了。

六,刪除數據:
這個也簡單了,創建一個Delete,指定對應的行健和列族,修飾名,通過拿到的表就可以完成這步操作了。

詳細代碼展示:
 

import java.util.Arrays;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseApi2 {
    public static void main(String[] args) {
        Admin admin = null;
        Connection con = null;
        try {
            Configuration conf = HBaseConfiguration.create();// 獲取hbase配置文件
            conf.set("hbase.zookeeper.quorum", "192.168.61.128");
            con = ConnectionFactory.createConnection(conf);
            admin=con.getAdmin();//獲得管理員
            TableName tn=TableName.valueOf("scores2");
            if(admin.tableExists(tn)){//判斷表是否存在
                admin.disableTable(tn);//使表失效
                admin.deleteTable(tn);//刪除該表
                System.out.println("你要創建的表已存在,已經將其刪除!");
            }
            System.out.println("要創建的表沒有重複,正在創建!");
            HTableDescriptor htd=new HTableDescriptor(tn);//創建表結構對象
            HColumnDescriptor hcd1=new HColumnDescriptor("grade");//創建列族
            HColumnDescriptor hcd2=new HColumnDescriptor("coures");
            htd.addFamily(hcd1);//將列族添加到表結構
            htd.addFamily(hcd2);
            admin.createTable(htd);//創建表

            System.out.println("創建表成功!");

            //向表中插入數據
            Put put=new Put(Bytes.toBytes("tg"));
            put.addColumn(Bytes.toBytes("grade"),null,Bytes.toBytes("5"));//列族就是列
            put.addColumn(Bytes.toBytes("coures"),Bytes.toBytes("art"),Bytes.toBytes("98"));//在列族下面,創建列
            put.addColumn(Bytes.toBytes("coures"),Bytes.toBytes("math"),Bytes.toBytes("91"));
            Table table=con.getTable(tn);
            table.put(put);

            //向表中批量插入數據
            Put put1=new Put(Bytes.toBytes("tg2"));
            put1.addColumn(Bytes.toBytes("grade"),null,Bytes.toBytes("4"))
            .addColumn(Bytes.toBytes("coures"),Bytes.toBytes("art"),Bytes.toBytes("88"))
            .addColumn(Bytes.toBytes("coures"),Bytes.toBytes("math"),Bytes.toBytes("85"));

            Put put2=new Put(Bytes.toBytes("tg3"));
            put2.addColumn(Bytes.toBytes("grade"),null,Bytes.toBytes("3"))
            .addColumn(Bytes.toBytes("coures"),Bytes.toBytes("art"),Bytes.toBytes("77"))
            .addColumn(Bytes.toBytes("coures"),Bytes.toBytes("math"),Bytes.toBytes("65"));

            List<Put> puts= Arrays.asList(put1,put2);//將數組轉爲集合
            Table table2=con.getTable(tn);
            table2.put(puts);

            //讀取操作
            //scan
            System.out.println("==================scan查詢======================");
            Scan scan=new Scan();
            Table table3=con.getTable(tn);
            ResultScanner resultScanner=table3.getScanner(scan);//獲得scan結果集
            for(Result rs:resultScanner){
                List<Cell> cs=rs.listCells();//將到的每一個結果,轉成list的形式
                for(Cell cell:cs){
                    String rowkey=Bytes.toString(CellUtil.cloneRow(cell));//取到行鍵
                    long timestamp=cell.getTimestamp();//取時間戳
                    String fname=Bytes.toString(CellUtil.cloneFamily(cell));//取到列族名
                    String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));//取修飾名,即列名
                    String value=Bytes.toString(CellUtil.cloneValue(cell)); //取值
                    System.out.println("rowkey=="+rowkey+"---timestamp=="+timestamp+"---qualifier=="+fname+"=>"+qualifier+"---value=="+value);
                }
            }

        //get查詢數據
        System.out.println("================================================================");
        System.out.println("========================get查詢的數據==============================");   
        System.out.println("-----------------------取到的該行所有數據----------------------");   
        Get get=new Get(Bytes.toBytes("tg"));//指定行
        Table table4=con.getTable(tn);
        Result rt=table4.get(get);
        List<Cell> cs=rt.listCells();
        for(Cell cell:cs){
            String rowkey=Bytes.toString(CellUtil.cloneRow(cell));//取到行鍵
            long timestamp=cell.getTimestamp();//取時間戳
            String fname=Bytes.toString(CellUtil.cloneFamily(cell));//取到列族名
            String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));//取修飾名,即列名
            String value=Bytes.toString(CellUtil.cloneValue(cell)); //取值
            System.out.println("rowkey=="+rowkey+"---timestamp=="+timestamp+"---qualifier=="+fname+"=>"+qualifier+"---value=="+value);
        }

        System.out.println("====================get取指定行列數據===================");
        Get get1=new Get(Bytes.toBytes("tg"));
        get1.addColumn(Bytes.toBytes("coures"), Bytes.toBytes("art"));//指定列族和修飾名
        Table table5=con.getTable(tn);
        Result rt1=table5.get(get1);
        List<Cell> cs1=rt1.listCells();
        for(Cell cell:cs1){
            String rowkey=Bytes.toString(CellUtil.cloneRow(cell));//取到行鍵
            long timestamp=cell.getTimestamp();//取時間戳
            String fname=Bytes.toString(CellUtil.cloneFamily(cell));//取到列族名
            String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));//取修飾名,即列名
            String value=Bytes.toString(CellUtil.cloneValue(cell)); //取值
            System.out.println("rowkey=="+rowkey+"---timestamp=="+timestamp+"---qualifier=="+fname+"=>"+qualifier+"---value=="+value);
        }

        //刪除數據
        System.out.println("********************************************************");
        System.out.println("******************************刪除數據******************************");
        System.out.println("刪除tg2這行所有數據");
        Delete delete=new Delete(Bytes.toBytes("tg2"));
        Table table6=con.getTable(tn);
        table6.delete(delete);
        System.out.println("刪除tg3的art列");
        Delete delete1=new Delete(Bytes.toBytes("tg3"));
        delete1.addColumn(Bytes.toBytes("coures"), Bytes.toBytes("art"));
        Table table7=con.getTable(tn);
        table7.delete(delete1);

        System.out.println("========================================================");
        System.out.println("==================刪除操作後的scan查詢======================");
        Scan scan1=new Scan();
        Table table8=con.getTable(tn);
        ResultScanner resultScanner1=table8.getScanner(scan1);//獲得scan結果集
        for(Result rs:resultScanner1){
            List<Cell> cs2=rs.listCells();//將到的每一個結果,轉成list的形式
            for(Cell cell:cs2){
                String rowkey=Bytes.toString(CellUtil.cloneRow(cell));//取到行鍵
                long timestamp=cell.getTimestamp();//取時間戳
                String fname=Bytes.toString(CellUtil.cloneFamily(cell));//取到列族名
                String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));//取修飾名,即列名
                String value=Bytes.toString(CellUtil.cloneValue(cell)); //取值
                System.out.println("rowkey=="+rowkey+"---timestamp=="+timestamp+"---qualifier=="+fname+"=>"+qualifier+"---value=="+value);
            }
        }




            if (admin != null){
                admin.close();
            }
            if(con != null){
                con.close();
            }


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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章