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

Getting started with HBase

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

I. Creating tables:

hbase(main):005:0> create 'users','info'

where,info is a column family of the users table, and tables in HBase must have at least one column family.

HBase creates tables without mentioning any columns or data types. Nothing is needed except family names. This is why HBase is called a "schema-less" database.

hbase(main):006:0> list

hbase(main):007:0> describe 'users'

DESCRIPTION ENABLED

'users', {NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPL true

ICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', T

TL => '2147483647', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY =

> 'false', ENCODE_ON_DISK => 'true', BLOCKCACHE => 'true'}

1 row(s) in 0.0580 seconds

Displays table names and list of column families

II. Establishing connections

The Htable object is used to communicate with an Hbase table.

Creating a table instance is an expensive operation that consumes network resources.

Configuration myconf = new Configuration();

//HTableInterface users = new HTable(myconf,"users"); //Create a table instance. It's expensive.

HConnection hc = HConnectionManager.getConnection(myconf);

HTableInterface users = hc.getTable("users"); //getTable using connection pool.

users.close();

2. Data manipulation:

The rows of the HBase table have unique identifiers called rowkeys.

There are 5 basic commands to access HBase: Get, Put, Delete, Scan, Increment.

All data in Hbase is stored as raw data in the form of "byte arrays," as are row keys.

Submission of data:

Use Put objects to store data to insert/commit.

Coordinates are used in HBase to locate data in a table. The row key is the first coordinate, the next is the column family, and the next coordinate is the column qualifier. Column qualifiers are name, email, and password.

Put p = new Put(Bytes.toBytes("TheRealMT"));

p.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Mark Twain"));

p.add(Bytes.toBytes("info"), Bytes.toBytes("email"), Bytes.toBytes("mt@google.com"));

p.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes("lamborgini"));

Modify data:

Modify data in the same way as insert data, using Put objects.

Working mechanism: HBase write path

By default, writes occur in two places: the write-ahead log (also known as HLog) and MemStore. MemStore is a write buffer in memory. HFile is the underlying storage format used by HBase.

If the HBase server goes down, data not flushed from MemStore to HFile will be restored via WAL.

Read the data:

Create a Get command instance. Result r = users.get(g);

HBase uses LRU(Least Recently Used Algorithm) caching technology for read operations. Also known as BlockCache, MemStore is in a JVM heap.

Delete data:

The Delete command does not delete the data immediately, that is, it is not physically deleted, it is simply marked for deletion. HFile files cannot be stored in the table until a large merge is performed, and the tombstone records are not processed until the space occupied by the deleted records is released.

Mergers are divided into small mergers and large mergers. Small merge is to merge multiple small HFiles into one large HFile, while large merge processes all HFiles of a column family at the same time

Data with time versions:

HBase is a database that has no schema, but also has a time version.

HBase Data Model:

Table:

Line:

Family:

Column qualifiers:

Cell: Row keys, column families, and column qualifiers together define a cell.

Time version: Each cell value has a time version.

The following code involves the data in the HBase table, adding, modifying, deleting, and reading data methods, TablesControl.java

package com.pmpa.hbase;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.HConnection;import org.apache.hadoop.hbase.client.HConnectionManager;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.util.Bytes;/** * HBase table operation, add, delete and modify. * @author Neil * @Date 2016-07-18 */public class TablesControl { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Configuration myconf = new Configuration(); myconf.set("hbase.zookeeper.quorum", "10.101.22.31"); myconf.set("hbase.zookeeper.property.clientPort", "5181"); HTableInterface users = new HTable(myconf,"users"); // HConnection hc = HConnectionManager.getConnection(myconf);// HTableInterface users = hc.getTable("users"); //Insert data. //Create Put pairs like Put p = new Put(Bytes.toBytes("TheRealMT")); p.add(Bytes.toBytes("info"), Bytes. toBytes("name"), Bytes. toBytes ("Mark Twain")); p.add(Bytes.toBytes("info"), Bytes.toBytes("email"), Bytes. toBytes("mt@google.com")); p.add(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes. toBytes ("lamborgini")); users.put(p); //modify data Put p1 = new Put(Bytes.toBytes("TheRealMT")); p1.add(Bytes.toBytes("info"), Bytes. toBytes("password"), Bytes. toBytes("Porsche")); users.put(p1); Get g = new Get (Bytes.toBytes("TheRealMT")); g.addColumn (Bytes.toBytes("info"), Bytes.toBytes("name")); g.addColumn (Bytes.toBytes("info"), Bytes.toBytes ("password")); Result r = users.get(g); byte[] b = r.getValue (Bytes.toBytes("info"), Bytes.toBytes("name")); System.out.println("The name of the one is " + Bytes.toString(b)); //Delete operation: Delete del = new Delete(Bytes.toBytes("TheRealMT")); del.deleteColumns(Bytes.toBytes("info"), Bytes.toBytes("password")); //deleteColumns is the delete column identifier, deleteColumn is the key to delete a row directly. users.delete(del); users.close(); }}

3. Data coordinates:

The coordinates used by HBase are row keys, column families, column qualifiers, and time versions. Considering all coordinates as a whole, HBase can be thought of as a database of keys. Looking at the logic model abstractly, coordinates can be regarded as keys and unit data as values.

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

Database

Wechat

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

12
Report