java操作hbase例子

hbase安装方法请参考:hbase-0.94安装方法详解

hbase常用的shell命令请参考:hbase常用的shell命令例子


java操作hbase,在eclipse中创建一个java项目,将hbase安装文件根目录的jar包和lib目录下jar包导入项目,然后就可以编写java代码操作hbase了。下面代码给出来一个简单的示例


/**
 * @date 2015-07-23 21:28:10
 * @author sgl
 */
package com.songguoliang.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.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 HBaseTest {
	//声明静态配置,HBaseConfiguration
	private static Configuration conf=null;
	static{
		conf=HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "sdw1,sdw2");
	}
	/**
	 * 创建一个表
	 * @date 2015-07-23 21:44:32
	 * @author sgl
	 * @param tableName 表名
	 * @param columnFamilys 列族
	 * @throws IOException
	 */
	public static void createTable(String tableName,String[]columnFamilys) throws IOException{
		HBaseAdmin admin =new HBaseAdmin(conf);
		if (admin.tableExists(tableName)) {
			System.out.println("table already exists!");
		}else{
			HTableDescriptor tableDesc=new HTableDescriptor(tableName);
			for(int i=0;i<columnFamilys.length;i++){
				tableDesc.addFamily(new HColumnDescriptor(columnFamilys[i]));
			}
			admin.createTable(tableDesc);
			System.out.println("create table "+tableName+" success!");
		}
		
	}
	/**
	 * 添加一条记录
	 * @date 2015-07-23 22:16:30
	 * @author sgl
	 * @param tableName 表名
	 * @param rowKey 行健
	 * @param family 列族
	 * @param qualifier 限定符
	 * @param value 值
	 * @throws IOException
	 */
	public static void addRecord(String tableName,String rowKey,String family,String qualifier,String value) throws IOException{
		HTable table =new HTable(conf, tableName);
		Put put=new Put(Bytes.toBytes(rowKey));
		put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
		table.put(put);
		System.out.println("insert record "+rowKey+" to table "+tableName+" success");
		
	}
	/**
	 * 删除一行记录
	 * @date 2015-07-23 22:17:53
	 * @author sgl
	 * @param tableName 表名
	 * @param rowKey 行健
	 * @throws IOException
	 */
	public static void deleteRecord(String tableName,String rowKey) throws IOException{
		HTable table=new HTable(conf, tableName);
		List<Delete>list=new ArrayList<Delete>();
		Delete delete=new Delete(rowKey.getBytes());
		list.add(delete);
		table.delete(list);
		System.out.println("delete record "+ rowKey+" success!");
		
	}
	/**
	 * 获取一行记录
	 * @date 2015-07-23 22:21:33
	 * @author sgl
	 * @param tableName 表名
	 * @param rowKey 行健
	 * @throws IOException 
	 */
	public static void getOneRecord(String tableName,String rowKey) throws IOException{
		HTable table=new HTable(conf, tableName);
		Get get=new Get(rowKey.getBytes());
		Result rs=table.get(get);
		for(KeyValue kv:rs.raw()){
			System.out.print(new String(kv.getRow()) + " " );       
            System.out.print(new String(kv.getFamily()) + ":" );       
            System.out.print(new String(kv.getQualifier()) + " " );       
            System.out.print(kv.getTimestamp() + " " );       
            System.out.println(new String(kv.getValue()));      
		}
		
	}
	/**
	 * 获取所有数据
	 * @date 2015-07-23 22:26:19
	 * @author sgl
	 * @param tableName 表名
	 * @throws IOException 
	 */
	public static void getAllRecord(String tableName) throws IOException{
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		ResultScanner scanner=table.getScanner(scan);
		for(Result result:scanner){
			for(KeyValue kv:result.raw()){
				System.out.print(new String(kv.getRow()) + " ");       
                System.out.print(new String(kv.getFamily()) + ":");       
                System.out.print(new String(kv.getQualifier()) + " ");       
                System.out.print(kv.getTimestamp() + " ");       
                System.out.println(new String(kv.getValue()));   
			}
		}
		
	}
	/**
	 * 删除一个表
	 * @date 2015-07-23 22:29:35
	 * @author sgl
	 * @param tableName 表名
	 * @throws IOException 
	 */
	public static void deleteTable(String tableName) throws IOException{
		HBaseAdmin admin=new HBaseAdmin(conf);
		admin.disableTable(tableName);
		admin.deleteTable(tableName);
		System.out.println("delete table "+tableName+" success!");
		
	}
	
	public static void main(String[] args) {
		try {
			String tableName="scores";
			String[]columnFamilys={"grade","course"};
			HBaseTest.createTable(tableName, columnFamilys);
			
			HBaseTest.addRecord(tableName, "sgl", "grade", "", "5");
			HBaseTest.addRecord(tableName, "sgl", "course", "", "90");
			HBaseTest.addRecord(tableName, "sgl", "course", "math", "97");
			HBaseTest.addRecord(tableName, "sgl", "course", "art", "87");
			HBaseTest.addRecord(tableName, "guoguo", "grade", "", "4");
			HBaseTest.addRecord(tableName, "guoguo", "course", "math", "89");
			
			System.out.println("********get one record*********");
			HBaseTest.getOneRecord(tableName, "sgl");
			
			System.out.println("********get all record*********");
			HBaseTest.getAllRecord(tableName);
			
			System.out.println("********delete one record*********");
			HBaseTest.deleteRecord(tableName, "guoguo");
			HBaseTest.getAllRecord(tableName);
			
			System.out.println("********delete table*********");
			HBaseTest.deleteTable(tableName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值