HBase Java Programming Example
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 ();
- /**
- * Initialize Configuration
- */
- static {
- conf = Hbaseconfiguration.create ();
- If you don't have a profile, be sure to declare it.
- 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<100; ++j) {
- for (int i=0; i<10000000; ++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 ();
- }
- }
- }
- /**
- * Create a form
- * @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 < cfs.length; 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 ();
- }
- }
- /**
- * Delete Table
- * @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 ();
- }
- }
- /**
- * Delete a piece of information
- * @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 ();
- }
- }
- /**
- * Get a piece of information
- * @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 ();
- }
- }
- /**
- * Get all the information
- * @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 ();
- }
- }
- /**
- * Get Family checklist
- * @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:", $)} ' 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 < 0)
- 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.html www.bkjia.com true http://www.bkjia.com/PHPjc/1109310.html techarticle HBase Java Programming Example Helloworld.zip package elementary; Import java.io.IOException; import Java.text.SimpleDateFormat; Import java.util.ArrayList; Import Java.util.Date; Import J ...