Hbase API Common Demo

1. HBaseHelper

package util;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseHelper implements Closeable {

  private Configuration configuration = null;
  private Connection connection = null;
  private Admin admin = null;

  protected HBaseHelper(Configuration configuration) throws IOException {
    this.configuration = configuration;
    this.connection = ConnectionFactory.createConnection(configuration);
    this.admin = connection.getAdmin();
  }

  public static HBaseHelper getHelper(Configuration configuration) throws IOException {
    return new HBaseHelper(configuration);
  }

  @Override
  public void close() throws IOException {
    connection.close();
  }

  public Connection getConnection() {
    return connection;
  }

  public Configuration getConfiguration() {
    return configuration;
  }

  public void createNamespace(String namespace) {
    try {
      NamespaceDescriptor nd = NamespaceDescriptor.create(namespace).build();
      admin.createNamespace(nd);
    } catch (Exception e) {
      System.err.println("Error: " + e.getMessage());
    }
  }

  public void dropNamespace(String namespace, boolean force) {
    try {
      if (force) {
        TableName[] tableNames = admin.listTableNamesByNamespace(namespace);
        for (TableName name : tableNames) {
          admin.disableTable(name);
          admin.deleteTable(name);
        }
      }
    } catch (Exception e) {
      // ignore
    }
    try {
      admin.deleteNamespace(namespace);
    } catch (IOException e) {
      System.err.println("Error: " + e.getMessage());
    }
  }

  public boolean existsTable(String table)
  throws IOException {
    return existsTable(TableName.valueOf(table));
  }

  public boolean existsTable(TableName table)
  throws IOException {
    return admin.tableExists(table);
  }

  public void createTable(String table, String... colfams)
  throws IOException {
    createTable(TableName.valueOf(table), 1, null, colfams);
  }

  public void createTable(TableName table, String... colfams)
  throws IOException {
    createTable(table, 1, null, colfams);
  }

  public void createTable(String table, int maxVersions, String... colfams)
  throws IOException {
    createTable(TableName.valueOf(table), maxVersions, null, colfams);
  }

  public void createTable(TableName table, int maxVersions, String... colfams)
  throws IOException {
    createTable(table, maxVersions, null, colfams);
  }

  public void createTable(String table, byte[][] splitKeys, String... colfams)
  throws IOException {
    createTable(TableName.valueOf(table), 1, splitKeys, colfams);
  }

  public void createTable(TableName table, int maxVersions, byte[][] splitKeys,
    String... colfams)
  throws IOException {
    HTableDescriptor desc = new HTableDescriptor(table);
    for (String cf : colfams) {
      HColumnDescriptor coldef = new HColumnDescriptor(cf);
      coldef.setMaxVersions(maxVersions);
      desc.addFamily(coldef);
    }
    if (splitKeys != null) {
      admin.createTable(desc, splitKeys);
    } else {
      admin.createTable(desc);
    }
  }

  public void disableTable(String table) throws IOException {
    disableTable(TableName.valueOf(table));
  }

  public void disableTable(TableName table) throws IOException {
    admin.disableTable(table);
  }

  public void dropTable(String table) throws IOException {
    dropTable(TableName.valueOf(table));
  }

  public void dropTable(TableName table) throws IOException {
    if (existsTable(table)) {
      if (admin.isTableEnabled(table)) disableTable(table);
      admin.deleteTable(table);
    }
  }

  public void fillTable(String table, int startRow, int endRow, int numCols,
                        String... colfams)
  throws IOException {
    fillTable(TableName.valueOf(table), startRow,endRow, numCols, colfams);
  }

  public void fillTable(TableName table, int startRow, int endRow, int numCols,
                        String... colfams)
  throws IOException {
    fillTable(table, startRow, endRow, numCols, -1, false, colfams);
  }

  public void fillTable(String table, int startRow, int endRow, int numCols,
                        boolean setTimestamp, String... colfams)
  throws IOException {
    fillTable(TableName.valueOf(table), startRow, endRow, numCols, -1,
      setTimestamp, colfams);
  }

  public void fillTable(TableName table, int startRow, int endRow, int numCols,
                        boolean setTimestamp, String... colfams)
  throws IOException {
    fillTable(table, startRow, endRow, numCols, -1, setTimestamp, colfams);
  }

  public void fillTable(String table, int startRow, int endRow, int numCols,
                        int pad, boolean setTimestamp, String... colfams)
  throws IOException {
    fillTable(TableName.valueOf(table), startRow, endRow, numCols, pad,
      setTimestamp, false, colfams);
  }

  public void fillTable(TableName table, int startRow, int endRow, int numCols,
                        int pad, boolean setTimestamp, String... colfams)
  throws IOException {
    fillTable(table, startRow, endRow, numCols, pad, setTimestamp, false,
      colfams);
  }

  public void fillTable(String table, int startRow, int endRow, int numCols,
                        int pad, boolean setTimestamp, boolean random,
                        String... colfams)
  throws IOException {
    fillTable(TableName.valueOf(table), startRow, endRow, numCols, pad,
      setTimestamp, random, colfams);
  }

  public void fillTable(TableName table, int startRow, int endRow, int numCols,
                        int pad, boolean setTimestamp, boolean random,
                        String... colfams)
  throws IOException {
    Table tbl = connection.getTable(table);
    Random rnd = new Random();
    for (int row = startRow; row <= endRow; row++) {
      for (int col = 1; col <= numCols; col++) {
        Put put = new Put(Bytes.toBytes("row-" + padNum(row, pad)));
        for (String cf : colfams) {
          String colName = "col-" + padNum(col, pad);
          String val = "val-" + (random ?
            Integer.toString(rnd.nextInt(numCols)) :
            padNum(row, pad) + "." + padNum(col, pad));
          if (setTimestamp) {
            put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(colName), col,
              Bytes.toBytes(val));
          } else {
            put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(colName),
              Bytes.toBytes(val));
          }
        }
        tbl.put(put);
      }
    }
    tbl.close();
  }

  public void fillTableRandom(String table,
      int minRow, int maxRow, int padRow,
      int minCol, int maxCol, int padCol,
      int minVal, int maxVal, int padVal,
      boolean setTimestamp, String... colfams)
    throws IOException {
    fillTableRandom(TableName.valueOf(table), minRow, maxRow, padRow, minCol,
      maxCol, padCol, minVal, maxVal, padVal, setTimestamp, colfams);
  }

  public void fillTableRandom(TableName table,
    int minRow, int maxRow, int padRow,
    int minCol, int maxCol, int padCol,
    int minVal, int maxVal, int padVal,
    boolean setTimestamp, String... colfams)
  throws IOException {
    Table tbl = connection.getTable(table);
    Random rnd = new Random();
    int maxRows = minRow + rnd.nextInt(maxRow - minRow);
    for (int row = 0; row < maxRows; row++) {
      int maxCols = minCol + rnd.nextInt(maxCol - minCol);
      for (int col = 0; col < maxCols; col++) {
        int rowNum = rnd.nextInt(maxRow - minRow + 1);
        Put put = new Put(Bytes.toBytes("row-" + padNum(rowNum, padRow)));
        for (String cf : colfams) {
          int colNum = rnd.nextInt(maxCol - minCol + 1);
          String colName = "col-" + padNum(colNum, padCol);
          int valNum = rnd.nextInt(maxVal - minVal + 1);
          String val = "val-" +  padNum(valNum, padCol);
          if (setTimestamp) {
            put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(colName), col,
              Bytes.toBytes(val));
          } else {
            put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(colName),
              Bytes.toBytes(val));
          }
        }
        tbl.put(put);
      }
    }
    tbl.close();
  }

  public String padNum(int num, int pad) {
    String res = Integer.toString(num);
    if (pad > 0) {
      while (res.length() < pad) {
        res = "0" + res;
      }
    }
    return res;
  }

  public void put(String table, String row, String fam, String qual,
                  String val) throws IOException {
    put(TableName.valueOf(table), row, fam, qual, val);
  }

  public void put(TableName table, String row, String fam, String qual,
                  String val) throws IOException {
    Table tbl = connection.getTable(table);
    Put put = new Put(Bytes.toBytes(row));
    put.addColumn(Bytes.toBytes(fam), Bytes.toBytes(qual), Bytes.toBytes(val));
    tbl.put(put);
    tbl.close();
  }

  public void put(String table, String row, String fam, String qual, long ts,
                  String val) throws IOException {
    put(TableName.valueOf(table), row, fam, qual, ts, val);
  }

  public void put(TableName table, String row, String fam, String qual, long ts,
                  String val) throws IOException {
    Table tbl = connection.getTable(table);
    Put put = new Put(Bytes.toBytes(row));
    put.addColumn(Bytes.toBytes(fam), Bytes.toBytes(qual), ts,
      Bytes.toBytes(val));
    tbl.put(put);
    tbl.close();
  }

  public void put(String table, String[] rows, String[] fams, String[] quals,
                  long[] ts, String[] vals) throws IOException {
    put(TableName.valueOf(table), rows, fams, quals, ts, vals);
  }

  public void put(TableName table, String[] rows, String[] fams, String[] quals,
                  long[] ts, String[] vals) throws IOException {
    Table tbl = connection.getTable(table);
    for (String row : rows) {
      Put put = new Put(Bytes.toBytes(row));
      for (String fam : fams) {
        int v = 0;
        for (String qual : quals) {
          String val = vals[v < vals.length ? v : vals.length - 1];
          long t = ts[v < ts.length ? v : ts.length - 1];
          System.out.println("Adding: " + row + " " + fam + " " + qual +
            " " + t + " " + val);
          put.addColumn(Bytes.toBytes(fam), Bytes.toBytes(qual), t,
            Bytes.toBytes(val));
          v++;
        }
      }
      tbl.put(put);
    }
    tbl.close();
  }

  public void dump(String table, String[] rows, String[] fams, String[] quals)
  throws IOException {
    dump(TableName.valueOf(table), rows, fams, quals);
  }

  public void dump(TableName table, String[] rows, String[] fams, String[] quals)
  throws IOException {
    Table tbl = connection.getTable(table);
    List<Get> gets = new ArrayList<Get>();
    for (String row : rows) {
      Get get = new Get(Bytes.toBytes(row));
      get.setMaxVersions();
      if (fams != null) {
        for (String fam : fams) {
          for (String qual : quals) {
            get.addColumn(Bytes.toBytes(fam), Bytes.toBytes(qual));
          }
        }
      }
      gets.add(get);
    }
    Result[] results = tbl.get(gets);
    for (Result result : results) {
      for (Cell cell : result.rawCells()) {
        System.out.println("Cell: " + cell +
          ", Value: " + Bytes.toString(cell.getValueArray(),
          cell.getValueOffset(), cell.getValueLength()));
      }
    }
    tbl.close();
  }

  public void dump(String table) throws IOException {
    dump(TableName.valueOf(table));
  }

  public void dump(TableName table) throws IOException {
    try (
      Table t = connection.getTable(table);
      ResultScanner scanner = t.getScanner(new Scan())
    ) {
      for (Result result : scanner) {
        dumpResult(result);
      }
    }
  }

  public void dumpResult(Result result) {
    for (Cell cell : result.rawCells()) {
      System.out.println("Cell: " + cell +
        ", Value: " + Bytes.toString(cell.getValueArray(),
        cell.getValueOffset(), cell.getValueLength()));
    }
  }
}

2.AppendExample

package client;

// cc AppendExample Example application appending data to a column in  HBase

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Append;
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.util.Bytes;
import util.HBaseHelper;

import java.io.IOException;

public class AppendExample {

  public static void main(String[] args) throws IOException {
    Configuration conf = HBaseConfiguration.create();

    HBaseHelper helper = HBaseHelper.getHelper(conf);
    helper.dropTable("testtable");
    helper.createTable("testtable", 100, "colfam1", "colfam2");
    helper.put("testtable",
      new String[] { "row1" },
      new String[] { "colfam1" },
      new String[] { "qual1" },
      new long[]   { 1 },
      new String[] { "oldvalue" });
    System.out.println("Before append call...");
    helper.dump("testtable", new String[]{ "row1" }, null, null);

    Connection connection = ConnectionFactory.createConnection(conf);
    Table table = connection.getTable(TableName.valueOf("testtable"));

    // vv AppendExample
    Append append = new Append(Bytes.toBytes("row1"));
    append.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
      Bytes.toBytes("newvalue"));
    append.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
      Bytes.toBytes("anothervalue"));

    table.append(append);
    // ^^ AppendExample
    System.out.println("After append call...");
    helper.dump("testtable", new String[]{"row1"}, null, null);
    table.close();
    connection.close();
    helper.close();
  }
}

3.IncrementMultipleExample

package client;

// cc IncrementMultipleExample Example incrementing multiple counters in one row
import java.io.IOException;
import java.util.Map;
import java.util.NavigableMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import util.HBaseHelper;

public class IncrementMultipleExample {

  public static void main(String[] args) throws IOException {
    Configuration conf = HBaseConfiguration.create();

    HBaseHelper helper = HBaseHelper.getHelper(conf);
    helper.dropTable("testtable");
    helper.createTable("testtable", "daily", "weekly");

    Connection connection = ConnectionFactory.createConnection(conf);
    Table table = connection.getTable(TableName.valueOf("testtable"));
    // vv IncrementMultipleExample
    Increment increment1 = new Increment(Bytes.toBytes("20150101"));

    increment1.addColumn(Bytes.toBytes("daily"), Bytes.toBytes("clicks"), 1);
    increment1.addColumn(Bytes.toBytes("daily"), Bytes.toBytes("hits"), 1); // co IncrementMultipleExample-1-Incr1 Increment the counters with various values.
    increment1.addColumn(Bytes.toBytes("weekly"), Bytes.toBytes("clicks"), 10);
    increment1.addColumn(Bytes.toBytes("weekly"), Bytes.toBytes("hits"), 10);
    // ^^ IncrementMultipleExample
    Map<byte[], NavigableMap<byte[], Long>> longs =
      increment1.getFamilyMapOfLongs();
    for (byte[] family : longs.keySet()) {
      System.out.println("Increment #1 - family: " + Bytes.toString(family));
      NavigableMap<byte[], Long> longcols = longs.get(family);
      for (byte[] column : longcols.keySet()) {
        System.out.print("  column: " + Bytes.toString(column));
        System.out.println(" - value: " + longcols.get(column));
      }
    }
    // vv IncrementMultipleExample

    Result result1 = table.increment(increment1); // co IncrementMultipleExample-2-Incr2 Call the actual increment method with the above counter updates and receive the results.

    for (Cell cell : result1.rawCells()) {
      System.out.println("Cell: " + cell +
        " Value: " + Bytes.toLong(cell.getValueArray(), cell.getValueOffset(),
        cell.getValueLength())); // co IncrementMultipleExample-3-Dump1 Print the cell and returned counter value.
    }

    Increment increment2 = new Increment(Bytes.toBytes("20150101"));

    increment2.addColumn(Bytes.toBytes("daily"), Bytes.toBytes("clicks"), 5);
    increment2.addColumn(Bytes.toBytes("daily"), Bytes.toBytes("hits"), 1); // co IncrementMultipleExample-4-Incr3 Use positive, negative, and zero increment values to achieve the wanted counter changes.
    increment2.addColumn(Bytes.toBytes("weekly"), Bytes.toBytes("clicks"), 0);
    increment2.addColumn(Bytes.toBytes("weekly"), Bytes.toBytes("hits"), -5);

    Result result2 = table.increment(increment2);

    for (Cell cell : result2.rawCells()) {
      System.out.println("Cell: " + cell +
        " Value: " + Bytes.toLong(cell.getValueArray(),
          cell.getValueOffset(), cell.getValueLength()));
    }
    // ^^ IncrementMultipleExample
    table.close();
    connection.close();
    helper.close();
  }
}

 

转载于:https://my.oschina.net/u/1416978/blog/725454

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值