Hbase学习笔记(四)java访问部署在Windows下的Hbase

package com.zfc.test.controller.test;

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.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.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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;


public class TestHbase{

	private static final String ROWKEY = "p001";
    private static final String ROWKEY2 = "p002";
    private static final String FAMILY1 = "cf1";
    private static final String FAMILY2 = "cf2";
    private static final String KEY = "name";
    private static final String VALUE = "huangbo";
	
	
	public static Connection connection;
	
	public static Configuration configuration;

	
	static{
		
		configuration = HBaseConfiguration.create();
		
		//设置连接参数:Hbase数据库使用的端口
		configuration.set("hbase.zookeeper.property.clientPort", "2181");
		//设置连接参数:Hbase数据库所在的主机Ip
		configuration.set("hbase.zookeeper.quorum", "127.0.0.1");
		configuration.set("zookeeper.znode.parent", "/hbase");
		
		try {
			//取的一个数据库连接对象
			connection = ConnectionFactory.createConnection(configuration);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		// 创建表
		String tableName = "blog2";
		String[] family = { "member", "author" };
		//createTable(tableName, family);
		descTable("member");
		String[] s = {"1","2"};	
		t(s);
	}

	
	//创建表
	public static void  createTable(String tableName,String[] family) throws IOException {
		
		Admin admin = connection.getAdmin();
		 //HTD需要TableName类型的tableName,创建TableName类型的tableName
		TableName tbName = TableName.valueOf(tableName);
		
		 //判断表述否已存在,不存在则创建表
		if(admin.tableExists(tbName)){
			System.out.println("表:" + tbName + "已存在!");
			return ;
		}
		
		//通过HTableDescriptor创建一个HTableDescriptor将表的描述传到createTable参数中
		HTableDescriptor  HTD = new HTableDescriptor(tableName);
	      //为描述器添加表的详细参数
        for(String fl : family){
            // 创建HColumnDescriptor对象添加表的详细的描述
            HColumnDescriptor HCD =new HColumnDescriptor(fl);
            HTD.addFamily(HCD);
        }
        //调用createtable方法创建表
        admin.createTable(HTD);
        System.out.println("创建成功");
        admin.close();

	}
	
	//创建表
	public static void createTable(String tableName, HTableDescriptor htds) throws Exception {
        HBaseAdmin admin=new HBaseAdmin(configuration);
        boolean tableExists1 = admin.tableExists(Bytes.toBytes(tableName));
        System.out.println(tableExists1 ? "表已存在" : "表不存在");
        admin.createTable(htds);
        boolean tableExists = admin.tableExists(Bytes.toBytes(tableName));
        System.out.println(tableExists ? "创建表成功" : "创建失败");
        admin.close();
	}
	
	//获取表 列簇
    public static void descTable(String tableName) throws Exception {
        HBaseAdmin admin=new HBaseAdmin(configuration);
        HTable table=new HTable(configuration, tableName);
        HTableDescriptor desc =table.getTableDescriptor();
        HColumnDescriptor[] columnFamilies = desc.getColumnFamilies();
     
        for(HColumnDescriptor t:columnFamilies){
            System.out.println(Bytes.toString(t.getName()));
        }
         
        admin.close();
    }
	
    //修改列簇
    public static void modifyTable(String tableName) throws Exception {
        HBaseAdmin admin=new HBaseAdmin(configuration);
        HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
        htd.addFamily(new HColumnDescriptor(Bytes.toBytes("cf3")));
        htd.addFamily(new HColumnDescriptor(Bytes.toBytes("cf2")));
        admin.modifyTable(tableName, htd);
 
        // 删除该表tableName当中的特定的列簇
        // admin.deleteColumn(tableName, "cf3");
 
        System.out.println("修改成功");
        admin.close();
    }
    
    //获取所有的表
    public static void getAllTables() throws Exception {
        HBaseAdmin admin =new HBaseAdmin(configuration);
         
        String[] tableNames = admin.getTableNames();
        for(int i=0;i<tableNames.length;i++){
            System.out.println(tableNames[i]);
        }
        admin.close();
    }
    
    //插入数据
    public static void putData(String tableName, String rowKey, String familyName, String columnName, String value)
            throws Exception {
        HTable htable=new HTable(configuration, Bytes.toBytes(tableName));
        Put put=new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));
        htable.put(put);
        htable.close();
        
    /*    Table  tb = connection.getTable(TableName.valueOf(tableName));
        Put put2 = new Put(Bytes.toBytes(rowKey));
        put2.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));
        tb.put(put2);
        tb.close();*/
        
    }
    
    //插入数据
    public void putData(String tableName, String rowKey, String[] column1, String[] value1, String[] column2,
            String[] value2) throws Exception {
         
        Put put=new Put(Bytes.toBytes(rowKey));
        HTable htable=new HTable(configuration, Bytes.toBytes(tableName));
        HColumnDescriptor[] columnFamilies = htable.getTableDescriptor().getColumnFamilies();
        for(int i=0;i<=columnFamilies.length;i++){
            String nameAsString = columnFamilies[i].getNameAsString();
            if(nameAsString.equals("lie01")){
                for(int j=0;j<column1.length;j++){
                    put.addColumn(Bytes.toBytes(nameAsString), Bytes.toBytes(column1[j]),Bytes.toBytes(value1[j]));
                }
            }
            if(nameAsString.equals("lie02")){
                for(int j=0;j<column2.length;j++){
                    put.addColumn(Bytes.toBytes(nameAsString), Bytes.toBytes(column2[j]),Bytes.toBytes(value2[j]));
                }
            }
             
        }
        htable.put(put);
        System.out.println("addData ok!");
        htable.close();
        
       
        Table table = connection.getTable(TableName.valueOf(tableName));
        
        Put put2 = new Put(Bytes.toBytes(rowKey));
        
        HColumnDescriptor[] columnFamilies2 = table.getTableDescriptor().getColumnFamilies();
        
        for (int i = 0; i < columnFamilies2.length; i++) {
			
        	String nameAsString = columnFamilies2[i].getNameAsString();
        	 if(nameAsString.equals("cf1")){
                 for(int j=0;j<column1.length;j++){
                     put2.addColumn(Bytes.toBytes(nameAsString), Bytes.toBytes(column1[j]),Bytes.toBytes(value1[j]));
                 }
             }
             if(nameAsString.equals("cf2")){
                 for(int j=0;j<column2.length;j++){
                     put2.addColumn(Bytes.toBytes(nameAsString), Bytes.toBytes(column2[j]),Bytes.toBytes(value2[j]));
                 }
             }
		}
        
        table.put(put2);
        System.out.println("addData ok!");
        table.close();
        
    }
    
    public void addData(String tableName, String rowKey, String[] column1, String[] value1, String[] column2, String[] value2) throws Exception {
        
    	Table table = connection.getTable(TableName.valueOf(tableName));
    	List<Put> puts = new ArrayList<Put>();
 
        for (int i = 0; i < column1.length; i++) {
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(FAMILY1), Bytes.toBytes(column1[i]), Bytes.toBytes(value1[i]));
            puts.add(put);
        }
 
        for (int i = 0; i < column2.length; i++) {
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(FAMILY2), Bytes.toBytes(column2[i]), Bytes.toBytes(value2[i]));
            puts.add(put);
        }
 
        table.put(puts);
        System.out.println("插入一堆数据成功");
    }
    
    
    //查询一条记录
    public Result getResult(String tableName, String rowKey) throws Exception {
      
    	Table table = connection.getTable(TableName.valueOf(tableName));
    	
    	Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        return result;
    }
 
  //查询 rowKey 列簇:列
    public Result getResult(String tableName, String rowKey, String familyName, String columnName) throws Exception {
        
    	Table table = connection.getTable(TableName.valueOf(tableName));
    	Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
        Result result = table.get(get);
        table.close();
        return result;
    }
    
    
    public ResultScanner getResultScann(String tableName, Scan scan) throws Exception {
    	Table table = connection.getTable(TableName.valueOf(tableName));
    	table.close();
    	return table.getScanner(scan);
    }
 
    public Result getResultByColumn(String tableName, String rowKey, String familyName, String columnName) throws Exception {
        return null;
    }
 
    // get 'person','p001',{COLUMNS => 'cf1:name', VERSIONS => 3}
    public Result getResultByVersion(String tableName, String rowKey, String familyName, String columnName, int versions) throws Exception {
    	Table table = connection.getTable(TableName.valueOf(tableName));
    	Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
        get.setMaxVersions(versions);
        Result result = table.get(get);
        table.close();
        return result;
    }
 
    public ResultScanner getResultByVersion(String rowKey, String familyName, String columnName, int versions) throws Exception {
    	Table table = connection.getTable(TableName.valueOf(""));
    	Scan scan = new Scan(Bytes.toBytes(rowKey), Bytes.toBytes(rowKey));
        scan.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
        scan.setMaxVersions(versions);
        ResultScanner scanner = table.getScanner(scan);
        scanner.close();
        table.close();
        return scanner;
    }
 

 
    public void deleteColumn(String tableName, String rowKey) throws Exception {
 				Table table = connection.getTable(TableName.valueOf(tableName));
 				Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
        table.close();
        
 				
    }
    
    
	public static void deleteColumn(String tableName, String rowKey, String falilyName, String columnName)throws IOException {
		HTable table = new HTable(configuration, Bytes.toBytes(tableName));
		Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
		deleteColumn.deleteColumns(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
		table.delete(deleteColumn);
		System.out.println(falilyName + ":" + columnName + "is deleted!");

	}
 
    public void disableTable(String tableName) throws Exception {
    	HBaseAdmin admin =new HBaseAdmin(configuration);
    	admin.disableTable(tableName);
    }
 
    public void dropTable(String tableName) throws Exception {
    	HBaseAdmin admin =new HBaseAdmin(configuration);
    	try {
            admin.deleteTable(tableName);
        } catch (Exception e) {
            // e.printStackTrace();
            disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println("ssssssss");
        } finally {
            boolean tableExists = admin.tableExists(Bytes.toBytes(tableName));
            System.out.println(tableExists ? "删除失败" : "删除成功");
        }
    }
    
     /**
     * put a cell data into a row identified by rowKey,columnFamily,identifier 
     * @param HTable, create by : HTable table = new HTable(conf, "tablename")
     * @param rowKey
     * @param columnFamily
     * @param identifier
     * @param data
     * @throws Exception
     */
    public static void putCell(HTable table, String rowKey, String columnFamily, String identifier, String data) throws Exception{
        Put p1 = new Put(Bytes.toBytes(rowKey));
        p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(identifier), Bytes.toBytes(data));
        table.put(p1);
        System.out.println("put '"+rowKey+"', '"+columnFamily+":"+identifier+"', '"+data+"'");
    }

    /**
     * get a row identified by rowkey
     * @param HTable, create by : HTable table = new HTable(conf, "tablename")
     * @param rowKey
     * @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;
    }

    /**
     * delete a row identified by rowkey
     * @param HTable, create by : HTable table = new HTable(conf, "tablename")
     * @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);
    }

    /**
     * return all row from a table
     * @param HTable, create by : HTable table = new HTable(conf, "tablename")
     * @throws Exception
     */
    public static ResultScanner scanAll(HTable table) throws Exception {
        Scan s =new Scan();
        ResultScanner rs = table.getScanner(s);
        return rs;
    }

    /**
     * return a range of rows specified by startrow and endrow
     * @param HTable, create by : HTable table = new HTable(conf, "tablename")
     * @param startrow
     * @param endrow
     * @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;
    }
    /**
     * return a range of rows filtered by specified condition
     * @param HTable, create by : HTable table = new HTable(conf, "tablename")
     * @param startrow
     * @param filter
     * @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;
    }

    
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值