hbase java编程示例

helloworld.zip
- package elementary;
- import java.io.ioexception;
- import java.text.simpledateformat;
- import java.util.arraylist;
- import java.util.date;
- import java.util.list;
- import java.util.concurrent.atomic.atomicinteger;
- import java.util.concurrent.executorservice;
- import java.util.concurrent.executors;
- import java.util.concurrent.timeunit;
- import org.apache.hadoop.conf.configuration;
- import org.apache.hadoop.hbase.cell;
- import org.apache.hadoop.hbase.hbaseconfiguration;
- import org.apache.hadoop.hbase.hcolumndescriptor;
- import org.apache.hadoop.hbase.htabledescriptor;
- import org.apache.hadoop.hbase.masternotrunningexception;
- import org.apache.hadoop.hbase.tablename;
- 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.admin;
- import org.apache.hadoop.hbase.client.bufferedmutator;
- import org.apache.hadoop.hbase.client.bufferedmutatorparams;
- import org.apache.hadoop.hbase.client.connection;
- import org.apache.hadoop.hbase.client.connectionfactory;
- import org.apache.hadoop.hbase.client.table;
- 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;
- import org.apache.hadoop.util.threadutil;
- public class helloworld {
- private static configuration conf = null;
- private static connection conn = null;
- private static admin admin = null;
- public static atomicinteger count = new atomicinteger();
- /**
- * 初始化配置
- */
- static {
- conf = hbaseconfiguration.create();
- //如果沒有配置文件,一定要記得手動宣告
- conf.set("hbase.zookeeper.quorum", "10.148.137.143");
- conf.set("hbase.zookeeper.property.clientport", "2181");
- }
- static {
- try {
- conn = connectionfactory.createconnection();
- admin = conn.getadmin();
- } catch (ioexception e) {
- e.printstacktrace();
- }
- }
- static public class mythread extends thread
- {
- int _start;
- string _tablename;
- connection conn;
- //bufferedmutator table;
- table table;
- public mythread(int start, string tablename) {
- _start = start;
- _tablename = tablename;
- }
- public void run() {
- string tablename = _tablename;
- thread current = thread.currentthread();
- long thread_id = current.getid();
- system.out.printf("thread[%d] run\n", thread_id);
- try {
- conn = connectionfactory.createconnection();
- //bufferedmutatorparams params = new bufferedmutatorparams(tablename.valueof(tablename));
- //params.writebuffersize(1024 * 4);
- //table = conn.getbufferedmutator(params);
- table = conn.gettable(tablename.valueof(tablename));
- for (int j=_start; j
- for (int i=0; i
- // zkb_0_0
- string zkb = "zkb_" + string.valueof(_start) + "_" + string.valueof(i);
- put put = new put(bytes.tobytes(zkb));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field1"),bytes.tobytes(string.valueof(i+0)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field2"),bytes.tobytes(string.valueof(i+1)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field3"),bytes.tobytes(string.valueof(i+2)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field4"),bytes.tobytes(string.valueof(i+3)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field5"),bytes.tobytes(string.valueof(i+4)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field6"),bytes.tobytes(string.valueof(i+5)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field7"),bytes.tobytes(string.valueof(i+6)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field8"),bytes.tobytes(string.valueof(i+7)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field9"),bytes.tobytes(string.valueof(i+8)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field10"),bytes.tobytes(string.valueof(i+9)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field11"),bytes.tobytes(string.valueof(i+10)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field12"),bytes.tobytes(string.valueof(i+11)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field13"),bytes.tobytes(string.valueof(i+12)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field14"),bytes.tobytes(string.valueof(i+13)));
- put.addcolumn(bytes.tobytes("grade"),bytes.tobytes("field15"),bytes.tobytes(string.valueof(i+14)));
- //table.mutate(put);
- table.put(put);
- int m = helloworld.count.incrementandget();
- if (m % 10000 == 0) {
- date dt = new date();
- simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss aa");
- string now = sdf.format(dt);
- system.out.printf("[%s] thread[%d] m=%d, j=%d, i=%d\n", now, thread_id, m, j, i);
- }
- }
- }
- system.out.printf("thread[%d] over\n", thread_id);
- }
- catch (exception e) {
- e.printstacktrace();
- }
- }
- }
- /**
- * 建立表格
- * @param tablename
- * @param cfs
- */
- public static void createtable(string tablename, string[] cfs){
- try {
- if (admin.tableexists(tablename.valueof(tablename))) {
- system.out.println("table already exists!");
- } else {
- htabledescriptor tabledesc = new htabledescriptor(tablename.valueof(tablename));
- for (int i = 0; i
- hcolumndescriptor desc = new hcolumndescriptor(cfs[i]);
- desc.setmaxversions(3650);
- tabledesc.addfamily(desc);
- }
- byte[][] splitkeys = new byte[][] {
- bytes.tobytes("zkb_0_0"),
- bytes.tobytes("zkb_10_0"),
- bytes.tobytes("zkb_20_0"),
- bytes.tobytes("zkb_30_0"),
- bytes.tobytes("zkb_40_0"),
- bytes.tobytes("zkb_50_0"),
- bytes.tobytes("zkb_60_0"),
- bytes.tobytes("zkb_70_0"),
- bytes.tobytes("zkb_80_0"),
- bytes.tobytes("zkb_90_0"),
- bytes.tobytes("zkb_100_0")
- };
- admin.createtable(tabledesc, splitkeys);
- admin.close();
- system.out.println("create table " + tablename + " ok.");
- }
- } catch (masternotrunningexception e) {
- e.printstacktrace();
- } catch (zookeeperconnectionexception e) {
- e.printstacktrace();
- } catch (ioexception e) {
- e.printstacktrace();
- }
- }
- /**
- * 刪除表格
- * @param tablename
- */
- public static void deletetable(string tablename){
- try {
- //connection conn = connectionfactory.createconnection();
- //admin admin = conn.getadmin();
- admin.disabletable(tablename.valueof(tablename));
- admin.deletetable(tablename.valueof(tablename));
- system.out.println("delete table " + tablename + " ok.");
- } catch (ioexception e) {
- e.printstacktrace();
- }
- }
- /**
- * 刪除一筆資料
- * @param tablename
- * @param rowkey
- */
- public static void delrecord (string tablename, string rowkey){
- try {
- table table = conn.gettable(tablename.valueof(tablename));
- list list = new arraylist();
- delete del = new delete(rowkey.getbytes());
- list.add(del);
- table.delete(list);
- system.out.println("del recored " + rowkey + " ok.");
- } catch (ioexception e) {
- e.printstacktrace();
- }
- }
- /**
- * 取得一筆資料
- * @param tablename
- * @param rowkey
- */
- public static void getonerecord (string tablename, string rowkey){
- try {
- table table = conn.gettable(tablename.valueof(tablename));
-
- get get = new get(rowkey.getbytes());
- result rs = table.get(get);
- list
list = rs.listcells();
|
- for(cell cell:list){
- system.out.print(new string(cell.getrowarray(),cell.getrowoffset(),cell.getrowlength()) + " " );
- system.out.print(new string(cell.getfamilyarray(),cell.getfamilyoffset(),cell.getfamilylength()) + ":" );
- system.out.print(new string(cell.getqualifierarray(),cell.getqualifieroffset(),cell.getqualifierlength()) + " " );
- system.out.print(cell.gettimestamp() + " " );
- system.out.print(new string(cell.getvaluearray(),cell.getvalueoffset(),cell.getvaluelength()) + " " );
- system.out.println("");
- }
- } catch (ioexception e) {
- e.printstacktrace();
- }
- }
- /**
- * 取得所有資料
- * @param tablename
- */
- public static void getallrecord (string tablename) {
- try{
- //connection conn = connectionfactory.createconnection();
- table table = conn.gettable(tablename.valueof(tablename));
- scan scan = new scan();
- resultscanner resultscanner = table.getscanner(scan);
- for(result rs:resultscanner){
- list
list = rs.listcells();
|
- for(cell cell:list){
- system.out.print(new string(cell.getrowarray(),cell.getrowoffset(),cell.getrowlength()) + " " );
- system.out.print(new string(cell.getfamilyarray(),cell.getfamilyoffset(),cell.getfamilylength()) + ":" );
- system.out.print(new string(cell.getqualifierarray(),cell.getqualifieroffset(),cell.getqualifierlength()) + " " );
- system.out.print(cell.gettimestamp() + " " );
- system.out.print(new string(cell.getvaluearray(),cell.getvalueoffset(),cell.getvaluelength()) + " " );
- system.out.println("");
- }
- }
- } catch (ioexception e){
- e.printstacktrace();
- }
- }
- /**
- * 取得family清單
- * @param tablename
- * @return
- */
- public static arraylist getallfamilyname(string tablename) {
- arraylist familyname_list = new arraylist();
- try{
- //connection conn = connectionfactory.createconnection();
- table table = conn.gettable(tablename.valueof(tablename));
- htabledescriptor htabledescriptor = table.gettabledescriptor();
- hcolumndescriptor[] hdlist = htabledescriptor.getcolumnfamilies();
- for(int i=0;i
- hcolumndescriptor hd = hdlist[i];
- familyname_list.add(hd.getnameasstring());
- }
- } catch (ioexception e){
- e.printstacktrace();
- }
- return familyname_list;
- }
- // java -cp helloworld.jar:`ls lib/*.jar|awk '{printf("%s:", $0)}'` elementary.helloworld 5
- public static void main(string[] args) {
- system.out.println("helloworldx");
- if (args.length > 0)
- system.out.println(args[0]);
- int start = 0;
- if (args.length > 1)
- start = integer.valueof(args[1]);
- if (start
- start = 0;
- int num_threads = 16;
- if (args.length > 2)
- num_threads = integer.valueof(args[2]);
- try {
- string tablename = "scores";
- string[] familys = {"grade", "course"};
- helloworld.createtable(tablename, familys);
- //executorservice thread_pool = executors.newsinglethreadexecutor();
- executorservice thread_pool = executors.newfixedthreadpool(num_threads);
- thread[] pool = new helloworld.mythread[80];
- for (int i=0; i
- pool[i] = new helloworld.mythread(i, tablename);
- thread_pool.execute(pool[i]);
- }
- thread_pool.shutdown();
- system.out.println("over");
- }
- catch (exception e) {
- e.printstacktrace();
- }
- }
- }
http://www.bkjia.com/PHPjc/1109310.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1109310.htmlTechArticleHBase Java编程示例 HelloWorld.zip package elementary; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import j...