h base introduction & development

24
HBASE INTRODUCTION & DEVELOPMENT The term planet-size web application comes to mind, and in this case it is fitting

Upload: shashwat-shriparv

Post on 09-May-2015

796 views

Category:

Technology


0 download

DESCRIPTION

HBase Development Java

TRANSCRIPT

Page 1: H base introduction & development

HBASE INTRODUCTION & DEVELOPMENT

The term planet-size web application comes to mind, and in this case it is fitting

Page 2: H base introduction & development

WHAT IS IT?

• It is the Hadoop database,

• Sclable

• Distributed

• BigDatastore

• Column Oriented

HBASE

HDFS

Reader Writer

Page 3: H base introduction & development

FEATURES OF HBASE

• Scalable.

• Automatic failover

•Consistent reads and writes.

• Sharding of tables

• Failover support

•Classes for backing Hadoop map reduce jobs

• Java API for client access

• Thrift gateway and a REST Web

• Shell support

Page 4: H base introduction & development

WHAT IT IS NOT

•No-Sql

•No relation

•No joins

•Not a replacement of RDBMS

Page 5: H base introduction & development

• NoSQL

• HBase is a type of "NoSQL" database. "NoSQL" is a general term meaning that the database isn't an RDBMS which supports SQL as its primary access language.

• When we should think of using it

• HBase isn't suitable for every problem. We should have lot of data, if data is less RDBMS is better.

• Difference Between HDFS and HBase

• HDFS is a distributed file system that is well suited for the storage of large files. It's documentation states that it is not, however, a general purpose file system, and does not provide fast individual record lookups in files. HBase, on the other hand, is built on top of HDFS and provides fast record lookups (and updates) for large tables.

Page 6: H base introduction & development

THINK ON THIS

• Facebook, for example, is adding more than 15 TB, and processing daily

• Google adding Peta-Bytes of data and processing.

• Companies storing Logs, temperature details, and many other prospective to store and process, which come in Peta-byte for which conventional technologies will days to read the data forget about processing it.

Page 7: H base introduction & development

WHAT IS COLUMNS ORIENTED MEANS

• Grouped by columns,

• The reason to store values on a per-column basis instead is based on the assumption

• that, for specific queries, not all of the values are needed.

• Reduced I/O

Page 8: H base introduction & development

COMPONENTS

Page 9: H base introduction & development

HMASTER

• Master server is responsible for monitoring all RegionServer instances in the cluster, and is the interface for all metadata changes, it runs on the server which hosts namenode.

• Master controls critical functions such as RegionServer failover and completing region splits. So while the cluster can still run for a time without the Master, the Master should be restarted as soon as possible.

Page 10: H base introduction & development

ZOOKEEPER

• Zookeeper is an open source software providing a highly reliable, distributed coordination service

• Entry point for an HBase system

• It includes tracking of region servers, where the root region is hosted

Page 11: H base introduction & development

API

• Interface to HBase

• Using these we can we can access HBase and perform read/write and other operation on HBase.

• REST, Thrift, and Avro

• Thrift API framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.

Page 12: H base introduction & development

• lib  

• commons-configuration-1.8.jar  

• commons-lang-2.6.jar  

• commons-logging-1.1.1.jar  

• hadoop-core-1.0.0.jar  

• hbase-0.92.1.jar  

• log4j-1.2.16.jar  

• slf4j-api-1.5.8.jar  

• slf4j-log4j12-1.5.8.jar  

• zookeeper-3.4.3.jar

Header Files you need to import

Page 13: H base introduction & development

•  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.MasterNotRunningException;

• import org.apache.hadoop.hbase.ZooKeeperConnectionException;

• 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.Result;

• import org.apache.hadoop.hbase.client.ResultScanner;

• import org.apache.hadoop.hbase.client.Scan;

• import org.apache.hadoop.hbase.client.Put;

• import org.apache.hadoop.hbase.util.Bytes;

Header Files you need to import

Page 14: H base introduction & development

• Configuration hConf = HBaseConfiguration.create(conf); hConf.set(Constants.HBASE_CONFIGURATION_ZOOKEEPER_QUORUM, hbaseZookeeperQuorum); hConf.set(Constants.HBASE_CONFIGURATION_ZOOKEEPER_CLIENTPORT, hbaseZookeeperClientPort); HTable hTable = new HTable(hConf, tableName);

Page 15: H base introduction & development

• public class HBaseTest {

• private static Configuration conf = null;

• /**

• * Initialization

• */

• static {

• conf = HBaseConfiguration.create();

• }

• }

Page 16: H base introduction & development

• /** Create a table

• */

• public static void creatTable(String tableName, String[] familys)

• throws Exception { HBaseAdmin admin = new HBaseAdmin(conf);

• if (admin.tableExists(tableName)) {System.out.println("table already exists!");

• } else { HTableDescriptor tableDesc = new HTableDescriptor(tableName);

• for (int i = 0; i < familys.length; i++) {

• tableDesc.addFamily(new HColumnDescriptor(familys[i]));

• }admin.createTable(tableDesc);

• System.out.println("create table " + tableName + " ok.");

• }

• }

•  

Page 17: H base introduction & development

• /** * Delete a table

• */

• public static void deleteTable(String tableName) throws Exception {

• try {HBaseAdmin admin = new HBaseAdmin(conf);

• admin.disableTable(tableName);

• admin.deleteTable(tableName);

• System.out.println("delete table " + tableName + " ok.");

• } catch (MasterNotRunningException e) {

• e.printStackTrace();

• } catch (ZooKeeperConnectionException e) {

• e.printStackTrace();

• }

• }

Page 18: H base introduction & development

• /**

• * Put (or insert) a row

• */

• public static void addRecord(String tableName, String rowKey,String family, String qualifier, String value) throws Exception {

• try {HTable table = new HTable(conf, tableName);

• Put put = new Put(Bytes.toBytes(rowKey));

• put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes

• .toBytes(value));

• table.put(put);

• System.out.println("insert recored " + rowKey + " to table "

• + tableName + " ok.");

• } catch (IOException e) {e.printStackTrace();

• }

• }

Page 19: H base introduction & development

• /**

• * Delete a row

• */

• public static void delRecord(String tableName, String rowKey)

• throws IOException {HTable table = new HTable(conf, tableName);

• List<Delete> list = new ArrayList<Delete>();

• Delete del = new Delete(rowKey.getBytes());

• list.add(del);

• table.delete(list);

• System.out.println("del recored " + rowKey + " ok.");

• }

Page 20: H base introduction & development

• /**Get a row

• */

• public static void getOneRecord (String tableName, String rowKey) throws IOException{

• HTable table = new HTable(conf, tableName);

• Get get = new Get(rowKey.getBytes());

• Result rs = table.get(get);

• for(KeyValue kv : rs.raw()){

• System.out.print(new String(kv.getRow()) + " " );

• System.out.print(new String(kv.getFamily()) + ":" );

• System.out.print(new String(kv.getQualifier()) + " " );

• System.out.print(kv.getTimestamp() + " " );

• System.out.println(new String(kv.getValue()));

• }

• }

Page 21: H base introduction & development

• /** Scan (or list) a table */

• public static void getAllRecord (String tableName) {

• try{

• HTable table = new HTable(conf, tableName);

• Scan s = new Scan();

• ResultScanner ss = table.getScanner(s);

• for(Result r:ss){

• for(KeyValue kv : r.raw()){

• System.out.print(new String(kv.getRow()) + " ");

• System.out.print(new String(kv.getFamily()) + ":"); System.out.print(new String(kv.getQualifier()) + " ");

• System.out.print(kv.getTimestamp() + " "); System.out.println(new String(kv.getValue())); } }

• } catch (IOException e){

• e.printStackTrace();

• }

• }

Page 22: H base introduction & development

• public static void main(String[] agrs) {

• try {

• String tablename = "scores";

• String[] familys = { "grade", "course" };

• HBaseTest.creatTable(tablename, familys);

• // add record zkb

• HBaseTest.addRecord(tablename, "zkb", "grade", "", "5");

• HBaseTest.addRecord(tablename, "zkb", "course", "", "90");

• HBaseTest.addRecord(tablename, "zkb", "course", "math", "97");

• HBaseTest.addRecord(tablename, "zkb", "course", "art", "87");

• // add record baoniu

• HBaseTest.addRecord(tablename, "baoniu", "grade", "", "4");

• HBaseTest.addRecord(tablename, "baoniu", "course", "math", "89");

•  

Page 23: H base introduction & development

• System.out.println("===========get one record========");

• HBaseTest.getOneRecord(tablename, "zkb");

•  System.out.println("===========show all record========");

• HBaseTest.getAllRecord(tablename);

• System.out.println("===========del one record========");

• HBaseTest.delRecord(tablename, "baoniu");

• HBaseTest.getAllRecord(tablename);

•  System.out.println("===========show all record========");

• HBaseTest.getAllRecord(tablename);

• } catch (Exception e) {

• e.printStackTrace();

• }

• }}

Page 24: H base introduction & development

Gmail [email protected]

Twitter shashwat_2010

Facebook [email protected]

Skype shriparv