HBase Java编程示例_PHP教程

php中文网
发布: 2016-07-12 08:57:20
原创
1499人浏览过

豆包AI编程
豆包AI编程

豆包推出的AI编程助手

豆包AI编程 483
查看详情 豆包AI编程

hbase java编程示例

HBase Java编程示例_PHP教程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. * 初始化配置
  40. */
  41. static {
  42. conf = hbaseconfiguration.create();
  43. //如果沒有配置文件,一定要記得手動宣告

  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
  78. for (int i=0; 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. * 建立表格
  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
  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. * 刪除表格
  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. * 刪除一筆資料
  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. * 取得一筆資料
  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. * 取得所有資料
  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. * 取得family清單
  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:", $0)}'` 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
  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. }

www.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...
相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号