zoukankan      html  css  js  c++  java
  • Hbase Java API 测试代码

    package com.rabbit.hadoop.testEnv;

    import java.io.IOException;
    import java.util.List;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.CellUtil;
    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.NamespaceDescriptor;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.TableNotFoundException;
    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.HBaseAdmin;
    import org.apache.hadoop.hbase.client.HConnection;
    import org.apache.hadoop.hbase.client.HConnectionManager;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.HTableInterface;
    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;

    public class TestHbaseApi {

    /**
    * Get configuration
    * @return
    */
    public static Configuration getconf() {
    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum",
    "bd27-server.ibeifeng.com,bd27-server2.ibeifeng.com,bd27-server3.ibeifeng.com");
    return configuration;
    }

    /**
    * Get hbase connection
    * @param conf
    * @return
    * @throws IOException
    */
    public static HConnection getConn(Configuration conf) throws IOException {
    ExecutorService pool = Executors.newFixedThreadPool(10);
    HConnection conn = HConnectionManager.createConnection(conf, pool);
    return conn;
    }

    /**
    * Get an table instance of a given table name, using hbase connection pool
    * @param tableName
    * @return
    * @throws IOException
    */
    public static HTableInterface getHTable(String tableName) throws IOException {
    HConnection connection = getConn(getconf());
    HTableInterface table = connection.getTable(tableName);
    return table;
    }

    /**
    * DDL: create table
    * @param admin
    * @param tName
    * @param families
    * @throws IOException
    */
    public static void createTable(HBaseAdmin admin, TableName tName, List<HColumnDescriptor> families)
    throws IOException {

    if (admin.tableExists(tName)) {
    try {
    throw new Exception("Table already exists.");
    } catch (Exception e) {
    e.printStackTrace();
    System.out.println("faild to create table " + """ + tName.toString() + "".");
    }
    } else {
    HTableDescriptor tableDescriptor = new HTableDescriptor(tName);

    for (HColumnDescriptor family : families) {
    tableDescriptor.addFamily(family);
    }

    tableDescriptor.setMaxFileSize(10000L);
    admin.createTable(tableDescriptor);
    System.out.println(tName.toString() + "表创建成功");
    }

    }

    /**
    * Get descriptions of a table
    * @param admin
    * @param tableName
    * @throws TableNotFoundException
    * @throws IOException
    */
    public static void getTableDescription(HBaseAdmin admin, String tableName)
    throws TableNotFoundException, IOException {
    if (admin.tableExists(tableName)) {
    TableName tName = TableName.valueOf(tableName);
    HTableDescriptor descriptor = admin.getTableDescriptor(tName);
    System.out.println(descriptor);
    } else {
    System.out.println("table descriptor: table does not exits.");
    }

    }

    /**
    * DDL: delete a table with parameter type : TableName
    * @param admin
    * @param tName
    * @throws IOException
    */
    public static void deleteTable(HBaseAdmin admin, TableName tName) throws IOException {
    if (admin.tableExists(tName)) {
    if (!admin.isTableDisabled(tName)) {
    admin.disableTable(tName);
    }
    admin.deleteTable(tName);
    System.out.println("deleted table successfully.");
    } else {
    System.out.println("table deleting: table does not exits.");
    }

    }

    /**
    * DDL: overwrite : delete a table with parameter table name type : String
    * @param admin
    * @param tableName
    * @throws IOException
    */
    public static void deleteTable(HBaseAdmin admin, String tableName) throws IOException {
    TableName tName = TableName.valueOf(tableName);
    deleteTable(admin, tName);
    }

    /**
    * Insert records into a table
    * @param conf
    * @param tableName
    * @param rowKey
    * @param familyName
    * @param column
    * @param value
    * @throws IOException
    */
    public static void putData(Configuration conf, String tableName, String rowKey, String familyName, String column,
    String value) throws IOException {
    HTable table = new HTable(conf, tableName);
    try {
    Put put = new Put(Bytes.toBytes(rowKey));
    put.add(familyName.getBytes(), column.getBytes(), value.getBytes());
    table.put(put);
    } finally {
    table.close();
    }

    }

    /**
    * Query records from a table
    * @param conf
    * @param tableName
    * @param rowKey
    * @throws IOException
    */
    public static void getData(Configuration conf, String tableName, String rowKey) throws IOException {
    HTable table = new HTable(conf, tableName);
    Get get = new Get(rowKey.getBytes());
    Result result = table.get(get);
    for (Cell cell : result.rawCells()) {
    System.out.println("family: " + Bytes.toString(CellUtil.cloneFamily(cell)) + " --> " + "column: "
    + Bytes.toString(CellUtil.cloneQualifier(cell)) + " --> " + "value: "
    + Bytes.toString(CellUtil.cloneValue(cell)));
    }

    table.close();
    }

    /**
    * Delete records from a table, using rowkey
    * @param conf
    * @param tableName
    * @param rowKey
    * @throws IOException
    */
    public static void deleteData(Configuration conf, String tableName, String rowKey) throws IOException {
    deleteData(conf, tableName, rowKey, null);
    }

    /**
    * Delete records from a table, using rowkey and family
    * @param conf
    * @param tableName
    * @param rowKey
    * @param family
    * @throws IOException
    */
    public static void deleteData(Configuration conf, String tableName, String rowKey, String family)
    throws IOException {
    deleteData(conf, tableName, rowKey, family, null);
    }

    /**
    * Delete records from a table, using rowkey,family and column name
    * @param conf
    * @param tableName
    * @param rowKey
    * @param family
    * @param column
    * @throws IOException
    */
    public static void deleteData(Configuration conf, String tableName, String rowKey, String family, String column)
    throws IOException {
    HTable table = new HTable(conf, tableName);
    Delete delete = new Delete(rowKey.getBytes());
    if (family != null && column != null) {
    delete.deleteColumn(family.getBytes(), column.getBytes());
    } else if (family != null) {
    delete.deleteFamily(family.getBytes());
    }
    table.delete(delete);
    System.out.println("delete records successfully.");
    table.close();
    }

    /**
    * Scan table for records match given startrow and stoprow
    * @param tableName
    * @param startRow
    * @param stopRow
    * @throws IOException
    */
    public static void scanData(String tableName, String startRow, String stopRow)
    throws IOException {
    HTable table = (HTable)getHTable(tableName);
    Scan scan = new Scan();
    if (startRow != null) {
    scan.setStartRow(startRow.getBytes());
    }

    if (stopRow != null) {
    scan.setStopRow(stopRow.getBytes());
    }

    ResultScanner resultScanner = table.getScanner(scan);

    for (Result result : resultScanner) {
    System.out
    .println("::::::::::::::::::::::::" + Bytes.toString(result.getRow()) + "::::::::::::::::::::::::");
    for (Cell cell : result.rawCells()) {
    System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)) + "-->"
    + Bytes.toString(CellUtil.cloneQualifier(cell)) + "-->"
    + Bytes.toString(CellUtil.cloneValue(cell)));
    }
    }

    table.close();
    }

    /**
    * Scan a table for all records
    * @param tableName
    * @throws IOException
    */
    public static void scanData(String tableName) throws IOException {
    scanData(tableName, null, null);
    }

    /**
    * main method
    * @param args
    * @throws MasterNotRunningException
    * @throws ZooKeeperConnectionException
    * @throws IOException
    */
    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {

    Configuration configuration = getconf();

    HBaseAdmin admin = new HBaseAdmin(configuration);// --------> DDL & DML operations

    // admin.createNamespace(NamespaceDescriptor.create("ns2").build());

    String tableName = "users2";
    // TableName tName = TableName.valueOf(tableName);
    // List<HColumnDescriptor> familyList = new ArrayList<HColumnDescriptor>();
    //
    // HColumnDescriptor f1 = new HColumnDescriptor("f1");
    // HColumnDescriptor f2 = new HColumnDescriptor("f2");
    // HColumnDescriptor f3 = new HColumnDescriptor("f3");
    //
    // familyList.add(f1);
    // familyList.add(f2);
    // familyList.add(f3);

    try {
    // createTable(admin, tName,familyList);
    // getTableDescription(admin, tableName);
    // deleteTable(admin, tableName);
    putData(configuration, tableName, "10001", "f1", "name", "user01");
    putData(configuration, tableName, "10001", "f1", "age", "20");
    putData(configuration, tableName, "10001", "f1", "gender", "male");
    putData(configuration, tableName, "10002", "f2", "address", "shanghai");
    putData(configuration, tableName, "10002", "f2", "qq", "10086");
    putData(configuration, tableName, "10002", "f1", "name", "user02");
    putData(configuration, tableName, "10002", "f1", "age", "22");
    putData(configuration, tableName, "10003", "f1", "name", "user03");
    putData(configuration, tableName, "10003", "f1", "age", "23");
    putData(configuration, tableName, "10003", "f1", "gender", "female");
    putData(configuration, tableName, "10003", "f2", "address", "beijing");
    putData(configuration, tableName, "10003", "f2", "qq", "95533");
    putData(configuration, tableName, "10005", "f1", "name", "user05");
    putData(configuration, tableName, "10004", "f1", "name", "user04");
    putData(configuration, tableName, "10004", "f1", "age", "24");
    putData(configuration, tableName, "10004", "f1", "gender", "female");
    //
    // getData(configuration, tableName, "10003");
    // System.out.println("***************************************");
    // getData(configuration, tableName, "10004");
    // System.out.println("***************************************");
    // deleteData(configuration, tableName, "10003", "f1");
    // deleteData(configuration, tableName, "10004", "f1", "age");
    scanData(tableName);
    System.out.println("==============================================================");
    scanData(tableName, "10002", "10004");
    } finally {
    if (admin != null) {
    admin.close();
    }

    }
    }
    }

  • 相关阅读:
    day23,xml 和面向对象
    day22,ConfigParser,subprocess,xlrd三个模块
    re的总结
    day20,日志和正则表达式
    day19,序列化与反序列化总结,和其它的有些模块
    字符串的内建函数
    字符串和编码
    python解释器分类
    git &github 快速入门
    do export method of oracle all database tables with dmp files.
  • 原文地址:https://www.cnblogs.com/rabbit624/p/10556622.html
Copyright © 2011-2022 走看看