hbase数据增删改查

/**
*想了解hbase的基本用法,参照别人的例子,写了一些增删改查的代码,实际测试测试
*测试环境:单机环境 版本:hadoop1.1.2 hbase0.94.7
*/

package com.cn.lmc.hbase.basic;

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.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

public class HbaseConnection {
	
	
	public static Configuration configuration;
	static
	{
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.master", "localhost:60000");
		configuration.set("hbase.zookeeper.quorum", "localhost");
		configuration.set("hbase.zookeeper.property.clientPort", "21818");
	}
	
	public static void createTable(String tableName) throws IOException
	{
		System.out.println("[INFO] starting create table: "+ tableName);
		HBaseAdmin hbaseAdmin = new HBaseAdmin(configuration);
		
		if(hbaseAdmin.tableExists(tableName))
		{
			System.out.println("[INFO] table["+tableName+"] exist, delete firstly.");
			hbaseAdmin.disableTable(tableName);
			hbaseAdmin.deleteTable(tableName);
			System.out.println("[INFO] table["+tableName+"] deleted.");
		}
		
		HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
		tableDescriptor.addFamily(new HColumnDescriptor("ColumnFamily1"));
		tableDescriptor.addFamily(new HColumnDescriptor("ColumnFamily2"));
		tableDescriptor.addFamily(new HColumnDescriptor("ColumnFamily3"));
		hbaseAdmin.createTable(tableDescriptor);
		hbaseAdmin.close();
		
		System.out.println("[INFO] table: "+ tableName + " created.");
	}
	
	public static void insert(String tableName) throws IOException
	{
		System.out.println("[INFO] starting insert data to table: "+ tableName);
		HTablePool tablePool = new HTablePool(configuration, 100);
		HTableInterface table = tablePool.getTable(tableName);
		for(int i=0; i<3; i++)
		{
			Put put = new Put(("Row"+i).getBytes());
			put.add("ColumnFamily1".getBytes(), null, ("row_"+i+"_column_1").getBytes());
			put.add("ColumnFamily2".getBytes(), null, ("row_"+i+"_column_2").getBytes());
			put.add("ColumnFamily3".getBytes(), null, ("row_"+i+"_column_3").getBytes());
			System.out.println("[INFO] starting insert ["+"row_"+i+"_column_1, row_"+i+"_column_2, row_"+i+"_column_3] to table ["+ tableName+"]");
			table.put(put);
		}
		table.flushCommits();
		table.close();
		tablePool.close();
	}
	
	public static void deleteByRowKey(String tableName, String rowKey) throws IOException
	{
		System.out.println("[INFO] starting delete data from table: "+ tableName+" where rowKey="+rowKey);
		HTablePool tablePool = new HTablePool(configuration,100);
		HTableInterface table = tablePool.getTable(tableName);
		List<Delete> list = new ArrayList<Delete>();
		Delete delete = new Delete(rowKey.getBytes());
		list.add(delete);
		table.delete(list);
		table.close();
		tablePool.close();
		System.out.println("[INFO] delect data from table: "+ tableName+" finished.");
		
		
	}
	
	public static void updateCell(String tableName, String rowKey, String columnFamily, String newValue) throws IOException
	{
		System.out.println("[INFO] starting update data from table: "+ tableName);
		HTablePool tablePool = new HTablePool(configuration,100);
		HTableInterface table = tablePool.getTable(tableName);
		Put put = new Put(rowKey.getBytes());
		put.add(columnFamily.getBytes(), null, newValue.getBytes());
		table.put(put);
		table.close();
		tablePool.close();
		System.out.println("[INFO] successfully updated data from table: "+ tableName);
		
	}
	
	/**
	 * Select * from table where rowKey = 'Row1'
	 * @param tableName
	 * @throws IOException
	 */
	public static void queryByCondition1(String tableName) throws IOException
	{
		System.out.println("[INFO] starting select[type1] data from table: "+ tableName);
		HTablePool tablePool = new HTablePool(configuration,100);
		HTableInterface table = tablePool.getTable(tableName);
		Get get = new Get("Row1".getBytes());
		Result result = table.get(get);
		for(KeyValue kv : result.raw())
		{
			System.out.println("Column:"+new String(kv.getFamily())+" Value:"+new String(kv.getValue()));
		}
		table.close();
		tablePool.close();
		System.out.println("[INFO] select[type1] data from table: "+ tableName+ " finished.");
	}
	
	/**
	 * select * from table where columnFamily1='columnFamilyValue1'
	 * @param tableName
	 * @throws IOException
	 */
	public static void queryByCondition2(String tableName) throws IOException
	{
		System.out.println("[INFO] starting select[type2] data from table: "+ tableName);
		HTablePool tablePool = new HTablePool(configuration,100);
		HTableInterface table = tablePool.getTable(tableName);
		Filter filter = new SingleColumnValueFilter("ColumnFamily1".getBytes(),null,CompareOp.EQUAL,"row_2_column_1".getBytes());
		Scan scan = new Scan();
		scan.setFilter(filter);
		ResultScanner result = table.getScanner(scan);
		for(Result rs:result)
		{
			System.out.print("Row:"+new String(rs.getRow()));
			for(KeyValue kv : rs.raw())
			{
				System.out.print(" Column:"+new String(kv.getFamily()) + " value:"+new String(kv.getValue()));
			}
			System.out.println();
		}
		table.close();
		tablePool.close();
		System.out.println("[INFO] select[type2] data from table: "+ tableName+ " finished.");
		
	}
	
	/**
	 * select * from table where columFamily1 = 'para1' and columnFamily2 = 'para2' and columnFamily3 = 'para3'
	 * @param tableName
	 * @throws IOException
	 */
	public static void queryByCondition3(String tableName) throws IOException
	{
		System.out.println("[INFO] starting select[type3] data from table: "+ tableName);
		HTablePool tablePool = new HTablePool(configuration,100);
		HTableInterface table = tablePool.getTable(tableName);
		Filter filter1 = new SingleColumnValueFilter("ColumnFamily1".getBytes(),null,CompareOp.EQUAL,"row_2_column_1".getBytes());
		Filter filter2 = new SingleColumnValueFilter("ColumnFamily2".getBytes(),null,CompareOp.EQUAL,"row_2_column_2".getBytes());
		Filter filter3 = new SingleColumnValueFilter("ColumnFamily3".getBytes(),null,CompareOp.EQUAL,"row_2_column_3".getBytes());
		FilterList filterList = new FilterList();
		filterList.addFilter(filter1);
		filterList.addFilter(filter2);
		filterList.addFilter(filter3);
		Scan scan = new Scan();
		scan.setFilter(filterList);
		ResultScanner result = table.getScanner(scan);
		for(Result rs:result)
		{
			System.out.print("Row:"+new String(rs.getRow()));
			for(KeyValue kv : rs.raw())
			{
				System.out.print(" Column:"+new String(kv.getFamily()) + " value:"+new String(kv.getValue()));
			}
			System.out.println();
		}
		table.close();
		tablePool.close();
		System.out.println("[INFO] select[type3] data from table: "+ tableName+ " finished.");
	}
	
    public static void queryAll(String tableName) throws IOException 
    {
    	System.out.println("[INFO] starting scan data from table: "+ tableName);
        HTablePool pool = new HTablePool(configuration, 100);  
        HTableInterface table = pool.getTable(tableName);  
        ResultScanner rs = table.getScanner(new Scan());  
        for (Result r : rs) 
        {  
            System.out.println("row:" + new String(r.getRow()));  
            for (KeyValue keyValue : r.raw()) 
            {  
                System.out.println(" Volumn:" + new String(keyValue.getFamily()) + " Value:" + new String(keyValue.getValue()));  
            }  
        }  
        pool.close();
    } 
    
    public static void dropTable(String tableName) throws IOException{  
        HBaseAdmin admin = new HBaseAdmin(configuration);  
        admin.disableTable(tableName);  
        admin.deleteTable(tableName);  
        admin.close();
  
    }

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
		    HbaseConnection.createTable("20130619");
		    HbaseConnection.insert("20130619");
		    HbaseConnection.queryByCondition1("20130619");
		    HbaseConnection.queryByCondition2("20130619");
		    HbaseConnection.queryByCondition3("20130619");
		    HbaseConnection.queryAll("20130619");
		    
		    HbaseConnection.updateCell("20130619", "Row2", "ColumnFamily1", "row_2_column_1_newValue");
		    HbaseConnection.queryAll("20130619");
		    
		    HbaseConnection.deleteByRowKey("20130619", "Row2");
		    HbaseConnection.queryAll("20130619");
		    
		    HbaseConnection.dropTable("20130619");
		    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值