真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

HBaseAPI操作范例

package com.test.hbase.api;

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供下城網(wǎng)站建設(shè)、下城做網(wǎng)站、下城網(wǎng)站設(shè)計(jì)、下城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、下城企業(yè)網(wǎng)站模板建站服務(wù),10多年下城做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

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;

import java.util.ArrayList;

import java.util.List;

/**

 * Created by zsq.

 * Date: 2017/6/14

 * desc:Hbase api操作

 */

public class HBaseAPIDemo {

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

        Configuration conf = HBaseConfiguration.create();

        //設(shè)置zk的地址

        conf.set("hbase.zookeeper.quorum", Constants.hostNames);

        //獲取鏈接hbase數(shù)據(jù)的鏈接對(duì)象

        Connection conn = ConnectionFactory.createConnection(conf);

        //過(guò)去時(shí)接口

        //  HBaseAdmin admin=new HBaseAdmin(conn);

        //獲取操作hbase數(shù)據(jù)庫(kù)的對(duì)象

        HBaseAdmin ha = (HBaseAdmin) conn.getAdmin();

//        createNamespace(ha);

//        listNamespace(ha);

//        createTables(ha);

//        listNamespaceTables(ha);

//        putDataToTables(ha, conn);

//        putMultilDataToTables(ha, conn);

//        scanTablesData(ha, conn);

//        getTablesData(ha, conn);

        deleteNsAndTable(ha, conn);

    }

    /**

     * shell操作:create_namespace 'ns2'

     * 說(shuō)明:創(chuàng)建命名空間

     *

     * @param ha

     * @throws IOException

     */

    public static void createNamespace(HBaseAdmin ha) throws IOException {

        ha.createNamespace(NamespaceDescriptor.create("ns2").build());

        ha.close();

        System.out.println("-----createNamespace----over");

    }

    /**

     * shell操作:list_namespace

     * 描述: 查看所有命名空間

     *

     * @param ha

     * @throws IOException

     */

    public static void listNamespace(HBaseAdmin ha) throws IOException {

        NamespaceDescriptor[] listns = ha.listNamespaceDescriptors();

        for (NamespaceDescriptor ns : listns) {

            System.out.println(ns.getName());

        }

        ha.close();

        System.out.println("-----listNamespace----over");

    }

    /**

     * 創(chuàng)建表:help 'create'

     * shell操作: create 'ns2:stu', 'cf_info', 'cf_beizhu'

     *

     * @param ha

     * @throws IOException

     */

    public static void createTables(HBaseAdmin ha) throws IOException {

        //先判斷表是否存在

        if (!ha.tableExists("ns2:stu")) {

            HTableDescriptor htable = new HTableDescriptor(TableName.valueOf("ns2:stu"));

            //創(chuàng)建一個(gè)表至少需要添加一個(gè)列族

            htable.addFamily(new HColumnDescriptor("cf_info"));

            htable.addFamily(new HColumnDescriptor("cf_beizhu"));

            ha.createTable(htable);

        }

        ha.close();

        System.out.println("-----createTables----over");

    }

    /**

     * shell操作:

     * 查看命名空下的表:

     * list_namespace_tables 'ns2'

     *

     * @param ha

     * @throws IOException

     */

    public static void listNamespaceTables(HBaseAdmin ha) throws IOException {

        HTableDescriptor[] htables = ha.listTableDescriptorsByNamespace("ns2");

        for (HTableDescriptor tb : htables) {

            System.out.println(tb.getTableName());

        }

        ha.close();

        System.out.println("-----createTables----over");

    }

    /**

     * 添加數(shù)據(jù)到表:help 'put'

     * shell操作:put 'ns1:t1', 'r1', 'c1', 'value'

     *

     * @param ha

     * @param conn

     * @throws IOException

     */

    public static void putDataToTables(HBaseAdmin ha, Connection conn) throws IOException {

        //判斷表是否存在

        if (ha.tableExists("ns2:stu")) {

            Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));

            //創(chuàng)建Put對(duì)象并且添加rowkey

            Put put = new Put("soft_20170101".getBytes());

            //第一個(gè)參數(shù)是列族,第二個(gè)參數(shù)是列名,第三個(gè)參數(shù)是列的值

            put.addColumn("cf_info".getBytes(), "name".getBytes(), "laowang".getBytes());

            put.addColumn("cf_info".getBytes(), "age".getBytes(), "20".getBytes());

            put.addColumn("cf_info".getBytes(), "sex".getBytes(), "nan".getBytes());

            put.addColumn("cf_beizhu".getBytes(), "address".getBytes(), "fangshan".getBytes());

            mTable.put(put);

        }

        ha.close();

        System.out.println("-----putDataToTables----over");

    }

    /**

     * 批量添加數(shù)據(jù)到Hbase表

     *

     * @param ha

     * @param conn

     * @throws IOException

     */

    public static void putMultilDataToTables(HBaseAdmin ha, Connection conn) throws IOException {

        //判斷表是否存在

        if (ha.tableExists("ns2:stu")) {

            Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));

            List listput = new ArrayList();

            for (int i = 0; i < 100; i++) {

                //創(chuàng)建Put對(duì)象并且添加rowkey

                Put put = new Put(("soft_20170101" + i).getBytes());

                //第一個(gè)參數(shù)是列族,第二個(gè)參數(shù)是列名,第三個(gè)參數(shù)是列的值

                put.addColumn("cf_info".getBytes(), "name".getBytes(), ("laowang" + i).getBytes());

                put.addColumn("cf_info".getBytes(), "age".getBytes(), "20".getBytes());

                if (i % 2 == 1) {

                    put.addColumn("cf_info".getBytes(), "sex".getBytes(), "nv".getBytes());

                } else {

                    put.addColumn("cf_info".getBytes(), "sex".getBytes(), "nan".getBytes());

                }

                put.addColumn("cf_beizhu".getBytes(), "address".getBytes(), ("fangshan" + i).getBytes());

                listput.add(put);

            }

            //批量插入

            mTable.put(listput);

        }

        ha.close();

        System.out.println("-----putMultilDataToTables----over");

    }

    /**

     * 查看表數(shù)據(jù):

     * scan 'ns2:stu'

     * 查看一個(gè)列族

     * scan 'ns2:stu',{COLUMNS => 'cf_info'}

     * 查看多個(gè)列族

     * scan 'ns2:stu',{COLUMNS => ['cf_info','cf_beizhu']}

     * 查看某個(gè)列的數(shù)據(jù):

     * scan 'ns2:stu',{COLUMNS => 'cf_info:name'}

     *

     * @param ha

     * @param conn

     * @throws IOException

     */

    public static void scanTablesData(HBaseAdmin ha, Connection conn) throws IOException {

        //判斷表是否存在

        if (ha.tableExists("ns2:stu")) {

            Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));

            //掃描整張表

            Scan scan = new Scan();

            //scan.addFamily("cf_info".getBytes());

//            scan.addColumn("cf_info".getBytes(), "name".getBytes());

            ResultScanner rs = mTable.getScanner(scan);

            for (Result result : rs) {

                System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

                System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

                System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

                System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

            }

        }

        ha.close();

        System.out.println("-----scanTablesData----over");

    }

    /**

     * 查看某一行數(shù)據(jù):help 'get'

     *

     * get 'ns1:stu','data_20170102'

     *

     * 查看某行的其中的一個(gè)列族的數(shù)據(jù):

     * get 'ns1:stu','data_20170102',{COLUMN => 'cf_info'}

     *

     * 查看某行的其中的多個(gè)列族的數(shù)據(jù):

     * get 'ns1:stu','data_20170102',{COLUMN => ['cf_info','cf_beizhu']}

     *

     * 獲取某一個(gè)列的值:

     * get 'ns1:stu','data_20170102','cf_info:name'

     *

     * @param ha

     * @param conn

     * @throws IOException

     */

    public static void getTablesData(HBaseAdmin ha, Connection conn) throws IOException {

        //判斷表是否存在

        if (ha.tableExists("ns2:stu")) {

            Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));

            //獲取整行數(shù)據(jù)

            Get get = new Get("soft_2017010196".getBytes());

//            get.addFamily("cf_info".getBytes());

            get.addColumn("cf_info".getBytes(), "name".getBytes());

            Result result = mTable.get(get);

            System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

            System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

            System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

            System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

        }

        ha.close();

        System.out.println("-----getTablesData----over");

    }

    /**

     * 操作:刪除命名空間和命名空間所有表

     *

     * @param ha

     * @param conn

     * @throws IOException

     */

    public static void deleteNsAndTable(HBaseAdmin ha, Connection conn) throws IOException {

        //遍歷命名空間下所有表

        HTableDescriptor[] htables = ha.listTableDescriptorsByNamespace("ns1");

        for (HTableDescriptor tb : htables) {

            System.out.println(tb.getTableName());

            if (ha.isTableEnabled(tb.getTableName())) {

                ha.disableTable(tb.getTableName());

            }

            ha.deleteTable(tb.getTableName());

        }

        ha.deleteNamespace("ns1");

        ha.close();

        System.out.println("-----deleteNsAndTable----over");

    }

    

}

        // HBase 的過(guò)濾器操作

public class HbaseAPIFilter {

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

//        singleColumnValueFilter();

//        qualifierFilter();

        familyFilter();

    }

    /**

     * 1、單個(gè)列值過(guò)濾   SingleColumnValueFilter

     *

     * @throws IOException

     */

    public static void singleColumnValueFilter() throws IOException {

        Table mTable = TableUtils.getTable(Constants.tableName);

        //如果匹配,那么可用獲取行數(shù)據(jù)

        //單個(gè)列值的匹配:比較基類為BinaryComparator   BinaryComparator 匹配完整字節(jié)數(shù)組

        // SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "laowang88".getBytes());

        SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator("laowang88".getBytes()));

        //BinaryPrefixComparator 匹配開始的部分字節(jié)數(shù)組 name的值開始部分為"xiao"的行都過(guò)濾出來(lái)

        SingleColumnValueFilter scvf2 = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator("xiao".getBytes()));

//        Only EQUAL or NOT_EQUAL comparisons are valid with this comparator.

//        所以對(duì)于RegexStringComparator基類只能用EQUAL或者NOT_EQUAL

        SingleColumnValueFilter scvf3 = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^[x].*$"));

//        Only EQUAL or NOT_EQUAL tests are valid with this comparator.

//        所以對(duì)于SubstringComparator基類只能用EQUAL或者NOT_EQUAL

        SingleColumnValueFilter scvf4 = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, new SubstringComparator("bai"));

        Scan scan = new Scan();

        scan.setFilter(scvf4);

        ResultScanner rs = mTable.getScanner(scan);

        for (Result result : rs) {

            System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

            System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

            System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

            System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

        }

    }

    /**

     * 2. 列過(guò)濾器(列過(guò)濾器表中存在該列的數(shù)據(jù)都出來(lái))

     * QualifierFilter

     *

     * @throws IOException

     */

    public static void qualifierFilter() throws IOException {

        Table mTable = TableUtils.getTable(Constants.tableName);

        //列名過(guò)濾:

        //比較基類為BinaryComparator   BinaryComparator 匹配完整字節(jié)數(shù)組

        QualifierFilter columnsNameFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator("name".getBytes()));

        QualifierFilter columnsNameFilter2 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator("age".getBytes()));

//        Only EQUAL or NOT_EQUAL tests are valid with this comparator.

//        所以對(duì)于SubstringComparator基類只能用EQUAL或者NOT_EQUAL

        QualifierFilter columnsNameFilter3 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("a"));

        

        //BinaryPrefixComparator 匹配開始的部分字節(jié)數(shù)組, (可能用)

        // RegexStringComparator, 正則表達(dá)式匹配(可能用)

        Scan scan = new Scan();

        scan.setFilter(columnsNameFilter3);

        ResultScanner rs = mTable.getScanner(scan);

        for (Result result : rs) {

            System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

            System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

            System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

            System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

        }

    }

    /**

     * 定義列族過(guò)濾器(表中存在該列族的數(shù)據(jù)都出來(lái))

     * FamilyFilter

     *

     * @throws IOException

     */

    public static void familyFilter() throws IOException {

        Table mTable = TableUtils.getTable(Constants.tableName);

        //因?yàn)槭歉鶕?jù)列族的名稱過(guò)濾,所以傳入列族名稱

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator("cf_i".getBytes()));

        //        Only EQUAL or NOT_EQUAL tests are valid with this comparator.

//        所以對(duì)于RegexStringComparator基類只能用EQUAL或者NOT_EQUAL

        FamilyFilter familyFilter2 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^[a-z_]{3}[^b].+$"));

//        BinaryComparator 匹配完整字節(jié)數(shù)組, (可能用)

//        SubstringComparator 比配子串、大小寫不敏感(可能用)

        Scan scan = new Scan();

        scan.setFilter(familyFilter2);

        ResultScanner rs = mTable.getScanner(scan);

        for (Result result : rs) {

            System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

            System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

            System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

            System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

        }

    }

}

注意: 前提是保證集群正常運(yùn)行和HBase集群正常工作。


網(wǎng)頁(yè)標(biāo)題:HBaseAPI操作范例
網(wǎng)頁(yè)路徑:http://weahome.cn/article/jjppci.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部