ラベル HBase の投稿を表示しています。 すべての投稿を表示
ラベル HBase の投稿を表示しています。 すべての投稿を表示

2012年9月3日月曜日

HBaseでリージョンを分割してTRUNCATE

HBaseのテーブル操作について。 リージョンの分割を指定して、テーブルをTRUNCATEしたい」
分割キーを指定した場合は、その通りに。しなかった場合は、消す前のテーブルのリージョン(枠)を再現する。
  • ソースコード
  • # java truncateWithRegions
    Usage : java truncateWithRegions  [<filename>]
    

    truncateWithRegions.java
    import java.io.*;
    import java.util.*;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.client.HBaseAdmin;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.util.Bytes;
    
    public class truncateWithRegions {
    
      public static void main(String[] args) throws Exception {
    
        if (args.length < 1 || args.length > 2) {
          printUsage();
          System.exit(1);
        }
    
        Configuration conf = HBaseConfiguration.create();
        HBaseAdmin hAdmin = new HBaseAdmin(conf);
    
        HTableDescriptor desc;
        byte[][] splits = null;
        if (args.length == 2) splits = readSplitKeys(args[1]);
        if (hAdmin.tableExists(args[0])) {
          desc = hAdmin.getTableDescriptor(Bytes.toBytes(args[0]));
          if (args.length == 1) {
            splits = getSplitKeys(args[0]);
          }
          hAdmin.disableTable(args[0]);
          hAdmin.deleteTable(args[0]);
        } else {
          desc = new HTableDescriptor(args[0]);
        }
        createTable(hAdmin, desc, splits);
      }
    
      private static byte[][] getSplitKeys(String tableName) throws Exception {
        HTable hTable = new HTable(tableName);
        byte[][] splits = new byte[hTable.getEndKeys().length - 1][];
        for ( int i = 0; i < hTable.getEndKeys().length - 1; i++) {
          splits[i] = hTable.getEndKeys()[i];
        }
        return splits;
      }
    
      private static byte[][] readSplitKeys(String filename) throws Exception {
    
        BufferedReader br = new BufferedReader(new FileReader(filename));
        List<byte[]> splitList = new ArrayList<byte[]>();
        String line = "";
        while ((line = br.readLine()) != null) {
          if (line.length() != 0) {
            byte[] split = Bytes.toBytes(line);
            splitList.add(split);
          }
        }
        br.close();
        return splitList.toArray(new byte[0][]);
      }
    
      private static void createTable(HBaseAdmin hAdmin, HTableDescriptor desc, byte[][] splits) throws Exception {
        if (splits == null) {
          hAdmin.createTable(desc);
        } else {
          hAdmin.createTable(desc, splits);
        }
      }
    
      private static void printUsage() {
        System.err.println("Usage : java truncateWithRegions <tablename> [<filename>]");
      }
    

おわり

2012年6月8日金曜日

HBaseとThriftとPython

CentOS上でThrift使ってPythonでHBaseアクセス。
  • HadoopディストリビューションはCDH3u3
  1. thriftインストール
  2. http://thrift.apache.org/docs/install/centos/
  3. hbase-thriftインストール
  4. yum使います。
    # yum install hadoop-hbase-thrift
  5. CDHのパッケージにはThriftの定義体がないので以下からソースダウンロード
  6. http://www.apache.org/dyn/closer.cgi/hbase/
  7. Hbase.thrift(Thriftの定義体)を作業ディレクトリにコピー
  8. # cp {hadoopのソースコード展開ディレクトリ}/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift {作業ディレクトリ}

    ついでに作業ディレクトリに移動。
    # cd {作業ディレクトリ}
  9. thriftによるpythonモジュールの生成
  10. # thrift --gen py Hbase.thrift
  11. クライアントのコードを書く
  12. # cd gen-py
    # vi hbaseClient.py

    hbaseClient.py
    import sys
    sys.path.append('./gen-py')
    
    from thrift.transport.TSocket import TSocket
    from thrift.transport.TTransport import TBufferedTransport
    from thrift.protocol import TBinaryProtocol
    from hbase import Hbase
    
    transport=TBufferedTransport(TSocket('localhost', 9090))
    transport.open()
    protocol=TBinaryProtocol.TBinaryProtocol(transport)
    client=Hbase.Client(protocol)
    
    print(client.getTableNames())
    
  13. 実行
  14. #service hadoop-hbase-thrift start
    # python hbaseClient.py
    ['usertable']
    

おわり