Hbase实例

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

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.KeyValue;
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.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.util.Bytes;

public class OperateTable {
    private static Configuration configuration=null;
    static{
        configuration=HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "master");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
    }
    
    public static void createTable(String tableName,String[] columnFamilys) throws IOException {
        HBaseAdmin admin=new HBaseAdmin(configuration);
        if (admin.tableExists(tableName)) {
            System.out.println("表已存在");
            System.exit(0);
        }
        else {
            HTableDescriptor descriptor=new HTableDescriptor(tableName);
            for (String columnFamily:columnFamilys) {
                descriptor.addFamily(new HColumnDescriptor(columnFamily));
            }
            admin.createTable(descriptor);
            System.out.println("创建表成功");
        }
    }
    
    //删除数据库表
    public static void deleteTable(String tableName) throws Exception, ZooKeeperConnectionException {
        HBaseAdmin admin=new HBaseAdmin(configuration);
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println("删除表成功");
        }
        else {
            System.out.println("删除的表不存在");
            System.exit(0);
        }
    }
    
    //添加一条数据
    public static void addRow(String tableName,String row,String columnFamily,String column,String value) throws IOException {
        HTable table=new HTable(configuration,tableName);
        Put put=new Put(Bytes.toBytes(row));
        put.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        table.put(put);
    }
    
    //删除一条数据
    public static void  delRow(String tableName,String row) throws Exception {
        HTable table=new HTable(configuration,tableName);
        Delete delete=new Delete(Bytes.toBytes(row));
        table.delete(delete);
    }
    
    //删除多条数据
    public static void delMulRows(String tableName,String[] rows) throws Exception{
        HTable table=new HTable(configuration,tableName);
        List<Delete> list=new ArrayList<Delete>();
        for (String row:rows) {
            Delete delete=new Delete(Bytes.toBytes(row));
            list.add(delete);
        }
        table.delete(list);
    }
    
    //获取一行数据
    public static void getRow(String tableName,String row) throws Exception {
        HTable table=new HTable(configuration,tableName);
        Get get=new Get(Bytes.toBytes(row));
        Result result=table.get(get);
        for(KeyValue keyValue:result.raw()){
            System.out.println("Row Name:"+new String(keyValue.getRow())+" ");
            System.out.println("Timestamp:"+keyValue.getTimestamp()+" ");
            System.out.println("Column Family:"+new String(keyValue.getFamily())+" ");
            System.out.println("Row Name:"+new String(keyValue.getQualifier())+" ");
            System.out.println("Value:"+new String(keyValue.getValue())+" ");
        }
    }
    
    //获取所有数据
    public static void  getAllRows(String tableName) throws Exception {
        HTable table=new HTable(configuration,tableName);
        Scan scan=new Scan();
        ResultScanner resultScanner=table.getScanner(scan);
        for(Result result:resultScanner){
            for(KeyValue keyValue:result.raw()){
                System.out.print("Row Name:"+new String(keyValue.getRow())+" ");
                System.out.println("Timestamp:"+keyValue.getTimestamp()+" ");
                System.out.println("Column Family:"+new String(keyValue.getFamily())+" ");
                System.out.println("Row Name:"+new String(keyValue.getQualifier())+" ");
                System.out.println("Value:"+new String(keyValue.getValue())+" ");
            }
        }
    }
    
    public static void main(String[] args) {
        try {
            String tableName="users";
            
            // 第一步:创建数据库表:“users” 
            String[] columnFamilyStrings={"info","course"};
            OperateTable.createTable(tableName, columnFamilyStrings);
            
             // 第二步:向数据表的添加数据   
            // 添加第一行数据   
            OperateTable.addRow(tableName, "tht", "info", "age", "20");  
            OperateTable.addRow(tableName, "tht", "info", "sex", "boy");  
            OperateTable.addRow(tableName, "tht", "course", "china", "97");  
            OperateTable.addRow(tableName, "tht", "course", "math", "128");  
            OperateTable.addRow(tableName, "tht", "course", "english", "85");  
            // 添加第二行数据   
            OperateTable.addRow(tableName, "xiaoxue", "info", "age", "19");  
            OperateTable.addRow(tableName, "xiaoxue", "info", "sex", "boy");  
            OperateTable.addRow(tableName, "xiaoxue", "course", "china", "90");  
            OperateTable.addRow(tableName, "xiaoxue", "course", "math", "120");  
            OperateTable.addRow(tableName, "xiaoxue", "course", "english", "90");  
            // 添加第三行数据   
            OperateTable.addRow(tableName, "qingqing", "info", "age", "18");  
            OperateTable.addRow(tableName, "qingqing", "info", "sex", "girl");  
            OperateTable.addRow(tableName, "qingqing", "course", "china", "100");  
            OperateTable.addRow(tableName, "qingqing", "course", "math", "100");  
            OperateTable.addRow(tableName, "qingqing", "course", "english", "99"); 
            
            System.out.println("获取一条数据:");
            OperateTable.getRow(tableName, "tht");
            
            System.out.println("获取所有数据");  
            OperateTable.getAllRows(tableName);
            
            System.out.println("删除一条数据");
            OperateTable.delRow(tableName, "tht");
            OperateTable.getAllRows(tableName);
            
            System.out.println("删除多条数据"); 
            String[] rowsStrings={ "xiaoxue", "qingqing" }; 
            OperateTable.delMulRows(tableName, rowsStrings);
            OperateTable.getAllRows(tableName);
            
            System.out.println("删除数据库");  
            OperateTable.deleteTable(tableName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
View Code

 

posted @ 2014-02-12 10:39  liutoutou  阅读(187)  评论(0编辑  收藏  举报