7/28/2019 HBase Development Java
1/24
HBASE INTRODUCTIODEVELOPMENT
The term planet-size web application comes to mind, and in this
7/28/2019 HBase Development Java
2/24
WHAT IS IT?
It is the Hadoop database,
Sclable
Distributed
BigDatastore
Column Oriented
HBASE
HDFS
Reader
7/28/2019 HBase Development Java
3/24
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
7/28/2019 HBase Development Java
4/24
WHAT IT IS NOT
No-Sql
No relation
No joins
Not a replacement of RDBMS
7/28/2019 HBase Development Java
5/24
NoSQL
HBase is a type of "NoSQL" database. "NoSQL" is a general term me
database isn't an RDBMS which supports SQL as its primary access langua
When we should think of using it
HBase isn't suitable for every problem. We should have lot of data,
RDBMS is better.
Difference Between HDFS and HBase
HDFS is a distributed file system that is well suited for the storage of
documentation states that it is not, however, a general purpose file system
provide fast individual record lookups in files. HBase, on the other hand, is
HDFS and provides fast record lookups (and updates) for large tables.
7/28/2019 HBase Development Java
6/24
THINK ONTHIS
Facebook, for example, is adding more than 15 TB, and proces
Google adding Peta-Bytes of data and processing.
Companies storing Logs, temperature details, and many other
to store and process, which come in Peta-byte for which convetechnologies will days to read the data forget about processing
7/28/2019 HBase Development Java
7/24
WHAT IS COLUMNS ORIENTEDMEANS 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
7/28/2019 HBase Development Java
8/24
COMPONENTS
7/28/2019 HBase Development Java
9/24
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
7/28/2019 HBase Development Java
10/24
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
7/28/2019 HBase Development Java
11/24
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 otherlan ua es.
7/28/2019 HBase Development Java
12/24
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
7/28/2019 HBase Development Java
13/24
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;
7/28/2019 HBase Development Java
14/24
Configuration hConf = HBaseConfiguration.create(conf);
hConf.set(Constants.HBASE_CONFIGURATION_ZOOKEEP
RUM, hbaseZookeeperQuorum);
hConf.set(Constants.HBASE_CONFIGURATION_ZOOKEEP
NTPORT, hbaseZookeeperClientPort); HTable hTable = new
HTable(hConf, tableName);
7/28/2019 HBase Development Java
15/24
/** Create a table
7/28/2019 HBase Development Java
16/24
/ Create a table
*/
public static void creatTable(String tableName, String[] familys)
throws Exception { HBaseAdmin adm
HBaseAdmin(conf); if (admin.tableExists(tableName)) { System.out.println
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 + " o
}
/** * D l t t bl
7/28/2019 HBase Development Java
17/24
/** * Delete a table
*/
public static void deleteTable(String tableName) throws Exce
try {HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("delete table " + tableName
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
}
/**
7/28/2019 HBase Development Java
18/24
/
* Put (or insert) a row
*/
public static void addRecord(String tableName, String rowKe
String family, String qualifier, String value) throws Exc
try {HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(family), Bytes.toBytes(
Bytes
.toBytes(value));
table.put(put);
System.out.println("insert recored " + rowKey
table "
+ tableName + " ok.");
} catch (IOException e) {e.printStackTrace();
/**
7/28/2019 HBase Development Java
19/24
/
* Delete a row
*/
public static void delRecord(String tableName, String ro
throws IOException {HTable table = new HTable(conf,
tableName);
List list = new ArrayList();
Delete del = new Delete(rowKey.getBytes());
list.add(del);
table.delete(list);
System.out.println("del recored " + rowKey + " ok
}
/**Get a row
7/28/2019 HBase Development Java
20/24
/ Get a row
*/
public static void getOneRecord (String tableName, String row
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()));
}
}
/** Scan (or list) a table */
7/28/2019 HBase Development Java
21/24
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.pr
String(kv.getQualifier()) + " ");
System.out.print(kv.getTimestamp() + " "); System.o
String(kv.getValue())); } }
} catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] agrs) {
7/28/2019 HBase Development Java
22/24
p ( g[] g ) {
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");
7/28/2019 HBase Development Java
23/24
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();
}
}}
7/28/2019 HBase Development Java
24/24
Gmail [email protected]
Twitter shashwat_2010
Facebook [email protected]
Skype shriparv