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

How to operate API in HBase

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is to share with you about how to operate API in HBase, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

[several commonly used classes]

1. Org.apache.hadoop.hbase.HBaseConfiguration

The object used by each hbase client, which represents the HBase configuration information. It can be constructed in two ways:

Public HBaseConfiguration ()

Public HBaseConfiguration (final Configuration c)

The default construction attempts to read the configuration from hbase-default.xml and hbase-site.xml. If classpath does not have these two files, you need to set up your own configuration.

Configuration HBASE_CONFIG = new Configuration ()

HBASE_CONFIG.set ("hbase.zookeeper.quorum", "zkServer")

HBASE_CONFIG.set ("hbase.zookeeper.property.clientPort", "2181")

HBaseConfiguration cfg = new HBaseConfiguration (HBASE_CONFIG)

2. Org.apache.hadoop.hbase.client.HBaseAdmin

Provides an interface to manage the table information of the HBase database. It provides methods such as creating tables, deleting tables, listing table items, making tables valid or invalid, and adding or deleting table family members.

3. Org.apache.hadoop.hbase.HTableDescriptor

Contains the name of the table and the column family that corresponds to the table.

Common method: void addFamily (HcolumnDescriptor family) add a column family. The detailed usage is as follows, adding a content column family to the tb_user table.

HTableDescriptor tableDescriptor = new HTableDescriptor ("tb_user")

HColumnDescriptor col = new HColumnDescriptor ("content:")

TableDescriptor.addFamily (col)

4. Org.apache.hadoop.hbase.HColumnDescriptor

Function: maintains information about column families, such as version numbers, compression settings, etc. It is usually used when creating a table or adding a column family to a table. Column families cannot be modified directly after they are created, but can only be deleted and recreated. When a column family is deleted, the data in the column family is also deleted.

5. Org.apache.hadoop.hbase.client.HTable

Function: can be used to communicate directly with the HBase table. This method is not thread-safe for update operations.

7. Org.apache.hadoop.hbase.client.Get

Function: used to get information about a single row

[actual combat]

Package com.youku.test

Import java.util.Iterator

Import java.util.List

Import org.apache.hadoop.conf.Configuration

Import org.apache.hadoop.hbase.Cell

Import org.apache.hadoop.hbase.HBaseConfiguration

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

Import org.junit.Before

Import org.junit.Test

/ * *

* HBase Java API Test Demo.

, /

Public class HbaseDemo {

Private Configuration conf = null

/ * *

* initialization

, /

@ Before

Public void init () {

Conf = HBaseConfiguration.create ()

Conf.set ("hbase.zookeeper.quorum", "zk01,zk02,zk03")

}

/ * *

* Delete tables

* @ throws Exception

, /

@ Test

Public void testDrop () throws Exception {

HBaseAdmin admin = new HBaseAdmin (conf)

Admin.disableTable ("yk_test")

Admin.deleteTable ("yk_test")

Admin.close ()

}

/ * *

* insert data

* @ throws Exception

, /

@ Test

Public void testPut () throws Exception {

HTable table = new HTable (conf, "person_info")

Put p = new Put (Bytes.toBytes ("person_rk_bj_zhang_000002"))

P.add ("base_info" .getBytes (), "name" .getBytes (), "zhangwuji" .getBytes ())

Table.put (p)

Table.close ()

}

/ * *

* delete a column

* @ throws Exception

, /

@ Test

Public void testDel () throws Exception {

HTable table = new HTable (conf, "user")

Delete del = new Delete (Bytes.toBytes ("rk0001"))

Del.deleteColumn (Bytes.toBytes ("data"), Bytes.toBytes ("pic"))

Table.delete (del)

Table.close ()

}

/ * *

* single query

* @ throws Exception

, /

@ Test

Public void testGet () throws Exception {

HTable table = new HTable (conf, "person_info")

Get get = new Get (Bytes.toBytes ("person_rk_bj_zhang_000001"))

Get.setMaxVersions (5)

Result result = table.get (get)

List cells = result.listCells ()

For (Cell c: cells) {

}

/ / result.getValue (family, qualifier); you can directly extract a specific value from the result

/ / traverses all the key-value pairs in result

List kvs = result.list ()

/ / kv-> F1 title Vista Vista superse.... F1:author:zhangsan f1:content:asdfasldgkjsldg

For (KeyValue kv: kvs) {

String family = new String (kv.getFamily ())

System.out.println (family)

String qualifier = new String (kv.getQualifier ())

System.out.println (qualifier)

System.out.println (new String (kv.getValue ()

}

Table.close ()

}

/ *

* traversal table

* @ throws Exception

, /

@ Test

Public void testScan () throws Exception {

HTable table = null

Try {

Table = new HTable (conf, "person_info")

Scan scan = new Scan ()

Scan.addFamily (Bytes.toBytes ("v"))

ResultScanner rs = table.getScanner (scan)

Iterator it = rs.iterator ()

While (it.hasNext ()) {

Result result = it.next ()

If (result! = null & & result.size () > 0) {

Byte [] row = result.getRow ()

String rowStr = Bytes.toString (row); / / rowkey

System.out.println ("rowkey:" + rowStr)

Byte [] value = result.getValue (Bytes.toBytes ("v"), Bytes.toBytes ("c"))

If (value! = null) {

Long count = Bytes.toLong (value); / / value

System.out.println ("colum value:" + count)

}

}

}

} catch (Exception e) {

E.printStackTrace ()

} finally {

If (table! = null) {

Try {

Table.close ()

} catch (Exception e2) {

E2.printStackTrace ()

}

}

}

}

}

[supplementary note]

In the use of scan operations, because the HBase table is generally very large, often need to be used in conjunction with filters, refer to the "HBase-- commonly used filters" in detail, in addition, if you specify startRow and stopRow in scan, the results do not contain stopRow, but include startRow, and startRow and stopRow support partial matching, in practical applications, if the rowkey design is more complex, composed of multiple parts, you can use this way to query rows that meet the criteria.

The above is how to operate API in HBase. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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

Servers

Wechat

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

12
Report