封装API

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.Result;
import org.apache.hadoop.hbase.client.Table;

public class App {

    public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();

        TableName tname = TableName.valueOf("test");
        conn.getTable(tname);

        //create table
        //admin.createTable(null);

        //disable
        //admin.disableTable(null);

        //drop
        //admin.deleteTable(null);

        Table table = conn.getTable(tname);

        Get get = new Get("row1".getBytes());

        Result result = table.get(get);

        System.out.println(result.toString());

        //table.put(null);

        //table.delete(null);

        //ResultScanner rs = table.getScanner(null);

        //for(Result r:rs){

        //}
    }

}

大数据量插入

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class TestZk {

    private static Configuration conf = null;
    private static Connection conn = null;

    static{
        try {
            conf = HBaseConfiguration.create();
            conn = ConnectionFactory.createConnection(conf);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        createTable("test");
        batchPut("test", 100);
    }

    public static void createTable(String name) throws Exception{
        Admin admin = conn.getAdmin();
        TableName tableName = TableName.valueOf(name);

        if(admin.tableExists(tableName)){
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }

        HTableDescriptor hdt = new HTableDescriptor(tableName);
        HColumnDescriptor hcd = new HColumnDescriptor("data");
        hdt.addFamily(hcd);
        admin.createTable(hdt);
    }

    public static void batchPut(String tableName,int n)throws Exception{
        TableName tm = TableName.valueOf(tableName);
        Table table = conn.getTable(tm);
        Put put = null;
        for(int i=1;i<n;i++){
            put = new Put(b(i+""));
            put.addColumn(b("data"),b(i+""),b("tom"+i));
            table.put(put);
        }
        table.close();
    } 

    public static byte[] b(String str){
        return Bytes.toBytes(str);
    }

}

显示表的描述信息

  • describe 'test'