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

Examples of the usage of SHELL operation and API in HBase

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you "examples of SHELL operation and API usage in HBase". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "examples of SHELL operation and API usage in HBase".

1. Table structure:

2. SHELL operation

Command: hbase shell

Display table: list

Create a table: create 'tb_name','column_family_1','column_family_2',...

Or create 'user', {NAME = >' column_family_1', VERSIONS = >'3'}

Insert data: put 'tb_name','rk_on','column_family: key','value'

Get data:

Get all the data: get 'tb_name','rk_on'

Get column family data: get 'tb_name','rk_on','column_family'

Or get 'tb_name',' rk_on', {column= > ['cf_name1',' cf_name2']}

Get the column data in the column family: get 'tb_name','rk_on','column_family:key','column_family:column2',..

Or get 'tb_name',' rk_on', {COLUMN = > ['cf_name:key',' cf_name:key']}

Get each version of the data

Column family version: get 'tb_name',' rk_on', {COLUMN = > 'cf_name1', VERSIONS = > Number}

Column version: get 'tb_name',' rk_on', {COLUMN = > 'cf_name1:c_name', VERSIONS = > Number}

Column version in the time range: get 'user',' rk0001', {COLUMN = > 'cfn:key', VERSIONS = > Number, TIMERANGE = > [1392368783980,1392380169184]}

VERSION: query the number of versions

TIMERANGE: timestamp range

Perfect match: get 'tb_name',' rk_on', {FILTER = > "ValueFilter (=, 'binary:value')"}

Rk_on contains information about value

Match: get 'tb_name',' rk_on', {FILTER = > "(ValueFilter (=, 'substring:a'))"}

The column identifier contains information about a

Scan query:

Scan 'tb_name'

Query the information of the column family cfn in the user table

Scan 'tb_name', {COLUMNS = >' cfn'}

Scan 'tb_name', {COLUMNS = >' cfn', RAW = > true, VERSIONS = > 5}

Scan 'tb_name', {COLUMNS = >' cfn', RAW = > true, VERSIONS = > 3}

Query the information that the column families in the user table are cfn1 and cfn2

Scan 'user', {COLUMNS = > [' cfn1', 'cfn2']}

Scan 'user', {COLUMNS = > [' cfn1:key1', 'cfn2:key2']}

Query the information in the user table where the column family is info and the column identifier is name

Scan 'user', {COLUMNS = >' info:name'}

Query the information in the user table with the column family of info, the column identifier of name, and the latest version of 5

Scan 'user', {COLUMNS = >' info:name', VERSIONS = > 5}

Query the information in the user table where the column families are info and data and the column identifier contains the a character

Scan 'user', {COLUMNS = > [' info', 'data'], FILTER = > "(QualifierFilter (=,' substring:a'))"}

Query the data in the user table whose column family is info,rk and the range is [rk0001, rk0003)

Scan 'people', {COLUMNS = >' info', STARTROW = > 'rk0001', ENDROW = >' rk0003'}

Query the row key that begins with the rk character in the user table

Scan 'user', {FILTER= > "PrefixFilter (' rk')"}

Query the specified range of data in the user table

Scan 'user', {TIMERANGE = > [1392368783980,1392380169184]}

Delete data

Delete the data whose row key is rk0001 and column identifier is info:name in the user table

Delete 'people',' rk0001', 'info:name'

Delete the data whose row key is rk0001 and column identifier is 1392383705316 in user table

Delete 'user',' rk0001', 'info:name', 1392383705316

Clear the data in the user table

Truncate 'people'

Modify table structure

First deactivate the user table (not in the new version)

Disable 'user'

Add two column families F1 and f2

Alter 'people', NAME = >' F1'

Alter 'user', NAME = >' f2'

Enable tabl

Enable 'user'

# disable 'user' (not needed in the new version)

Delete a column family:

Alter 'user', NAME = >' F1', METHOD = > 'delete' or alter' user', 'delete' = >' F1'

Add column family F1 and delete column family f2

Alter 'user', {NAME = >' f1'}, {NAME = >'f2', METHOD = > 'delete'}

Change the F1 column family version number of user to 5

Alter 'people', NAME = >' info', VERSIONS = > 5

Enable tabl

Enable 'user'

Delete tabl

Disable 'user'

Drop 'user'

2 、 API:

The corresponding relationship between several related classes and HBase data Model

Java class HBase data model HBaseAdmin database (DataBase) HBaseConfiguration

HTable (Table) HTableDescriptor column family (Column Family) Put column modifier (Column Qualifier) Get

Scanner

1. HBaseConfiguration

Relationship: org.apache.hadoop.hbase.HBaseConfiguration

Purpose: configure HBase

The return value function describes that voidaddResource (Path file) adds resources voidclear () clears all set attributes stringget (String name) to get the value corresponding to the attribute name StringgetBoolean (String name, boolean defaultValue) to get the attribute value of type boolean, if its attribute value type part boolean, returns the default attribute value voidset (String name, String value) sets the value voidsetBoolean (String name, boolean value) through the attribute name to set the attribute value of type boolean

Usage example:

HBaseConfiguration hconfig = new HBaseConfiguration (); hconfig.set ("hbase.zookeeper.property.clientPort", "2181")

This method sets the port number of "hbase.zookeeper.property.clientPort" to 2181. In general, HBaseConfiguration uses constructors for initialization and then uses other methods.

II. HBaseAdmin

Relationship: org.apache.hadoop.hbase.client.HBaseAdmin

Purpose: 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.

The return value function describes that voidaddColumn (String tableName, HColumnDescriptor column) adds a checkHBaseAvailable (HBaseConfiguration conf) static function to an existing table to see if HBase is running

CreateTable (HTableDescriptor desc) creates a table and synchronizes the operation

DeleteTable (byte [] tableName) deletes an existing table

EnableTable (byte [] tableName) makes the table valid

DisableTable (byte [] tableName) makes the table invalid

HTableDescriptor [] listTables () lists all user control table items voidmodifyTable (byte [] tableName, HTableDescriptor htd) modify table mode, which is an asynchronous operation and may take a certain amount of time to booleantableExists (String tableName) to check whether the table exists

Usage example:

HBaseAdmin admin = new HBaseAdmin (config); admin.disableTable ("tablename") III. HTableDescriptor

Relationship: org.apache.hadoop.hbase.HTableDescriptor

Function: contains the name of the table and the column family that corresponds to the table

Return value function description voidaddFamily (HColumnDescriptor) add a column family HColumnDescriptorremoveFamily (byte [] column) remove a column family byte [] getName () get the name of the table byte [] getValue (byte [] key) get the value of the property voidsetValue (String key, String value) set the value of the property

Usage example:

HTableDescriptor htd = new HTableDescriptor (table); htd.addFamily (new HcolumnDescriptor ("family"))

In the above example, a column family is added for HTableDescriptor through a HColumnDescriptor instance: family

IV. HColumnDescriptor

Relationship: 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.

Return value function description byte [] getName () get the name of the column family byte [] getValue (byte [] key) get the value of the corresponding attribute voidsetValue (String key, String value) set the value of the corresponding attribute

Usage example:

HTableDescriptor htd = new HTableDescriptor (tablename); HColumnDescriptor col = new HColumnDescriptor ("content:"); htd.addFamily (col)

This example adds a column family of content

5. HTable

Relationship: 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.

Return value function description voidcheckAdnPut (byte [] row, byte [] family, byte [] qualifier, byte [] value) Put put automatically checks whether row/family/qualifier matches the given value voidclose () releases all resources or suspends the update Booleanexists in the internal buffer (Get get) checks whether the value specified by the Get instance exists in the column of HTable (Get get) gets the value corresponding to some cells of the specified row byte [] getEndKeys () gets the end key value of each region of the currently open table ResultScannergetScanner (byte [] family) ) get the scanner instance HTableDescriptorgetTableDescriptor () of the current given column family, get the HTableDescriptor instance byte [] getTableName () of the current table, get the table name static booleanisTableEnabled (HBaseConfiguration conf) String tableName) check whether the table is valid voidput (Put put) add values to the table

Usage example:

HTable table = new HTable (conf, Bytes.toBytes (tablename)); ResultScanner scanner = table.getScanner (family); VI. Put

Relationship: org.apache.hadoop.hbase.client.Put

Function: used to perform add operations on a single row

Return value function description Putadd (byte [] family, byte [] qualifier, byte [] value) adds the specified column and corresponding value to Putadd (byte [] family, byte [] qualifier, long ts) in the Put instance Byte [] value) add the specified column and corresponding value and timestamp to the Put instance byte [] getRow () get the row of the Put instance RowLockgetRowLock () get the row lock of the Put instance longgetTimeStamp () get the timestamp of the Put instance booleanisEmpty () check whether the familyMap is empty PutsetTimeStamp (long timeStamp) set the timestamp of the Put instance

Usage example:

HTable table = new HTable (conf,Bytes.toBytes (tablename)); Put p = new Put (brow); / / create a Put operation p.add (family,qualifier,value) for the specified line; table.put (p); 7. Get

Relationship: org.apache.hadoop.hbase.client.Get

Function: used to get information about a single row

Return value function description GetaddColumn (byte [] family, byte [] qualifier) get the column GetaddFamily corresponding to the specified column family and column modifier (byte [] family) get all the columns corresponding to its corresponding column through the specified column family GetsetTimeRange (long minStamp,long maxStamp) get the version number of the specified column GetsetFilter (Filter filter) set the server-side filter when performing Get operation

Usage example:

HTable table = new HTable (conf, Bytes.toBytes (tablename)); Get g = new Get (Bytes.toBytes (row)); VIII. Result

Relationship: org.apache.hadoop.hbase.client.Result

Function: get the single-row value of the table after storing the Get or Scan operation. Using the methods provided by this class, you can get values directly or various Map structures (key-value pairs)

Return value function description booleancontainsColumn (byte [] family, byte [] qualifier) check whether the specified column exists NavigableMapgetFamilyMap (byte [] family) get the key value pair of modifiers and values contained in the corresponding column family byte [] getValue (byte [] family, byte [] qualifier) get the latest value of the corresponding column 9.

Relationship: Interface

Function: the interface through which the client gets the value

The return value function describes that voidclose () closes scanner and releases the resource assigned to it, Resultnext (), to get the value of the next line.

Related codes:

Package cn.itcast.hbase;import java.io.IOException;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.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", "hadoop01,hadoop02,hadoop03");} @ Test public void testDrop () throws Exception {HBaseAdmin admin = new HBaseAdmin (conf); admin.disableTable ("account"); admin.deleteTable ("account"); admin.close () } @ Test public void testPut () throws Exception {HTable table = new HTable (conf, "user"); Put put = new Put (Bytes.toBytes ("rk0003")); put.add (Bytes.toBytes ("info"), Bytes.toBytes ("name"), Bytes.toBytes ("liuyan"); table.put (put); 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, "user"); 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 ()); System.out.println (family); String qualifier = new String (kv.getQualifier ()); System.out.println (qualifier) System.out.println (new String (kv.getValue ();} table.close ();} @ Test public void testScan () throws Exception {HTablePool pool = new HTablePool (conf, 10); 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) {/ * * for (KeyValue kv: r.list ()) {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 ();} * / byte [] value = r.getValue (Bytes.toBytes ("info"), Bytes.toBytes ("name")); System.out.println (new String (value));} pool.close () } @ 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 () } public static void main (String [] args) throws Exception {Configuration conf = HBaseConfiguration.create (); conf.set ("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03"); HBaseAdmin admin = new HBaseAdmin (conf); HTableDescriptor td = new HTableDescriptor ("account"); HColumnDescriptor cd = new HColumnDescriptor ("info"); cd.setMaxVersions (10); td.addFamily (cd) Admin.createTable (td); admin.close ();} public void createTable (String tableName, int maxVersion, String...) Cf) {}} these are all the contents of this article entitled "examples of SHELL Operation and API usage in HBase". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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