1. 程式人生 > >HBase API操作

HBase API操作

1.環境準備

    新建專案後在pom.xml中新增依賴:

<dependency>

    <groupId>org.apache.hbase</groupId>

    <artifactId>hbase-server</artifactId>

    <version>1.3.1</version>

</dependency>



<dependency>

    <groupId>org.apache.hbase</groupId>

    <artifactId>hbase-client</artifactId>

    <version>1.3.1</version>

</dependency>



<dependency>

    <groupId>jdk.tools</groupId>

    <artifactId>jdk.tools</artifactId>

    <version>1.8</version>

    <scope>system</scope>

    <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>

</dependency>

2.HBaseAPI

public class HbaseDemo {



    public static Configuration conf;

    static private Admin admin;

    static private Connection connection = null;



    static {

        //使用HBaseConfiguration的單例方法例項化

        conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum", "10.211.55.102");

        conf.set("hbase.zookeeper.property.clientPort", "2181");

        try {

            //獲取連線

            connection = ConnectionFactory.createConnection(conf);

        } catch (IOException e) {

            e.printStackTrace();

        }



        try {

            admin = connection.getAdmin();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }





    public static void close() throws IOException {

        connection.close();

        admin.close();

    }



    /**

     * 1.判斷表名是否存在

     *

     * @param tableName

     * @return

     */

    public static boolean isTableExist(String tableName) throws IOException {

        boolean b = admin.tableExists(TableName.valueOf(tableName));

        System.out.println(b);

        return b;

    }





    /**

     * 2.建立表

     */

    public static void createTable(String tableName, String... columeFamily) throws IOException {

        if (isTableExist(tableName)) {

            System.out.println("表" + tableName + "已經存在!");

        } else {

            HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

            for (String cf : columeFamily) {

                hTableDescriptor.addFamily(new HColumnDescriptor(cf));

            }

            admin.createTable(hTableDescriptor);

        }

    }





    /**

     * 3.刪除表

     */

    public static void delTable(String tableName) throws IOException {

        if (isTableExist(tableName)) {

            TableName name = TableName.valueOf(tableName);

            admin.disableTable(TableName.valueOf(tableName));

            admin.deleteTable(TableName.valueOf(tableName));

        } else {

            System.out.println("表" + tableName + "已經存在");

        }

    }





    /**

     * 4.插入一條資料

     */

    public static void createData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {

        //獲取表的物件

        HTable hTable = new HTable(TableName.valueOf(tableName), connection);

        //獲取put物件

        Put put = new Put(Bytes.toBytes(rowKey));

        //向put中插入資料

        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));

        //向表中插入資料

        hTable.put(put);

        //關閉資源

        hTable.close();

    }





    /**

     * 5.插入多條資料

     */

    public static void createsTable(String tableName, String rowKey, String cf, String[] cn, String[] value) throws IOException {

        //獲取表物件

        Table table = connection.getTable(TableName.valueOf(tableName));

        //獲取put物件

        Put put = new Put(Bytes.toBytes(rowKey));

        //插入多條資料

        for (int i = 0; i < cn.length; i++) {

            put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn[i]), Bytes.toBytes(value[i]));

        }

        table.put(put);

        table.close();



    }





    /**

     * 6.獲取一條資料

     */

    public static void getData(String tableName,String rowkey) throws IOException {

        //獲取表的物件

        Table table = connection.getTable(TableName.valueOf(tableName));

        //獲取get物件

       Get get =  new Get(Bytes.toBytes(rowkey));

       //設定最大的版本數量

        get.setMaxVersions(2);

        //執行獲取資料的操作

        Result result = table.get(get);

        Cell[] cells = result.rawCells();

        for (Cell cell:cells) {

            System.out.println("rowKey:"+rowkey+";"+

                    "ColumnFamily:"+Bytes.toString(CellUtil.cloneFamily(cell))+";"+

                    "ColumnName:"+Bytes.toString(CellUtil.cloneQualifier(cell))+";"+

                    "values:"+Bytes.toString(CellUtil.cloneValue(cell))+";"+

                    "TS:"+cell.getTimestamp());

        }

    }





    /**

     * 7.獲取指定列簇的值

     */

    private static void getRowByCF(String tableName, String rowkey, String cf, String cn) throws IOException {

        //獲取表物件

        Table hTable = connection.getTable(TableName.valueOf(tableName));

        //獲取get物件

        Get get = new Get(Bytes.toBytes(rowkey));

        get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));

        //執行獲取資料操作

        Result result = hTable.get(get);

        Cell[] cells = result.rawCells();



        for (Cell cell : cells) {

            System.out.println(

                    "RowKey:" + rowkey +

                            ",ColumnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)) +

                            ",ColumnName:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +

                            ",Value:" + Bytes.toString(CellUtil.cloneValue(cell)) +

                            ",TS:" + cell.getTimestamp());

        }

    }



    /**

     * 8.掃描一張表

     */

    public static void scanTable(String tableName) throws IOException {

        //獲取表物件

        Table hTable = connection.getTable(TableName.valueOf(tableName));



        Scan scan = new Scan();

        ResultScanner scanner = hTable.getScanner(scan);



        for (Result result : scanner) {

            Cell[] cells = result.rawCells();

            for (Cell cell : cells) {

                System.out.println(

                        "RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)) +

                                ",ColumnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)) +

                                ",ColumnName:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +

                                ",Value:" + Bytes.toString(CellUtil.cloneValue(cell)) +

                                ",TS:" + cell.getTimestamp());

            }

        }

    }



    /**

     * 9.刪除一條資料

     */

    private static void delTableData(String tableName,String rowKey) throws IOException {

        //獲取表物件

        Table hTable = connection.getTable(TableName.valueOf(tableName));

        //封裝刪除物件

        Delete delete = new Delete(Bytes.toBytes(rowKey));

        hTable.delete(delete);

        hTable.close();

    }





    /**

     * 10.刪除多條是資料

     */

    private static void deleteDatas(String tableName, String... rows) throws IOException {



        //獲取表物件

        Table table = connection.getTable(TableName.valueOf(tableName));



        ArrayList deletes = new ArrayList();



        for (String row : rows) {

            Delete delete = new Delete(Bytes.toBytes(row));

            deletes.add(delete);

        }

        table.delete(deletes);

        table.close();

    }





    /**

     * 測試

     *

     * @param args

     */

    public static void main(String[] args) throws IOException {

        //1.判斷表名是否存在

        isTableExist("student");

        //2.建立表

        //createTable("test","info");

        //3.刪除表

        //delTable("test");

        //4.插入資料

        //createData("staff", "1001", "info", "age1", "18");

        //createData("staff", "1001", "info", "age2", "20");

        //5.插入多條資料

        //createsTable("staff", "1005", "info", new String[]{"name", "sex","address","like"}, new String[]{"sunxueni", "nv","jindong","lovewo"});

        //createsTable("staff", "1004", "info", new String[]{"name", "sex"}, new String[]{"luomk", "nan"});

        //6.獲取一條資料

        //getData("staff","1002");

        //7.獲取某一行的列資料

        // getRowByCF("staff","1002","info","name");

        //8.掃描一張表

        // scanTable("staff");

        //9.刪除一條資料

        // delTableData("staff","1003");

        //10。刪除多條資料

        // deleteDatas("staff","1001","1002");

    }

}

相關推薦

HBase API操作

1.環境準備     新建專案後在pom.xml中新增依賴: <dependency>     <groupId>org.apache.hbase</groupId>     <artifactId>hbase-serv

hbaseapi操作

personal ner except create value str nero test 技術分享 創建maven工程,修改jdk pom文件裏添加需要的jar包 dependencies> <dependency>

二:Java API操作HBase

package com.zoujc.Utils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*;

一:Java API操作HBase

package com.zoujc.Utils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.fil

利用hbase api在本地訪問並操作伺服器的hbase資料庫

最近因為實驗室專案需要,開始研究了hbase。然後想一次性往叢集伺服器上寫入大量的資料,並存到hbase中,考慮到在hbase shell下只能單個數據put,這樣對於批量插入資料的要求完全不合適。於是就研究起hbase的java api,然後去大量填充資料到hbase以測試查詢的效能。於是,故事開

使用scala操作hbase api

最近在研究hbase 和scala,研究scala是為了spark,剛好hbase的api我也不熟,scala也不熟悉,所以就用來練手了 程式碼是兩個類 ConfigUtil 是用來產生configuration的 TestHbaeJavaApi是用來測試hbase a

hadoop2-HBase的Java API操作

Hbase提供了豐富的Java API,以及執行緒池操作,下面我用執行緒池來展示一下使用Java API操作Hbase。 專案結構如下: 我使用的Hbase的版本是 大家下載後,可以拿到裡面的lib目錄下面的jar檔案,即上所示的hbase-lib資源。 介面類: /hbase-util

使用Phoenix api操作hbase 報錯java.lang.ClassNotFoundException: org.apache.phoenix.jdbc.PhoenixDriver

缺少 驅動,如果是maven專案的話,在pom檔案新增對應版本的依賴即可 <dependency> <groupId>org.apach

Hbase實戰--HBASEAPI操作(增刪改查)

連線HBase的正確姿勢 Connection是什麼 在眾多HBase使用者中,常見的使用Connection的錯誤方法有: (1)自己實現一個Connection物件的資源池,每次使用都從資源池中取出一個Connection物件; (2)每個執行緒一個Connection物件。 (

使用用Phoenix的Java api操作HBase

①先將phoenix的 core.jar包 和 phoenix的client.jar 包放到lib裡。 ②建立連線,過程和mysql類似 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

hbase基本命令,api操作

hbase單節點安裝    啟動一個單節點的hbase方便我們測試,學習。在官網下載hbase.tar.gz安裝包 https://hbase.apache.org/修改配置檔案${hbase}/conf/hbase-env.sh,指定jdk位置【hbase-env.sh】e

JAVA API 操作HBASE(一)

使用java API操作HBase 實現功能 建立表 刪除表 新增列 新增列名稱 列出所有表名稱 列出所有表下的列名稱 使用到的Hbase操作類 HBaseConfiguration     配置hbase配置資訊 HBaseAdmin 使用其進行Hbase資料表的操作

hbase java api操作匯入資料

使用hbase儲存名人資料集,資料集由名人文字資訊以及名人圖片組成。 名人文字資訊使用scrapy框架從wiki百科上爬取並儲存在csv格式中。 圖片資訊從百度圖片上爬取每人30張儲存在以該名人姓名命名的資料夾中 因此本文包含以下幾個方面: - 爬取文字

hbase程式設計:通過Java api操作hbase

轉:http://www.aboutyun.com/thread-7151-1-1.html http://blog.csdn.net/cnweike/article/details/42920547 http://blog.csdn.net/zwx19921215/art

HBase的基本api操作及簡要說明

hbase的jar包 HBase-0.98.6原始碼和安裝包下載 這裡用的是hbase-0.98.6-hadoop2-bin.tar.gz ,直接下載解壓後將對應的lib下的包匯入專案即可,這裡只是測試多一點,實際專案需要自己剔除一些不需要的jar包 hb

hbaseapi操作之scan

ann value art println col ase cache set 掃描器 掃描器緩存---------------- 面向行級別的。 @Test public void getScanCache() throws IOException {

關於代碼通過API操作阿裏雲RDS的巨坑

mysq tools per 創建 實例 normal 帳號 mysql5 默認 由於項目原因,要通過API操作阿裏雲的數據庫,於是簡單研究了一下阿裏雲提供的相關文檔,發現官方提供了.NET的SDK,而且還提供了github開源代碼,這個要為阿裏點贊! 於是到github

hbase簡單操作

limit ble mit 查詢 truncate can 清空 簡單 bsp hbase shell ---進入hbaselist ---給出所有表count "table_name" ---查看表的記錄數scan "table_name" --- 查詢多條記錄scan

Solr API操作

api solr 1、需要在/usr/local/services/solr/solr-4.10.3/example/solr/collection1/conf的solrconfig.xml加上 <requestHandler name="/select" class="solr.SearchH

Zabbix監控tomcat、監控代理、API操作、server被動監控 (2)

zabbix監控tomcat、監控代理、api操作、server被動監控接上篇配置十一.Zabbix監控tomcat1.在server3上安裝java和tomcat2.配置tomcat采集腳本vim catalina.sh啟動tomcat3.添加zabbix監控jave程序zabbix本身不能監控到jave,