HBase Java Programming Example _php tutorial

Source: Internet
Author: User

HBase Java Programming Example


Helloworld.zip
  1. Package Elementary;

  2. Import java.io.IOException;
  3. Import Java.text.SimpleDateFormat;
  4. Import java.util.ArrayList;
  5. Import Java.util.Date;
  6. Import java.util.List;
  7. Import Java.util.concurrent.atomic.AtomicInteger;
  8. Import Java.util.concurrent.ExecutorService;
  9. Import java.util.concurrent.Executors;
  10. Import Java.util.concurrent.TimeUnit;

  11. Import org.apache.hadoop.conf.Configuration;
  12. Import Org.apache.hadoop.hbase.Cell;
  13. Import org.apache.hadoop.hbase.HBaseConfiguration;
  14. Import Org.apache.hadoop.hbase.HColumnDescriptor;
  15. Import Org.apache.hadoop.hbase.HTableDescriptor;
  16. Import org.apache.hadoop.hbase.MasterNotRunningException;
  17. Import Org.apache.hadoop.hbase.TableName;
  18. Import org.apache.hadoop.hbase.ZooKeeperConnectionException;
  19. Import Org.apache.hadoop.hbase.client.Delete;
  20. Import Org.apache.hadoop.hbase.client.Get;
  21. Import Org.apache.hadoop.hbase.client.Admin;
  22. Import Org.apache.hadoop.hbase.client.BufferedMutator;
  23. Import Org.apache.hadoop.hbase.client.BufferedMutatorParams;
  24. Import org.apache.hadoop.hbase.client.Connection;
  25. Import Org.apache.hadoop.hbase.client.ConnectionFactory;
  26. Import org.apache.hadoop.hbase.client.Table;
  27. Import Org.apache.hadoop.hbase.client.Put;
  28. Import Org.apache.hadoop.hbase.client.Result;
  29. Import Org.apache.hadoop.hbase.client.ResultScanner;
  30. Import Org.apache.hadoop.hbase.client.Scan;
  31. Import org.apache.hadoop.hbase.util.Bytes;
  32. Import Org.apache.hadoop.util.ThreadUtil;

  33. public class HelloWorld {
  34. private static Configuration conf = null;
  35. private static Connection conn = null;
  36. private static admin admin = null;
  37. public static Atomicinteger count = new Atomicinteger ();

  38. /**
  39. * Initialize Configuration
  40. */
  41. static {
  42. conf = Hbaseconfiguration.create ();
  43. If you don't have a profile, be sure to declare it.

  44. Conf.set ("Hbase.zookeeper.quorum", "10.148.137.143");
  45. Conf.set ("Hbase.zookeeper.property.clientPort", "2181");
  46. }

  47. static {
  48. try {
  49. conn = Connectionfactory.createconnection ();
  50. admin = conn.getadmin ();
  51. } catch (IOException e) {
  52. E.printstacktrace ();
  53. }
  54. }

  55. static public class MyThread extends Thread
  56. {
  57. int _start;
  58. String _tablename;
  59. Connection Conn;
  60. Bufferedmutator table;
  61. Table table;

  62. Public MyThread (int start, String tablename) {
  63. _start = start;
  64. _tablename = tablename;
  65. }

  66. public void Run () {
  67. String tablename = _tablename;
  68. Thread current = Thread.CurrentThread ();
  69. Long thread_id = Current.getid ();
  70. System.out.printf ("thread[%d] run\n", thread_id);

  71. try {
  72. conn = Connectionfactory.createconnection ();
  73. Bufferedmutatorparams params = new Bufferedmutatorparams (tablename.valueof (TableName));
  74. Params.writebuffersize (1024 * 4);
  75. Table = Conn.getbufferedmutator (params);
  76. Table = conn.gettable (tablename.valueof (TableName));

  77. for (int j=_start; j<100; ++j) {
  78. for (int i=0; i<10000000; ++i) {
  79. Zkb_0_0
  80. String zkb = "Zkb_" + string.valueof (_start) + "_" + string.valueof (i);
  81. Put put = new put (Bytes.tobytes (ZKB));
  82. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("field1"), Bytes.tobytes (string.valueof (i+0)));
  83. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field2"), Bytes.tobytes (string.valueof (i+1)));
  84. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("field3"), Bytes.tobytes (string.valueof (i+2)));
  85. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field4"), Bytes.tobytes (string.valueof (i+3)));
  86. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field5"), Bytes.tobytes (string.valueof (i+4)));
  87. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field6"), Bytes.tobytes (string.valueof (i+5)));
  88. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field7"), Bytes.tobytes (string.valueof (i+6)));
  89. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field8"), Bytes.tobytes (string.valueof (i+7)));
  90. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("field9"), Bytes.tobytes (string.valueof (i+8)));
  91. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field10"), Bytes.tobytes (string.valueof (i+9)));
  92. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field11"), Bytes.tobytes (string.valueof (i+10)));
  93. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field12"), Bytes.tobytes (string.valueof (i+11)));
  94. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field13"), Bytes.tobytes (string.valueof (i+12)));
  95. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field14"), Bytes.tobytes (string.valueof (i+13)));
  96. Put.addcolumn (Bytes.tobytes ("Grade"), Bytes.tobytes ("Field15"), Bytes.tobytes (string.valueof (i+14)));
  97. Table.mutate (Put);
  98. Table.put (Put);

  99. int m = HelloWorld.count.incrementAndGet ();
  100. if (m% 10000 = = 0) {
  101. Date dt = new Date ();
  102. SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss AA");
  103. String now = sdf.format (DT);
  104. System.out.printf ("[%s] thread[%d] m=%d, j=%d, i=%d\n", now, thread_id, M, J, I);
  105. }
  106. }
  107. }

  108. System.out.printf ("thread[%d] over\n", thread_id);
  109. }
  110. catch (Exception e) {
  111. E.printstacktrace ();
  112. }
  113. }
  114. }

  115. /**
  116. * Create a form
  117. * @param tablename
  118. * @param cfs
  119. */
  120. public static void CreateTable (String tablename, string[] cfs) {
  121. try {
  122. if (Admin.tableexists (tablename.valueof (TableName))) {
  123. SYSTEM.OUT.PRINTLN ("Table already exists!");
  124. } else {
  125. Htabledescriptor Tabledesc = new Htabledescriptor (tablename.valueof (TableName));
  126. for (int i = 0; i < cfs.length; i++) {
  127. Hcolumndescriptor desc = new Hcolumndescriptor (cfs[i]);
  128. Desc.setmaxversions (3650);
  129. Tabledesc.addfamily (DESC);
  130. }

  131. byte[][] Splitkeys = new byte[][] {
  132. Bytes.tobytes ("Zkb_0_0"),
  133. Bytes.tobytes ("Zkb_10_0"),
  134. Bytes.tobytes ("Zkb_20_0"),
  135. Bytes.tobytes ("Zkb_30_0"),
  136. Bytes.tobytes ("Zkb_40_0"),
  137. Bytes.tobytes ("Zkb_50_0"),
  138. Bytes.tobytes ("Zkb_60_0"),
  139. Bytes.tobytes ("Zkb_70_0"),
  140. Bytes.tobytes ("Zkb_80_0"),
  141. Bytes.tobytes ("Zkb_90_0"),
  142. Bytes.tobytes ("Zkb_100_0")
  143. };
  144. Admin.createtable (Tabledesc, Splitkeys);
  145. Admin.close ();
  146. SYSTEM.OUT.PRINTLN ("CREATE TABLE" + tablename + "OK.");
  147. }
  148. } catch (Masternotrunningexception e) {
  149. E.printstacktrace ();
  150. } catch (Zookeeperconnectionexception e) {
  151. E.printstacktrace ();
  152. } catch (IOException e) {
  153. E.printstacktrace ();
  154. }
  155. }

  156. /**
  157. * Delete Table
  158. * @param tablename
  159. */
  160. public static void Deletetable (String tablename) {
  161. try {
  162. Connection conn = Connectionfactory.createconnection ();
  163. Admin admin = conn.getadmin ();
  164. Admin.disabletable (tablename.valueof (TableName));
  165. Admin.deletetable (tablename.valueof (TableName));
  166. System.out.println ("delete table" + tablename + "OK.");
  167. } catch (IOException e) {
  168. E.printstacktrace ();
  169. }
  170. }

  171. /**
  172. * Delete a piece of information
  173. * @param tableName
  174. * @param RowKey
  175. */
  176. public static void Delrecord (String tableName, String rowKey) {
  177. try {
  178. Table table = conn.gettable (tablename.valueof (TableName));

  179. List list = new ArrayList ();
  180. Delete del = new Delete (Rowkey.getbytes ());
  181. List.add (DEL);
  182. Table.delete (list);
  183. System.out.println ("del recored" + RowKey + "OK.");
  184. } catch (IOException e) {
  185. E.printstacktrace ();
  186. }
  187. }

  188. /**
  189. * Get a piece of information
  190. * @param tableName
  191. * @param RowKey
  192. */
  193. public static void Getonerecord (String tableName, String rowKey) {
  194. try {
  195. Table table = conn.gettable (tablename.valueof (TableName));

  196. Get get = new Get (Rowkey.getbytes ());
  197. Result rs = Table.get (get);
  198. List list = Rs.listcells ();
  199. for (Cell cell:list) {
  200. System.out.print (New String (Cell.getrowarray (), Cell.getrowoffset (), Cell.getrowlength ()) + "");
  201. System.out.print (New String (Cell.getfamilyarray (), Cell.getfamilyoffset (), Cell.getfamilylength ()) + ":");
  202. System.out.print (New String (Cell.getqualifierarray (), Cell.getqualifieroffset (), Cell.getqualifierlength ()) + "");
  203. System.out.print (Cell.gettimestamp () + "");
  204. System.out.print (New String (Cell.getvaluearray (), Cell.getvalueoffset (), Cell.getvaluelength ()) + "");
  205. System.out.println ("");
  206. }
  207. } catch (IOException e) {
  208. E.printstacktrace ();
  209. }
  210. }

  211. /**
  212. * Get all the information
  213. * @param tableName
  214. */
  215. public static void Getallrecord (String tableName) {
  216. try{
  217. Connection conn = Connectionfactory.createconnection ();
  218. Table table = conn.gettable (tablename.valueof (TableName));

  219. Scan scan = new scan ();
  220. Resultscanner Resultscanner = Table.getscanner (scan);
  221. for (Result Rs:resultscanner) {
  222. List list = Rs.listcells ();
  223. for (Cell cell:list) {
  224. System.out.print (New String (Cell.getrowarray (), Cell.getrowoffset (), Cell.getrowlength ()) + "");
  225. System.out.print (New String (Cell.getfamilyarray (), Cell.getfamilyoffset (), Cell.getfamilylength ()) + ":");
  226. System.out.print (New String (Cell.getqualifierarray (), Cell.getqualifieroffset (), Cell.getqualifierlength ()) + "");
  227. System.out.print (Cell.gettimestamp () + "");
  228. System.out.print (New String (Cell.getvaluearray (), Cell.getvalueoffset (), Cell.getvaluelength ()) + "");
  229. System.out.println ("");
  230. }
  231. }
  232. } catch (IOException e) {
  233. E.printstacktrace ();
  234. }
  235. }

  236. /**
  237. * Get Family checklist
  238. * @param tableName
  239. * @return
  240. */
  241. public static ArrayList getallfamilyname (String tableName) {
  242. ArrayList familyname_list = new ArrayList ();
  243. try{
  244. Connection conn = Connectionfactory.createconnection ();
  245. Table table = conn.gettable (tablename.valueof (TableName));

  246. Htabledescriptor htabledescriptor = Table.gettabledescriptor ();
  247. hcolumndescriptor[] hdlist = Htabledescriptor.getcolumnfamilies ();
  248. for (int i=0;i<>
  249. Hcolumndescriptor HD = hdlist[i];
  250. Familyname_list.add (Hd.getnameasstring ());
  251. }
  252. } catch (IOException e) {
  253. E.printstacktrace ();
  254. }
  255. return familyname_list;
  256. }

  257. JAVA-CP helloworld.jar: ' ls lib/*.jar|awk ' {printf ("%s:", $)} ' elementary. HelloWorld 5
  258. public static void Main (string[] args) {
  259. System.out.println ("Helloworldx");
  260. if (Args.length > 0)
  261. System.out.println (Args[0]);

  262. int start = 0;
  263. if (Args.length > 1)
  264. Start = Integer.valueof (Args[1]);
  265. if (Start < 0)
  266. start = 0;

  267. int num_threads = 16;
  268. if (Args.length > 2)
  269. Num_threads = integer.valueof (args[2]);

  270. try {
  271. String tablename = "scores";
  272. String[] Familys = {"Grade", "course"};
  273. Helloworld.createtable (tablename, familys);

  274. Executorservice Thread_pool = Executors.newsinglethreadexecutor ();
  275. Executorservice Thread_pool = Executors.newfixedthreadpool (num_threads);
  276. thread[] pool = new HELLOWORLD.MYTHREAD[80];
  277. for (int i=0; i
  278. Pool[i] = new Helloworld.mythread (i, TableName);
  279. Thread_pool.execute (Pool[i]);
  280. }

  281. Thread_pool.shutdown ();
  282. System.out.println ("over");
  283. }
  284. catch (Exception e) {
  285. E.printstacktrace ();
  286. }
  287. }

  288. }

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

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.