Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

HBase API operation example

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Package com.test.hbase.api

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 operation

, /

Public class HBaseAPIDemo {

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

Configuration conf = HBaseConfiguration.create ()

/ / set the address of zk

Conf.set ("hbase.zookeeper.quorum", Constants.hostNames)

/ / get the linked object that links the hbase data

Connection conn = ConnectionFactory.createConnection (conf)

/ / past tense interface

/ / HBaseAdmin admin=new HBaseAdmin (conn)

/ / get the objects that operate on the hbase database

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 operation: create_namespace 'ns2'

* description: create a namespace

*

* @ 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 operation: list_namespace

* description: view all namespaces

*

* @ 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")

}

/ * *

* create table: help 'create'

* shell operation: create 'ns2:stu',' cf_info', 'cf_beizhu'

*

* @ param ha

* @ throws IOException

, /

Public static void createTables (HBaseAdmin ha) throws IOException {

/ / determine whether the table exists first

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

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

/ / you need to add at least one column family to create a table.

Htable.addFamily (new HColumnDescriptor ("cf_info"))

Htable.addFamily (new HColumnDescriptor ("cf_beizhu"))

Ha.createTable (htable)

}

Ha.close ()

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

}

/ * *

* shell operation:

* View the table under the empty name:

* 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")

}

/ * *

* add data to the table: help 'put'

* shell operation: put 'ns1:t1',' R1 operations,'C1 operations, 'value'

*

* @ param ha

* @ param conn

* @ throws IOException

, /

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

/ / determine whether the table exists

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

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

/ / create a Put object and add rowkey

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

/ / the first parameter is the column family, the second parameter is the column name, and the third parameter is the value of the column

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")

}

/ * *

* add data to the Hbase table in batch

*

* @ param ha

* @ param conn

* @ throws IOException

, /

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

/ / determine whether the table exists

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

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

List listput = new ArrayList ()

For (int I = 0; I

< 100; i++) { //创建Put对象并且添加rowkey Put put = new Put(("soft_20170101" + i).getBytes()); //第一个参数是列族,第二个参数是列名,第三个参数是列的值 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"); } /** * 查看表数据: * scan 'ns2:stu' * 查看一个列族 * scan 'ns2:stu',{COLUMNS =>

'cf_info'}

* View multiple column families

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

* View the data of a column:

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

*

* @ param ha

* @ param conn

* @ throws IOException

, /

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

/ / determine whether the table exists

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

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

/ / scan the whole table

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")

}

/ * *

* View a row of data: help 'get'

*

* get 'ns1:stu','data_20170102'

*

* View the data of one of the column families in a row:

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

*

* View data for multiple column families in a row:

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

*

* get the value of a column:

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

*

* @ param ha

* @ param conn

* @ throws IOException

, /

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

/ / determine whether the table exists

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

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

/ / get the whole row of data

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")

}

/ * *

* Action: delete namespaces and all tables in namespaces

*

* @ param ha

* @ param conn

* @ throws IOException

, /

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

/ / traverse all tables under the namespace

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")

}

}

/ / filter operation of HBase

Public class HbaseAPIFilter {

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

/ / singleColumnValueFilter ()

/ / qualifierFilter ()

FamilyFilter ()

}

/ * *

* 1. Filter SingleColumnValueFilter with single column value

*

* @ throws IOException

, /

Public static void singleColumnValueFilter () throws IOException {

Table mTable = TableUtils.getTable (Constants.tableName)

/ / if there is a match, then you can get row data

/ / match of individual column values: compare the base class for BinaryComparator BinaryComparator to match the full byte array

/ / 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 ()

/ / rows with the value "xiao" at the beginning of the partial byte array name at the beginning of the BinaryPrefixComparator match are filtered out.

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.

/ / so you can only use EQUAL or NOT_EQUAL for the RegexStringComparator base class

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.

/ / so you can only use EQUAL or NOT_EQUAL for the SubstringComparator base class

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. Column filter (all the data that exists in the column filter table)

* QualifierFilter

*

* @ throws IOException

, /

Public static void qualifierFilter () throws IOException {

Table mTable = TableUtils.getTable (Constants.tableName)

/ / column name filtering:

/ / compare the base class to match the complete byte array for BinaryComparator BinaryComparator

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.

/ / so you can only use EQUAL or NOT_EQUAL for the SubstringComparator base class

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

/ / the partial byte array at the beginning of the BinaryPrefixComparator match, (possible)

/ / RegexStringComparator, regular expression matching (possible)

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 ()

}

}

/ * *

* define the column family filter (all the data of the column family that exists in the table)

* FamilyFilter

*

* @ throws IOException

, /

Public static void familyFilter () throws IOException {

Table mTable = TableUtils.getTable (Constants.tableName)

/ / because it is filtered based on the name of the column family, pass in the column family name

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.

/ / so you can only use EQUAL or NOT_EQUAL for the RegexStringComparator base class

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

/ / BinaryComparator matches the full byte array, (possible)

/ / SubstringComparator is less sensitive than gamete string and case (possible)

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 ()

}

}

}

Note: the premise is to ensure that the cluster is running properly and the HBase cluster is working properly.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report