ホームページ  >  記事  >  バックエンド開発  >  HBase Java プログラミング例_PHP チュートリアル

HBase Java プログラミング例_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-12 08:57:201137ブラウズ

HBase Java プログラムの例

HBase Java プログラミング例_PHP チュートリアルHelloWorld.zip
  1. package Elementary;

  2. import java.io.IOException;
  3. import java.text.SimpleDateFormat;
  4. 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"); = conn.getAdmin();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }

  50. static public class MyThread extends Thread
  51. {
  52. int _start;
  53. String _tablename;
  54. Connection conn;
  55. //BufferedMutator table;
  56. Table table;

  57. public MyThread(int start) 、文字列テーブル名) {
  58. _start = 開始;
  59. _tablename = テーブル名;
  60. }

  61. public void run() {
  62. String tablename = _tablename;
  63. スレッドcurrent = Thread.currentThread();
  64. long thread_id = current.getId();
  65. System.out.printf("thread[%d] runn", thread_id);

  66. try {
  67. conn = ConnectionFactory.createConnection( );
  68. //BufferedMutatorParams params = new BufferedMutatorParams(TableName.valueOf(tablename));
  69. //params.writeBufferSize(1024 * 4);
  70. //table = conn.getBufferedMutator(params);
  71. table = conn.getTable(TableName.valueOf(tablename));

  72. for (int j=_start; j
  73. for (int i=0; i
  74. // zkb_0_0
  75. String zkb = "zkb_" + String.valueOf(_start) + "_" + String.valueOf(i);
  76. Put put = new Put( Bytes.toBytes(zkb));
  77. put.addColumn(Bytes.toBytes("グレード"),Bytes.toBytes("フィールド1"),Bytes.toBytes(String.valueOf(i+0)));
  78. put.addColumn(Bytes.toBytes("グレード"),Bytes.toBytes("フィールド2"),Bytes.toBytes(String.valueOf(i+1)));
  79. put.addColumn(Bytes.toBytes ("グレード"),Bytes.toBytes("フィールド3"),Bytes.toBytes(String.valueOf(i+2)));
  80. put.addColumn(Bytes.toBytes("グレード"),Bytes.toBytes( "field4"),Bytes.toBytes(String.valueOf(i+3)));
  81. put.addColumn(Bytes.toBytes("grade"),Bytes.toBytes("field5"),Bytes.toBytes(String .valueOf(i+4)));
  82. put.addColumn(Bytes.toBytes("grade"),Bytes.toBytes("field6"),Bytes.toBytes(String.valueOf(i+5)));
  83. put.addColumn(Bytes.toBytes( "グレード"),Bytes.toBytes("フィールド7"),Bytes.toBytes(String.valueOf(i+6)));
  84. put.addColumn(Bytes.toBytes("グレード"),Bytes.toBytes(" field8"),Bytes.toBytes(String.valueOf(i+7)));
  85. put.addColumn(Bytes.toBytes("grade"),Bytes.toBytes("field9"),Bytes.toBytes(String. valueOf(i+8)));
  86. put.addColumn(Bytes.toBytes("グレード"),Bytes.toBytes("フィールド10"),Bytes.toBytes(String.valueOf(i+9)));
  87. put.addColumn(Bytes.toBytes("grade"),Bytes.toBytes("field11"),Bytes.toBytes(String.valueOf(i+10)));
  88. put.addColumn(Bytes.toBytes( "グレード"),Bytes.toBytes("フィールド12"),Bytes.toBytes(String.valueOf(i+11)));
  89. put.addColumn(Bytes.toBytes("グレード"),Bytes.toBytes(" field13"),Bytes.toBytes(String.valueOf(i+12)));
  90. put.addColumn(Bytes.toBytes("grade"),Bytes.toBytes("field14"),Bytes.toBytes(String. valueOf(i+13)));
  91. put.addColumn(Bytes.toBytes("グレード"),Bytes.toBytes("フィールド15"),Bytes.toBytes(String.valueOf(i+14)));
  92. //table.mutate(put);
  93. table.put(put);

  94. int m = HelloWorld.count.incrementAndGet();
  95. if (m % 10000 == 0) {
  96. Date dt = new Date();
  97. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa");
  98. String now = sdf.format(dt);
  99. System.out.printf("[%s] thread[%d] m=%d, j=%d, i=%dn", now, thread_id, m, j, i);
  100. }
  101. }
  102. }

  103. System.out.printf("thread[%d] overn", thread_id);
  104. }
  105. catch (例外 e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. }

  110. /**
  111. * テーブルを作成します
  112. * @param tablename
  113. * @param cfs
  114. */
  115. public静的 void createTable (String tablename, String[] cfs){
  116. try {
  117. if (admin.tableExists(TableName.valueOf(tablename))) {
  118. System.out.println("テーブルはすでに存在します!");
  119. } else {
  120. HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
  121. for (int i = 0; i < cfs.length; i++) {
  122. HColumnDescriptor desc = new HColumnDescriptor(cfs[i]);
  123. desc.setMaxVersions(3650);
  124. tabledesc.addfamily(desc);
  125. }

  126. byte[] [[] [] splitkeys = new byte [] [] {
  127. bytes.tobytes( "zkb_0_0")、
  128. bytes .toBytes("zkb_10_0")、
  129. Bytes.toBytes("zkb_20_0")、
  130. Bytes.toBytes("zkb_30_0")、
  131. Bytes.toBytes("zkb_40_0")、
  132. Bytes .toBytes ("zkb_50_0")、
  133. Bytes.toBytes("zkb_60_0")、
  134. Bytes.toBytes("zkb_70_0")、
  135. Bytes.toBytes("zkb_80_0")、
  136. Bytes.toBytes(" zkb_90_0 ")、
  137. bytes.tobytes(" zkb_100_0 ")
  138. }; "create table " + tablename + " ok.");
  139. }
  140. } catch (MasterNotRunningException e) {
  141. e.printStackTrace();
  142. } catch (ZooKeeperConnectionException e) {
  143. e .printStackTrace();
  144. } catch (IOException e) {
  145. e.printStackTrace();
  146. }
  147. }

  148. /**
  149. * テーブルを削除
  150. * @param tablename
  151. */
  152. 公共のstatic void deleteTable(String tablename){
  153. try {
  154. //接続 conn = ConnectionFactory.createConnection();
  155. //Admin admin = conn.getAdmin();
  156. admin.disableTable(TableName.valueOf(テーブル名));
  157. admin.deleteTable(TableName.valueOf(テーブル名));
  158. System.out.println("テーブルの削除 " + テーブル名 + " OK。" );
  159. } catch(ioexception e){
  160. e.printstacktrace(); (String tableName, String rowKey){
  161. try {
  162. Table table = conn.getTable(TableName.valueOf(tableName));

  163. List<削除> list = new ArrayList();
  164. Delete del = new Delete(rowKey.getBytes());
  165. list.add(del);
  166. table.delete(list);
  167. システム.out.println("del recored " + rowKey + " ok.");
  168. } catch (IOException e) {
  169. e.printStackTrace();
  170. }
  171. }

  172. /**
  173. * 一筆資料取得
  174. * @param tableName
  175. * @param rowKey
  176. */
  177. public static void getOneRecord (String tableName, String rowKey){
  178. try {
  179. Table table = conn.getTable(TableName. valueOf(tableName));

  180. Get get = new Get(rowKey.getBytes());
  181. Result rs = table.get(get);
  182. List list = rs.listCells();
  183. for(Cell cell:list){
  184. System.out.print(new String(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength()) + " " );
  185. System.out.print(new String(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()) + ":" );
  186. System.out.print(new String(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()) + " " );
  187. System.out.print(cell.getTimestamp() + " " );
  188. System.out .print(new String(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()) + " " );
  189. System.out.println("");
  190. }
  191. } catch (IOException e) {
  192. e.printStackTrace();
  193. }
  194. }

  195. /**
  196. * すべての情報を入手してください
  197. * @param tableName
  198. */
  199. public static void getAllRecord (String tableName) {
  200. try{
  201. //Connection conn = ConnectionFactory.createConnection();
  202. Table table = conn.getTable(TableName.valueOf(tableName));

  203. Scan scan = new Scan();
  204. ResultScanner resultscanner = table.getScanner(scan);
  205. for(Result rs:resultscanner){
  206. List list = rs.listCells();
  207. for(Cell cell:list){
  208. System.out.print(new String(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength()) + " " );
  209. System.out.print(new String(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()) + ":" );
  210. System.out.print(new String(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()) + " " );
  211. System.out.print(cell.getTimestamp() + " " );
  212. System.out .print(new String(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()) + " " );
  213. System.out.println("");
  214. }
  215. }
  216. } catch (IOException e){
  217. e.printStackTrace();
  218. }
  219. }

  220. /**
  221. * ファミリーリストを取得
  222. * @param tableName
  223. * @return
  224. */
  225. public static<文字列> getAllFamilyName(String tableName) {
  226. ArrayList familyname_list = new ArrayList();
  227. try{
  228. //接続 conn = ConnectionFactory.createConnection();
  229. テーブル table = conn.getTable(TableName.valueOf(tableName));

  230. HTableDescriptor htabledescriptor = table.getTableDescriptor();
  231. HColumnDescriptor[] hdlist = htabledescriptor.getColumnFamilies();
  232. for(int i=0;i
  233. HColumnDescriptor hd = hdlist[i];
  234. familyname_list.add(hd.getNameAsString());
  235. }
  236. } catch (IOException e){
  237. e.printStackTrace();
  238. }
  239. return familyname_list;
  240. }

  241. // java -cp HelloWorld.jar:`ls lib/*.jar|awk '{printf("%s:", $0)}'elementary.HelloWorld 5
  242. public static void main(String[] args) {
  243. System.out.println("HelloWorldX");
  244. if (args.length > 0)
  245. System.out.println(args [0]);

  246. int start = 0;
  247. if (args.length > 1)
  248. start = Integer.valueOf(args[1]);
  249. if (start < ; 0)
  250. start = 0;

  251. int num_threads = 16;
  252. if (args.length > 2)
  253. num_threads = Integer.valueOf(args[2]);

  254. try {
  255. String tablename = "スコア";
  256. String[] ファミリー = {"学年", "コース"};
  257. HelloWorld.createTable(tablename, familys);

  258. //ExecutorService thread_pool = Executors.newSingleThreadExecutor();
  259. ExecutorService thread_pool = Executors.newFixedThreadPool(num_threads)
  260. スレッド[]プール = new HelloWorld.MyThread[80];

  261. for (int i=0; i
    pool[i] = new HelloWorld.MyThread(i, tablename);

  262. thread_pool.execute(pool[i]);

  263. }


  264. thread_pool.shutdown();

  265. System.out.println("over");

  266. }

  267. catch (例外) e) {

  268. e.printStackTrace();

  269. }

  270. }

  271. }

www.bkjia.com本当http://www.bkjia.com/PHPjc/1109310.html技術記事 HBase Java プログラムの例 HelloWorld.zip パッケージの基本。インポート java.io.IOException;インポート java.text.SimpleDateFormat; java.util.ArrayListをインポートします。 java.util.Dateをインポートします。インポート j...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。