Basic java operations on Hbase

Source: Internet
Author: User

Java implements various Hbase operations through HTable. As HTable creation consumes some resources, we recommend that you create only one HTable instance. If you must use multiple HTable instances, you can use HTablePool. This document does not describe HTablePool.

Create a table:

Creating a table is implemented through the HBaseAdmin class, And the HBaseAdmin class mainly manages the table.

 1 public static void createTable(String tableName, String[] cfs) throws IOException{ 2         HBaseAdmin admin = new HBaseAdmin(configuration); 3         if (admin.tableExists(tableName)) { 4             System.out.println("table already exists"); 5         }else { 6             HTableDescriptor descriptor = new HTableDescriptor(tableName); 7             for(int i = 0; i < cfs.length; ++i){ 8                 descriptor.addFamily(new HColumnDescriptor(cfs[i])); 9             }10             admin.createTable(descriptor);11         }12     }

HBaseAdmin also contains APIs for operating tables, including deleting tables and columns. If you are interested, you can refer to its official API.

Add data:

Add a put object through the Put Operation of HTable;

void put(Put put) throws IOException

The Put class contains multiple constructors. Here we only use its first constructor.

Put(byte[] row)Put(byte[] row, RowLock rowLock)Put(byte[] row, long ts)Put(byte[] row, long ts, RowLock rowLock)

Use the add function to add data like a Put object:

Put add(byte[] family, byte[] qualifier, byte[] value)Put add(byte[] family, byte[] qualifier, long ts, byte[] value)Put add(KeyValue kv) throws IOException

Add data operation:

1 public static void putData(String tableName) throws IOException{2         HTable table = new HTable(configuration, tableName);3         Put put = new Put(Bytes.toBytes("row1"));4         put.add(Bytes.toBytes("cf"), Bytes.toBytes("a"), Bytes.toBytes("v1"));5         table.put(put);6         table.close();7     }

 

Get data:

To obtain data, you need to use the HTable get function:

Result get(Get get) throws IOException

The Get class constructor is less ts than the Put class.

Get(byte[] row)Get(byte[] row, RowLock rowLock)

Operations to obtain data:

1 public static void getData(String tableName) throws IOException{2         HTable table = new HTable(configuration, tableName);3         Get get = new Get(Bytes.toBytes("row1"));4         Result result = table.get(get);5         String value = new String(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("a")));6         System.out.println(value);7         table.close();8     }

To scan the entire table, you need to use "tables.

public static void scannerData(String tablename) throws IOException {        HTable table = new HTable(configuration, tablename);        Scan s = new Scan();        ResultScanner rs = table.getScanner(s);        for (Result r : rs) {            KeyValue[] kv = r.raw();            for (int i = 0; i < kv.length; i++) {                System.out.print(new String(kv[i].getRow()) + "  ");                System.out.print(new String(kv[i].getFamily()) + ":");                System.out.print(new String(kv[i].getQualifier()) + "  ");                System.out.print(kv[i].getTimestamp() + "  ");                System.out.println(new String(kv[i].getValue()));            }        }    }

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.