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

What are the common database operation classes in Hbase

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail the commonly used database operation classes in Hbase, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Reference hbase-client in pom.xml

Org.apache.hbase hbase-client 2.2.5

HBaseConn.java gets the hbase link

Package com.rumenz;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.Table;import java.io.IOException;public class HBaseConn {private static final HBaseConn INSTANCE=new HBaseConn (); private static Configuration configuration; private static Connection connection Private HBaseConn () {try {if (configuration==null) {configuration=HBaseConfiguration.create (); configuration.set ("hbase.zookeeper.quorum", "192.168.82.177 configuration==null 2181");} catch (Exception e) {e.printStackTrace () }} private Connection getConnection () {if (connection==null | | connection.isClosed ()) {try {connection=ConnectionFactory.createConnection (configuration);} catch (Exception e) {e.printStackTrace ();}} return connection } public static Connection getHbaseConn () {return INSTANCE.getConnection ();} public static Table getTable (String table) throws IOException {return INSTANCE.getConnection () .getTable (TableName.valueOf (table));} public static void closeConn () {try {connection.close () } catch (Exception e) {e.printStackTrace ();}}

Operation of adding, deleting, changing and searching HBaseUtil.java database

Package com.rumenz;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.List Public class HBaseUtil {/ * create a table * @ param tableName table name * @ param cf column family name * @ return * @ throws IOException * / public static boolean createTable (String tableName,String [] cf) throws IOException {try (HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHbaseConn (). GetAdmin ()) {if (admin.tableExists (TableName.valueOf (tableName) {return false } List res=new ArrayList (); Arrays.stream (cf) .forEach (cff- > {res.add (ColumnFamilyDescriptorBuilder.newBuilder (Bytes.toBytes (cff)). Build ();}); TableDescriptor hTableDescriptor= TableDescriptorBuilder.newBuilder (TableName.valueOf (tableName)) .setColumnfamilies (res). Build (); admin.createTable (hTableDescriptor) } catch (Exception e) {e.printStackTrace ();} return true;} / * * Delete a table * @ param tableName table name * @ return * / public static boolean deleteTable (String tableName) {try (HBaseAdmin admin= (HBaseAdmin) HBaseConn.getHbaseConn (). GetAdmin ()) {admin.disableTable (TableName.valueOf (tableName)) Admin.deleteTable (TableName.valueOf (tableName));} catch (Exception e) {e.printStackTrace ();} return true } / * insert a data * @ param tableName table name * @ param rowkey primary key * @ param cfName column family * @ param qualifier column name * @ param data data * @ return * / public static boolean putRow (String tableName,String rowkey,String cfName,String qualifier String data) {try (Table table= HBaseConn.getTable (tableName)) {Put put=new Put (Bytes.toBytes (rowkey)) Put.addColumn (Bytes.toBytes (cfName), Bytes.toBytes (qualifier), Bytes.toBytes (data)); table.put (put);} catch (Exception e) {e.printStackTrace ();} return true } / * bulk add data * @ param tableName Table name * @ param puts data list * @ return * / public static boolean putRows (String tableName,List puts) {try (Table table= HBaseConn.getTable (tableName)) {table.put (puts);} catch (Exception e) {e.printStackTrace () } return true;} / * query data through rowkey * @ param tableName Table name * @ param rowkey rowkey * @ return * @ throws IOException * / public static Result getRow (String tableName,String rowkey) throws IOException {try (Table table=HBaseConn.getTable (tableName)) {Get get=new Get (Bytes.toBytes (rowkey)); return table.get (get) } catch (Exception e) {e.printStackTrace ();} return null } / * query data via scan * @ param tableName Table name * @ row * @ param endRow ends with row * @ return * / public static ResultScanner getScanner (String tableName,String startRow,String endRow) {try (Table table=HBaseConn.getTable (tableName)) {Scan scan=new Scan (); scan.withStartRow (Bytes.toBytes (startRow)) Scan.withStopRow (Bytes.toBytes (endRow)); scan.setCaching (1000); return table.getScanner (scan);} catch (Exception e) {e.printStackTrace ();} return null } / * query data through filter * @ param tableName Table name * @ param startRow start * @ param endRow end * @ param flist filter * @ return * / public static ResultScanner getScanner (String tableName, String startRow, String endRow, FilterList flist) {try (Table table=HBaseConn.getTable (tableName)) {Scan scan=new Scan () Scan.withStartRow (Bytes.toBytes (startRow)); scan.withStopRow (Bytes.toBytes (endRow)); scan.setFilter (flist); scan.setCaching (1000); return table.getScanner (scan);} catch (Exception e) {e.printStackTrace ();} return null } / * Delete data through rowkey * @ param tableName Table name * @ param rowKey rowkey * @ return * / public static boolean deleteRow (String tableName,String rowKey) {try (Table table=HBaseConn.getTable (tableName)) {Delete delete=new Delete (Bytes.toBytes (rowKey)); table.delete (delete);} catch (Exception e) {e.printStackTrace () } return true;} / * Delete column Family * @ param tableName Table name * @ param cfName column Family * @ return * / public static boolean deleteColumnFamily (String tableName,String cfName) {try (HBaseAdmin admin= (HBaseAdmin) HBaseConn.getHbaseConn (). GetAdmin ()) {admin.deleteColumnFamily (TableName.valueOf (tableName), Bytes.toBytes (cfName)) } catch (Exception e) {e.printStackTrace ();} return true } / * Delete column name * @ param tableName Table name * @ param rowKey rowkey * @ param cfName column Family * @ param qualifier column name * @ return * / public static boolean deleteQualifier (String tableName,String rowKey,String cfName,String qualifier) {try (Table table=HBaseConn.getTable (tableName)) {Delete delete=new Delete (Bytes.toBytes (rowKey)) Delete.addColumn (Bytes.toBytes (cfName), Bytes.toBytes (qualifier)); table.delete (delete);} catch (Exception e) {e.printStackTrace ();} return true;}}

About which commonly used database operation classes in Hbase are shared here, I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report