hbase2.1

 

 

package com.lxkj.hbase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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.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.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
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.TableDescriptor;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;

import com.google.common.collect.Table;

public class HbaseDemo1 {

    // 读取Hbase的配置
    private static Configuration conf = null;
    private static Connection  connection = null;
    static  ExecutorService executor = Executors.newFixedThreadPool(10);
    ///Hbase/src/main/resources/hbase/hbase-site.xml
    static {
        try {
        conf = HBaseConfiguration.create();
        //conf.addResource("/hbase/hbase-site.xml");
        conf.set("hbase.zookeeper.quorum", "192.168.8.35");
        conf.set("hbase.zookeeper.property.clientPort", "2184");
        
            connection=ConnectionFactory.createConnection(conf,executor);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 建表
     * 
     * @param tablename
     * @param columnFamily
     * @throws Exception
     */
    @SuppressWarnings("deprecation")
    public static void createTable(String tablename, String columnFamily) throws Exception {
        Admin admin = connection.getAdmin();
        // 检测表是否存在
        if (admin.tableExists(TableName.valueOf(tablename))) {
            System.out.println("Table exists!");
            System.exit(0);
        } else {
            HTableDescriptor tableDesc = new HTableDescriptor(
                    TableName.valueOf(tablename));
            HColumnDescriptor columnDesc = new HColumnDescriptor(
                    columnFamily);
            tableDesc.addFamily(columnDesc);
            admin.createTable(tableDesc);
            System.out.println("create table success!");
        }
        admin.close();

    }

    /**
     * 查看Hbase中所有的表
     * 
     * @throws Exception
     */
    @SuppressWarnings("deprecation")
    public static TableName[] seeTables() throws Exception {
        Admin admin = connection.getAdmin();
        // 列出HBase中所有的表的列表
        TableName[] tableDescriptor = admin.listTableNames();

        admin.close();
        return tableDescriptor;

    }

    /**
     * 删表
     * 
     * @param tablename
     * @return
     * @throws IOException
     */
    @SuppressWarnings("deprecation")
    public static boolean deleteTable(String tablename) throws IOException {
        Admin admin = connection.getAdmin();
        if (admin.tableExists(TableName.valueOf(tablename))) {
            try {
                // 先禁用再删除
                admin.disableTable(TableName.valueOf(tablename));;
                admin.deleteTable(TableName.valueOf(tablename));
            } catch (Exception e) {
                e.printStackTrace();
                admin.close();
                return false;
            }
        }
        admin.close();
        return true;
    }

    /**
     * 添加数据
     * 
     * @param table
     * @param rowKey
     * @param columnFamily
     * @param identifier
     * @param data
     * @throws Exception
     */
    @SuppressWarnings("deprecation")
    public static void putCell(HTable table, String rowKey, String columnFamily, String identifier, String data)
            throws Exception {
        Put p1 = new Put(Bytes.toBytes(rowKey));
        p1.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(identifier), Bytes.toBytes(data));  
        table.put(p1);
        System.out.println("put '" + rowKey + "', '" + columnFamily + ":" + identifier + "', '" + data + "'");
    }

    /**
     * 获取某一行数据
     * 
     * @param table
     * @param rowKey
     * @return
     * @throws Exception
     */
    public static Result getRow(HTable table, String rowKey) throws Exception {
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        System.out.println("Get: " + result);
        return result;
    }

    /**
     * 删除行
     * 
     * @param table
     * @param rowKey
     * @throws Exception
     */
    public static void deleteRow(HTable table, String rowKey) throws Exception {
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
        System.out.println("Delete row: " + rowKey);
    }

    /**
     * 扫描全表
     * 
     * @param table
     * @return
     * @throws Exception
     */
    public static ResultScanner scanAll(HTable table) throws Exception {
        Scan s = new Scan();
        ResultScanner rs = table.getScanner(s);
        return rs;
    }

    /**
     * 按行范围扫描
     * 
     * @param table
     * @param startrow
     * @param endrow
     * @return
     * @throws Exception
     */
    public static ResultScanner scanRange(HTable table, String startrow, String endrow) throws Exception {
        Scan s = new Scan(Bytes.toBytes(startrow), Bytes.toBytes(endrow));
        ResultScanner rs = table.getScanner(s);
        return rs;
    }

    /**
     * 扫描并过滤,按条件扫描
     * 
     * @param table
     * @param startrow
     * @param filter
     * @return
     * @throws Exception
     */
    public static ResultScanner scanFilter(HTable table, String startrow, Filter filter) throws Exception {
        Scan s = new Scan(Bytes.toBytes(startrow), filter);
        ResultScanner rs = table.getScanner(s);
        return rs;
    }
    
    public static ResultScanner scanAll(HTable table, Filter filter) throws Exception {
        Scan s = new Scan();
        s.setFilter(filter);
        ResultScanner rs = table.getScanner(s);
        return rs;
    }
    /**
     * 得到表的所有行
     * 
     * @return
     * @throws Exception
     */
    @SuppressWarnings("deprecation")
    public static Set getRows(String tableName) throws Exception {
        HTable hTable = getHTable(tableName);
        Set set = new HashSet();
        Filter filter = new KeyOnlyFilter();
        ResultScanner results = scanAll(hTable,filter);
        for (Result result : results) {  
            for (Cell cell : result.rawCells()) {  
                set.add(new String(CellUtil.cloneRow(cell)));
            }  
        }  
        return set;

    }

    /**
     * 获取一个表所有的列族名
     * 
     * @param tablename
     * @throws IOException
     */
    public static Set getAllColumnFamilies(String tablename) throws IOException {
        HTable table = getHTable(tablename);
        TableDescriptor hTableDescriptor = table.getDescriptor();
        Set fdescriptor = hTableDescriptor.getColumnFamilyNames();
        return fdescriptor;
    }

    /**
     * 获取一个表的所有列
     * 
     * @throws Exception
     */
    @SuppressWarnings("deprecation")
    public static Set getAllColumn(String tableName) throws Exception {
        Set columns = new HashSet();
        //Set fdescriptor = getAllColumnFamilies(tableName);
        Set rowKey = getRows(tableName);
        String row = null;
        if (!rowKey.isEmpty()) {
            for (String string : rowKey) {
                row = string;
                break;
            }
        }
        HTable hTable = getHTable(tableName);
        Result result = getRow(hTable, row);
        for (Cell cell : result.rawCells()) { 
            System.out.println(new String(CellUtil.cloneQualifier(cell)));
            columns.add(new String(CellUtil.cloneQualifier(cell)));
            
         }  

        return columns;
    }
    
   @SuppressWarnings("deprecation")
  private static HTable getHTable(String tablename) throws IOException {
       return (HTable) connection.getTable(TableName.valueOf(tablename));
   }
   
   private static Table getTable(String tablename) throws IOException {
        return (Table) connection.getTable(TableName.valueOf(tablename));
    }
  /* public static ResultScanner processResultSet(String tablename, List list) throws Exception {
        Table table = getTable(tablename);
        Scan s = new Scan();
        byte[][] columnByte = new byte[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            columnByte[i] = list.get(i).getBytes();
        }
        System.out.println("length-------------------"+columnByte.length);
        MultipleColumnPrefixFilter cpf = new MultipleColumnPrefixFilter(columnByte);
        s.setFilter(cpf);
        //ResultScanner rs = table.getScanner(s);
        return rs;
    }*/
   
    public static void main(String[] args) throws Exception {
        //HTable hTable = new HTable(conf, TableName.valueOf("student"));
        // createTable("student","info");
        /*
         * putCell(hTable,"0001","info","weight","12.9");
         * putCell(hTable,"0002","info","weight","67.9");
         * putCell(hTable,"0003","info","weight","68.3");
         * putCell(hTable,"0004","info","weight","89.4");
         * putCell(hTable,"0005","info","weight","100");
         * putCell(hTable,"0006","info","weight","45");
         * putCell(hTable,"0007","info","weight","76.9");
         * putCell(hTable,"0008","info","weight","43.8");
         * putCell(hTable,"0009","info","weight","65.67");
         * putCell(hTable,"0010","info","weight","78");
         * putCell(hTable,"0011","info","weight","67");
         * putCell(hTable,"0012","info","weight","78");
         * putCell(hTable,"0013","info","weight","89");
         */
        //putCell(getHTable("student"),"0013","info","text","89");
        // deleteTable("student");
        /*
         * putCell(hTable,"0001","info","sex","男");
         * putCell(hTable,"0003","info","sex","男");
         * putCell(hTable,"0004","info","sex","男");
         * putCell(hTable,"0005","info","sex","男");
         * putCell(hTable,"0006","info","sex","女");
         * putCell(hTable,"0007","info","sex","女");
         */
        // Result result= getRow(hTable,"0001");

        // byte[] value = result.getValue("0001".getBytes());
        // String m = new String(value);
        // System.out.println(result.getColumnLatest("info".getBytes(),
        // "name".getBytes("utf-8")));
        // deleteRow(hTable,"0001");
        // scanAll(hTable);
        // 获取表名
        /*
         * HTableDescriptor[] tables=seeTables(); System.out.println(tables.length);
         * for(int i=0;i          * System.out.println(tables[i].getTableName()); }
         */

        /*
         * ResultScanner rs = scanAll(hTable); for (Result result : rs) {
         * 
         * System.out.println(result); System.out.println(result.current());
         * List key=result.list(); for (KeyValue keyValue : key) {
         * System.out.println("List : "+keyValue.getKeyString()
         * +" : "+keyValue); } List cell=result.listCells(); for (Cell cell2 :
         * cell) { System.out.println("List :"+cell2.getFamily().toString()); }
         * 
         * 
         * // System.out.println(result.getRow());
         * 
         * KeyValue[] kv = result.raw(); for (int i = 0; i < kv.length; i++) {
         * System.out.println(new String(kv[i].getRow()) + "");// 行号
         * System.out.println(new String(kv[i].getFamily()) + ":");// 列族
         * System.out.println(new String(kv[i].getQualifier()) + "");// 列
         * System.out.print(kv[i].getTimestamp() + "");// 时间措 System.out.println(new
         * String(kv[i].getValue()));// 值
         * 
         * }
         * 
         * }
         */
        //getAllColumn("student");
       //createTable("yufei","D");
        

    }}
 

你可能感兴趣的:(hbase)