In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to program HBase, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
Using Eclipse to develop HBase applications
1. Create a new Java Project in Eclipse and name it HBaseTest, then right-click in Properties and select Java Build Path and Add External Jars to import the jar package under the HBase/lib directory.
2. Create a Conf folder under the root directory of the project, copy the hbase-site.xml file under HBase/Conf to this folder, right-select Properties- > Java BuildPath- > Libraries- > Add Class Folder, and then select the Conf folder.
Second, a brief introduction to HBaseJava API
1, HbaseConfiguration
Relationship: org.apache.hadoop.hbase.HBaseConfiguration
Purpose: HBase can be configured through this class
2, HBaseAdmin
Relationship: org.apache.hadoop.hbase.client.HBaseAdmin
Purpose: provides an interface to manage table information in the HBase database. It provides methods for creating tables, deleting tables, and so on.
3, HTableDescriptor
Relationship: org.apache.hadoop.hbase.client.HTableDescriptor
Function: contains the name of the table and its corresponding column family. The methods provided are
Void addFamily (HColumnDescriptor) add a column family
HColumnDescriptor removeFamily (byte [] column) removes a column family
Byte [] getName () gets the name of the table
Byte [] getValue (byte [] key) gets the value of the attribute
Void setValue (String key,Stringvalue) sets the value of the property
4, HColumnDescriptor
Relationship: org.apache.hadoop.hbase.client.HColumnDescriptor
Purpose: to maintain information about columns. The methods provided are
Byte [] getName () gets the name of the column family
Byte [] getValue () gets the value of the corresponding attribute
Void setValue (String key,String value) sets the value of the corresponding property
5, HTable
Relationship: org.apache.hadoop.hbase.client.HTable
Function: the user communicates with HBase table. This method is not thread-safe for update operations, and the write buffer may crash if multiple threads are started to try to communicate with a single HTable instance.
6, Put
Relationship: org.apache.hadoop.hbase.client.Put
Function: used to perform add operations on a single row
7, Get
Relationship: org.apache.hadoop.hbase.client.Get
Function: used to get information about a single row
8, Result
Relationship: org.apache.hadoop.hbase.client.Result
Purpose: stores the single-line value obtained after the Get or Scan operation.
9, ResultScanner
Relationship: Interface
Function: the interface through which the client gets the value.
3. HBase Java API simple instance 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.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Get;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;/* * @ author minglaihan * / public class HBaseTest {static Configuration cfg = HBaseConfiguration.create (); / / create a new table public static void create (String tableName, String columnFamily) throws Exception {HBaseAdmin admin = new HBaseAdmin (cfg) through HBaseAdmin HTableDescriptor If (admin.tableExists (tableName)) {System.out.println ("Table exist"); System.exit (0);} else {HTableDescriptor tableDescriptor = new HTableDescriptor (tableName); tableDescriptor.addFamily (new HColumnDescriptor (columnFamily)) Admin.createTable (tableDescriptor); System.out.println ("Table create success") }} / / add a piece of data to the existing table through HTable Put public static void put (String tableName,String row,String columnFamily,String column,String data) throws IOException {HTable table = new HTable (cfg, tableName); Put put = new Put (Bytes.toBytes (row)) Put.add (Bytes.toBytes (columnFamily), Bytes.toBytes (column), Bytes.toBytes (data)); table.put (put); System.out.println ("put success") } / / get the result set public static void get (String tableName,String row) throws IOException {HTable table = new HTable (cfg, tableName); Get get = new Get (Bytes.toBytes (row)); Result result = table.get (get); System.out.println ("get" + result) listed in the tableName table } / / get all the data information of the tableName table through HTable Scan public static void scan (String tableName) throws IOException {HTable table = new HTable (cfg, tableName); Scan scan = new Scan (); ResultScanner resultScanner = table.getScanner (scan) For (Result s:resultScanner) {System.out.println ("Scan" + resultScanner);}} public static boolean delete (String tableName) throws Exception {HBaseAdmin admin = new HBaseAdmin (cfg) If (admin.tableExists (tableName)) {try {admin.disableTable (tableName); admin.deleteTable (tableName) } catch (Exception e) {/ / TODO: handle exception e.printStackTrace (); return false;}} return true } public static void main (String [] args) {String tableName = "hbase_test"; String columnFamily = "C1"; try {HBaseTest.create (tableName, columnFamily); HBaseTest.put (tableName, "row1", columnFamily, "column1", "data1") HBaseTest.get (tableName, "row1"); HBaseTest.scan (tableName); if (HBaseTest.delete (tableName) = = true) {System.out.println ("delete table" + tableName+ "success") Catch (Exception e) {/ / TODO: handle exception e.printStackTrace ();}}
Screenshot of the running result commenting out the steps of Delete:
Thank you for reading this article carefully. I hope the article "how to program HBase" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.