Apache HBase

+_+ another note to read through HBase documentation... endless

 

http://hbase.apache.org/

This project's goal is the hosting of very large tables -- billions of rowsXmillions of columns -- atop clusters of commodity hardware.

HBase is really more a "Data Store" than "Data Base"

"NoSQL" is a general term meaning that the database isn't an RDBMS which supports SQL

 

 

JAVA API doc:

http://hbase.apache.org/apidocs/index.html

 

http://hbase.apache.org/book/architecture.html#arch.overview

When Should I Use HBase?

HBase isn't suitable for every problem.

First, make sure you have enough data. If you have hundreds of millions or billions of rows, then HBase is a good candidate. If you only have a few thousand/million rows, then using a traditional RDBMS might be a better choice due to the fact that all of your data might wind up on a single node (or two) and the rest of the cluster may be sitting idle.

 

0.97 - Chinese Version User Guide

http://abloz.com/hbase/book.html#shell_tricks

 

0.94 - Apache HBase official reference guide

http://hbase.apache.org/0.94/book.html

 一个 {row, column, version} 元组是HBase中的一个单元(cell)

 

 

hadoop1.2
hive0.11
sqoop1.4.3
HBase 0.94.6

build restful data service, and also hbase APIs la


1.HBase 0.94.6
http://abloz.com/hbase/book.html#client
Run on HDFS, Zookeeper
Master, RegionServer

 

---------------------------------------------------
Migrate Oracle to HBase
---------------------------------------------------
//Oracle Table Structure
msgId globalId msgSerial sysId ctryCde grpCde recBic msgType bsCde msgCrtDt msgCapDt msgStatus fullMsgTxt ghssMsg errCde errMsg remarks msgTsmDt msgAckDt swiftRef lastUptDt

//HBase Table - SME_OUT_MSG - Version 0.1
rowKey, columnFamily, version
?_msgId2, cf:{ctryCde, grpCde, msgType, ...}, t0
?_msgId1, cf:{ctryCde, grpCde, msgType, ...}, t9
?_msgId1, cf:{ctryCde, grpCde, msgType, ...}, t8
//? design to balance loading among more region servers..
//it's free to add column into cf  - 当表或列族改变时(如 region size, block size), 当下次存在主紧缩及存储文件重写时起作用。

尽量使列族名小,最好一个字符。(如 "d" 表示 data/default).
最好还是用短属性名 (e.g., "via") 保存到HBase
让行键短到可读即可,这样对获取数据有用(e.g., Get vs. Scan)。


//HBase Java Sample Codes
Configuration config = HBaseConfiguration.create(); 
HBaseAdmin admin = new HBaseAdmin(conf);   
String table = "myTable";

admin.disableTable(table);          

HColumnDescriptor cf1 = ...;
admin.addColumn(table, cf1);      // adding new ColumnFamily
HColumnDescriptor cf2 = ...;
admin.modifyColumn(table, cf2);    // modifying existing ColumnFamily

admin.enableTable(table);  

 

 


2.
It is recommended that the server follow a URL structure for resources as follows:

/{Table}/{id}


http://dojotoolkit.org/reference-guide/1.9/dojo/store/JsonRest.html

HTTP methods                   Store Actions                                      HTTP Response in Store
GET                                     get, query                                            404 Not found
DELETE                             remove                                                204 success, 404
PUT                                     add, put(has identity property)          n/a         
POST                                  add, put                                               n/a

 

require(["dojo/store/JsonRest"], function(JsonRest){
  var store = new JsonRest({
    target: "/Table"
  });

  var self = this;

  var results = store.query("foo=bar").then(function(data){
    results.total.then(function(total){
      console.log("total results: ", total);
      console.log("go on and use data ", data, " with this ", self);
    });
  });
});

 

 - how about its ACID

http://archfan.sinaapp.com/2012/04/18/acid-in-hbase/
==Hbase API==

@Spring Hbase

http://docs.spring.io/spring-data/hadoop/docs/2.0.0.M1/api/org/springframework/data/hadoop/hbase/HbaseTemplate.html

http://docs.spring.io/spring-hadoop/docs/1.0.1.RELEASE/reference/html/hbase.html



@HBase JAVA API Example:

package control.hb;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.Put;
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;

/**
 * Servlet implementation class HbaseServlet
 */
@WebServlet("/HbaseServlet")
public class HbaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PrintWriter writer = null;
	private static Configuration conf = null;
       
	static {
		Configuration HBASE_CONFIG = new Configuration();
		HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.1.146");
		HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
		conf = HBaseConfiguration.create(HBASE_CONFIG);
	}
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HbaseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		writer = response.getWriter();
		try{
			String tableName = "scores";
			String[] familys = {"grade", "course"};
			
			this.creatTable(tableName, familys);
			
			this.addRecord(tableName, "zkb", "grade", "", "5");
			this.addRecord(tableName, "zkb", "course", "", "90");
			this.addRecord(tableName, "zkb", "course", "math", "90");
			this.addRecord(tableName, "zkb", "course", "art", "50");
			
			this.addRecord(tableName, "paul", "grade", "", "10");
			this.addRecord(tableName, "zkb", "course", "ABC", "100");
			
			writer.println("=====get one record======");
			writer.println("<br>");
			this.getOneRecord(tableName, "zkb");
			
			writer.println("=====show all record======");
			writer.println("<br>");
			this.getAllRecord(tableName);
			
			writer.println("=====del one record======");
			writer.println("<br>");
			this.delRecord(tableName, "zkb");
			
			writer.println("=====show all record======");
			writer.println("<br>");
			this.getAllRecord(tableName);
			
			writer.close();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void creatTable(String tableName, String[] familys) throws Exception {
		HBaseAdmin admin = new HBaseAdmin(conf);
		if(admin.tableExists(tableName)) {
			System.out.println(writer==null);
			writer.println("table already exists!");
			writer.println("<br>");
		} else {
			HTableDescriptor tableDessc = new HTableDescriptor(tableName);
			for(int i=0; i<familys.length; i++){
				tableDessc.addFamily(new HColumnDescriptor(familys[i]));
			}
			admin.createTable(tableDessc);
			writer.println("create table " + tableName + "ok.");
			writer.println("<br>");
		}
	}
	
	public 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);
			writer.println("insert record " + rowKey + " to table " + tableName + "ok.");
			writer.println("<br>");
			
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteTable(String tableName)  throws Exception {
		try{
			HBaseAdmin admin = new HBaseAdmin(conf);
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			writer.println("delete table " + tableName + "ok.");
			writer.println("<br>");
		} catch(MasterNotRunningException e){
			e.printStackTrace();
		} catch(ZooKeeperConnectionException e){
			e.printStackTrace();
		}
	}
	
	public void delRecord (String tableName, String rowKey) throws Exception {
		try{
			HTable table = new HTable(conf, tableName);
			List list = new ArrayList();
			Delete del = new Delete(rowKey.getBytes());
			list.add(del);
			table.delete(list);
			writer.println("delete record " + rowKey + " ok.");
			writer.println("<br>");
			//table.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getOneRecord (String tableName, String rowKey) throws Exception {
		try{
			HTable table = new HTable(conf, tableName);
			Get get = new Get(rowKey.getBytes());
			Result rs = table.get(get);
			for(KeyValue kv : rs.raw()){
				writer.println(new String(kv.getRow()) + " ");
				writer.println("<br>");
				writer.println(new String(kv.getFamily()) + " ");
				writer.println("<br>");
				writer.println(new String(kv.getQualifier()) + " ");
				writer.println("<br>");
				writer.println(kv.getTimestamp() + " ");
				writer.println("<br>");
				writer.println(new String(kv.getValue()) + " ");
				writer.println("<br>");
			}
			//table.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getAllRecord (String tableName) throws Exception {
		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()){
					writer.println(new String(kv.getRow()) + " ");
					writer.println("<br>");
					writer.println(new String(kv.getFamily()) + " ");
					writer.println("<br>");
					writer.println(new String(kv.getQualifier()) + " ");
					writer.println("<br>");
					writer.println(kv.getTimestamp() + " ");
					writer.println("<br>");
					writer.println(new String(kv.getValue()) + " ");
					writer.println("<br>");
				}
			}
			//table.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值