HBase Java プログラムの例
HelloWorld.zip
- package Elementary;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- 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"); = 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) 、文字列テーブル名) {
- _start = 開始;
- _tablename = テーブル名;
- }
- public void run() {
- String tablename = _tablename;
- スレッドcurrent = Thread.currentThread();
- long thread_id = current.getId();
- System.out.printf("thread[%d] runn", 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("グレード"),Bytes.toBytes("フィールド1"),Bytes.toBytes(String.valueOf(i+0)));
- put.addColumn(Bytes.toBytes("グレード"),Bytes.toBytes("フィールド2"),Bytes.toBytes(String.valueOf(i+1)));
- put.addColumn(Bytes.toBytes ("グレード"),Bytes.toBytes("フィールド3"),Bytes.toBytes(String.valueOf(i+2)));
- put.addColumn(Bytes.toBytes("グレード"),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( "グレード"),Bytes.toBytes("フィールド7"),Bytes.toBytes(String.valueOf(i+6)));
- put.addColumn(Bytes.toBytes("グレード"),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("グレード"),Bytes.toBytes("フィールド10"),Bytes.toBytes(String.valueOf(i+9)));
- put.addColumn(Bytes.toBytes("grade"),Bytes.toBytes("field11"),Bytes.toBytes(String.valueOf(i+10)));
- put.addColumn(Bytes.toBytes( "グレード"),Bytes.toBytes("フィールド12"),Bytes.toBytes(String.valueOf(i+11)));
- put.addColumn(Bytes.toBytes("グレード"),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("グレード"),Bytes.toBytes("フィールド15"),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=%dn", now, thread_id, m, j, i);
- }
- }
- }
- System.out.printf("thread[%d] overn", thread_id);
- }
- catch (例外 e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * テーブルを作成します
- * @param tablename
- * @param cfs
- */
- public静的 void createTable (String tablename, String[] cfs){
- try {
- if (admin.tableExists(TableName.valueOf(tablename))) {
- System.out.println("テーブルはすでに存在します!");
- } 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 ")
- }; "create table " + tablename + " ok.");
- }
- } catch (MasterNotRunningException e) {
- e.printStackTrace();
- } catch (ZooKeeperConnectionException e) {
- e .printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * テーブルを削除
- * @param tablename
- */
- 公共のstatic void deleteTable(String tablename){
- try {
- //接続 conn = ConnectionFactory.createConnection();
- //Admin admin = conn.getAdmin();
- admin.disableTable(TableName.valueOf(テーブル名));
- admin.deleteTable(TableName.valueOf(テーブル名));
- System.out.println("テーブルの削除 " + テーブル名 + " OK。" );
- } catch(ioexception e){
- e.printstacktrace(); (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);
- システム.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();
- }
- }
- /**
- * ファミリーリストを取得
- * @param tableName
- * @return
- */
- public static<文字列> getAllFamilyName(String tableName) {
- ArrayList familyname_list = new ArrayList();
- try{
- //接続 conn = ConnectionFactory.createConnection();
- テーブル 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 < ; 0)
- start = 0;
- int num_threads = 16;
- if (args.length > 2)
- num_threads = Integer.valueOf(args[2]);
- try {
- String tablename = "スコア";
- String[] ファミリー = {"学年", "コース"};
- HelloWorld.createTable(tablename, familys);
- //ExecutorService thread_pool = Executors.newSingleThreadExecutor();
- ExecutorService thread_pool = Executors.newFixedThreadPool(num_threads)
スレッド[]プール = 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 (例外) e) {
e.printStackTrace();
}
}
-
- }
http://www.bkjia.com/PHPjc/1109310.htmlwww.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...