JAVA操作Hbase

實際開發(fā)中可以利用javaAPI去操作控制Hbase

【準備】
1:開啟集群,一次開啟(zookeeper,hdfs,hbase)
2:創(chuàng)建項目,導入jar
3:代碼實現(xiàn)

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.util.Bytes;
import org.junit.Before;
import org.junit.Test;

public class HbaseDemo {
    private Configuration conf = null;
    
    @Before
    public void init(){
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop2004:2181,hadoop2005:2181,hadoop2006:2181");
    }
    
    @Test
    public void testPut() throws Exception{
        HTable table = new HTable(conf, "tab_dog");
        Put put = new Put(Bytes.toBytes("rk0001"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("旺財"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("3"));
        put.add(Bytes.toBytes("info"),Bytes.toBytes("sex"),Bytes.toBytes("boy"));
        table.put(put);
        table.close();
    }

    @Test
    public void testPutAll() throws Exception{
        HTable table = new HTable(conf, "tab_dog");
        List<Put> puts = new ArrayList<Put>(10000);
        for(int i=0 ; i<1000000; i++){
            Put put = new Put(Bytes.toBytes("rk"+i));
            put.add(Bytes.toBytes("info"), Bytes.toBytes("sal"), Bytes.toBytes(""+i));
            puts.add(put);
            //這里防止數(shù)據(jù)量過大,內存溢出,所以10000條就插入一次,分批插入
            if(i % 10000 == 0){
                table.put(puts);
                puts = new ArrayList<Put>(10000);
            }
        }
        table.put(puts);
        table.close();
    }
    
    @Test
    public void testGet() throws Exception{
        //HTablePool pool = new HTablePool(conf, 10);
        //HTable table = (HTable) pool.getTable("user");
        HTable table = new HTable(conf, "tab_dog");
        Get get = new Get(Bytes.toBytes("rk0001"));
        //get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
        get.setMaxVersions(5);
        Result result = table.get(get);
        //result.getValue(family, qualifier)
        
        for(KeyValue kv: result.list()){
            String family = new String(kv.getFamily());
            String qualifier = new String(kv.getQualifier());
            String value = new String(kv.getValue());
            System.out.println(family + " " + qualifier + " : "+ value);            
        }
        table.close();
    }
    
    @Test
    public void testScan() throws Exception{
        HTablePool pool = new HTablePool(conf,1000);
        HTableInterface table = pool.getTable("user");
        Scan scan = new Scan(Bytes.toBytes("rk0001"),Bytes.toBytes("rk0002"));
        scan.addFamily(Bytes.toBytes("info"));
        ResultScanner scanner = table.getScanner(scan);
        for(Result r : scanner){
            byte[] name = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
            byte[] age = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
            byte[] sex = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("sex"));
            System.out.println(new String(name) + " : " + new String(age) + " : " + new String(sex));
        }
        pool.close();
    }
    
    @Test
    public void testDel() throws Exception{
        HTable table = new HTable(conf,"tab_dog");
        Delete del = new Delete(Bytes.toBytes("rk0001"));
        del.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
        table.delete(del);
        table.close();
    }
    
    @Test
    public void testDrop() throws Exception{
        HBaseAdmin admin = new HBaseAdmin(conf);
        admin.disableTable("tab_dog");
        admin.deleteTable("tab_dog");
        admin.close();
    }

    @Test
    public void testCreate() throws Exception {     
        //設置操作用戶(建表要有用戶,誰建的)
        HBaseAdmin admin = new HBaseAdmin(conf);        
        //創(chuàng)建表信息(表名)
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("tab_dog"));
        //創(chuàng)建列族
        HColumnDescriptor cd = new HColumnDescriptor("info");
        cd.setMaxVersions(10);
        //添加列族
        desc.addFamily(cd); 
        //創(chuàng)建表
        admin.createTable(desc);
        admin.close();
    }
}
2016-12-23_175418.png
/*
 * 創(chuàng)建一個students表,并進行相關操作
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.util.Bytes;
 
public class HBaseJavaAPI {
    // 聲明靜態(tài)配置
    private static Configuration conf = null;
 
    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.6.91");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
    }
 
    //判斷表是否存在
    private static boolean isExist(String tableName) throws IOException {
        HBaseAdmin hAdmin = new HBaseAdmin(conf);
        return hAdmin.tableExists(tableName);
    }
 
    // 創(chuàng)建數(shù)據(jù)庫表
    public static void createTable(String tableName, String[] columnFamilys)
            throws Exception {
        // 新建一個數(shù)據(jù)庫管理員
        HBaseAdmin hAdmin = new HBaseAdmin(conf);
        if (hAdmin.tableExists(tableName)) {
            System.out.println("表 "+tableName+" 已存在!");
            System.exit(0);
        } else {
            // 新建一個students表的描述
            HTableDescriptor tableDesc = new HTableDescriptor(tableName);
            // 在描述里添加列族
            for (String columnFamily : columnFamilys) {
                tableDesc.addFamily(new HColumnDescriptor(columnFamily));
            }
            // 根據(jù)配置好的描述建表
            hAdmin.createTable(tableDesc);
            System.out.println("創(chuàng)建表 "+tableName+" 成功!");
        }
    }
 
    // 刪除數(shù)據(jù)庫表
    public static void deleteTable(String tableName) throws Exception {
        // 新建一個數(shù)據(jù)庫管理員
        HBaseAdmin hAdmin = new HBaseAdmin(conf);
        if (hAdmin.tableExists(tableName)) {
            // 關閉一個表
            hAdmin.disableTable(tableName);
            hAdmin.deleteTable(tableName);
            System.out.println("刪除表 "+tableName+" 成功!");
        } else {
            System.out.println("刪除的表 "+tableName+" 不存在!");
            System.exit(0);
        }
    }
 
    // 添加一條數(shù)據(jù)
    public static void addRow(String tableName, String row,
            String columnFamily, String column, String value) throws Exception {
        HTable table = new HTable(conf, tableName);
        Put put = new Put(Bytes.toBytes(row));// 指定行
        // 參數(shù)分別:列族、列、值
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
                Bytes.toBytes(value));
        table.put(put);
    }
 
    // 刪除一條(行)數(shù)據(jù)
    public static void delRow(String tableName, String row) throws Exception {
        HTable table = new HTable(conf, tableName);
        Delete del = new Delete(Bytes.toBytes(row));
        table.delete(del);
    }
 
    // 刪除多條數(shù)據(jù)
    public static void delMultiRows(String tableName, String[] rows)
            throws Exception {
        HTable table = new HTable(conf, tableName);
        List<Delete> delList = new ArrayList<Delete>();
        for (String row : rows) {
            Delete del = new Delete(Bytes.toBytes(row));
            delList.add(del);
        }
        table.delete(delList);
    }
 
    // 獲取一條數(shù)據(jù)
    public static void getRow(String tableName, String row) throws Exception {
        HTable table = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(row));
        Result result = table.get(get);
        // 輸出結果,raw方法返回所有keyvalue數(shù)組
        for (KeyValue rowKV : result.raw()) {
            System.out.print("行名:" + new String(rowKV.getRow()) + " ");
            System.out.print("時間戳:" + rowKV.getTimestamp() + " ");
            System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
            System.out.print("列名:" + new String(rowKV.getQualifier()) + " ");
            System.out.println("值:" + new String(rowKV.getValue()));
        }
    }
 
    // 獲取所有數(shù)據(jù)
    public static void getAllRows(String tableName) throws Exception {
        HTable table = new HTable(conf, tableName);
        Scan scan = new Scan();
        ResultScanner results = table.getScanner(scan);
        // 輸出結果
        for (Result result : results) {
            for (KeyValue rowKV : result.raw()) {
                System.out.print("行名:" + new String(rowKV.getRow()) + " ");
                System.out.print("時間戳:" + rowKV.getTimestamp() + " ");
                System.out.print("列族名:" + new String(rowKV.getFamily()) + " ");
                System.out
                        .print("列名:" + new String(rowKV.getQualifier()) + " ");
                System.out.println("值:" + new String(rowKV.getValue()));
            }
        }
    }
 
    // 主函數(shù)
    public static void main(String[] args) {
        try {
            String tableName = "student";
            // 第一步:創(chuàng)建數(shù)據(jù)庫表:“student”
            String[] columnFamilys = { "info", "course" };
            HBaseJavaAPI.createTable(tableName, columnFamilys);
            // 第二步:向數(shù)據(jù)表的添加數(shù)據(jù)
            // 添加第一行數(shù)據(jù)
            if (isExist(tableName)) {
                HBaseJavaAPI.addRow(tableName, "zpc", "info", "age", "20");
                HBaseJavaAPI.addRow(tableName, "zpc", "info", "sex", "boy");
                HBaseJavaAPI.addRow(tableName, "zpc", "course", "china", "97");
                HBaseJavaAPI.addRow(tableName, "zpc", "course", "math", "128");
                HBaseJavaAPI.addRow(tableName, "zpc", "course", "english", "85");
                // 添加第二行數(shù)據(jù)
                HBaseJavaAPI.addRow(tableName, "henjun", "info", "age", "19");
                HBaseJavaAPI.addRow(tableName, "<span style="font-family: Arial, Helvetica, sans-serif;">henjun</span>", "info", "sex", "boy");
                HBaseJavaAPI.addRow(tableName, "henjun", "course", "china","90");
                HBaseJavaAPI.addRow(tableName, "henjun", "course", "math","120");
                HBaseJavaAPI.addRow(tableName, "henjun", "course", "english","90");
                // 添加第三行數(shù)據(jù)
                HBaseJavaAPI.addRow(tableName, "niaopeng", "info", "age", "18");
                HBaseJavaAPI.addRow(tableName, "<span style="font-family: Arial, Helvetica, sans-serif;">niaopeng</span>", "info", "sex","girl");
                HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "china","100");
                HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "math","100");
                HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "english","99");
                // 第三步:獲取一條數(shù)據(jù)
                System.out.println("**************獲取一條(zpc)數(shù)據(jù)*************");
                HBaseJavaAPI.getRow(tableName, "zpc");
                // 第四步:獲取所有數(shù)據(jù)
                System.out.println("**************獲取所有數(shù)據(jù)***************");
                HBaseJavaAPI.getAllRows(tableName);
 
                // 第五步:刪除一條數(shù)據(jù)
                System.out.println("************刪除一條(zpc)數(shù)據(jù)************");
                HBaseJavaAPI.delRow(tableName, "zpc");
                HBaseJavaAPI.getAllRows(tableName);
                // 第六步:刪除多條數(shù)據(jù)
                System.out.println("**************刪除多條數(shù)據(jù)***************");
                String rows[] = new String[] { "qingqing","xiaoxue" };
                HBaseJavaAPI.delMultiRows(tableName, rows);
                HBaseJavaAPI.getAllRows(tableName);
                // 第七步:刪除數(shù)據(jù)庫
                System.out.println("***************刪除數(shù)據(jù)庫表**************");
                HBaseJavaAPI.deleteTable(tableName);
                System.out.println("表"+tableName+"存在嗎?"+isExist(tableName));
            } else {
                System.out.println(tableName + "此數(shù)據(jù)庫表不存在!");
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容