集群中通过Java操作Hbase 0.98

Hbase是一个分布式的开源数据库,在集群中可以通过Shell操作Hbase数据库,也可以通过Java的提供的API对HBase进行操作。但是Hbase的API更新前后版本会有一些不同,很多方法在新版的HBase之中都已经过期了,而新版本的API调用在网上的资料也非常的少,于是这里采用新版本的HBase的API进行操作。


import java.util.ArrayList;
import java.util.List;

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.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
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;

public class HBaseBasic {
	
	private HBaseAdmin admin = null;
	private Configuration conf = null;
	
	public HBaseBasic() throws Exception
	{
		conf = HBaseConfiguration.create();
		//配置参数,第一个是主机名,第二个是端口号
		conf.set("hbase.zookeeper.quorum", "hadoop");
		conf.set("hbase.zookeeper.property.clientPort", "2181");
		admin = new HBaseAdmin(conf);
	}
	
	public void createTable (String tableName , String[] columnFamily) throws Exception
	{
		if (admin.tableExists(tableName))
		{
			System.out.println(tableName + "已存在");
			return ;
			//System.exit(0);
		}
		
		HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
		
		for (String colunm : columnFamily)
		{
			tableDescriptor.addFamily(new HColumnDescriptor(colunm));
		}
		
		admin.createTable(tableDescriptor);
		System.out.println("Create table successfully..");
	}
	
	public boolean deleteTable (String tableName)
	{
		try {
			if(admin.tableExists(tableName))
			{
				admin.disableTable(tableName);
				admin.deleteTable(tableName);
				System.out.println("drop table " + tableName);
			}
			return true;
		} catch (Exception e) {
			System.out.println("删除" + tableName + "失败");
			return false;
		}
	}
	
	public List getAllTables()
	{
		List<String> tables = null;
		if (admin != null)
		{
			try{
				HTableDescriptor[] allTables = admin.listTables();
				if(allTables.length > 0)
				{
					tables = new ArrayList<String>();
				}
				
				for (HTableDescriptor tableDesc : allTables)
				{
					tables.add(tableDesc.getNameAsString());
					System.out.println(tableDesc.getNameAsString());
				}
			}catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
		
		return tables;
	}
	
	public boolean addOneRecord (String tableName , String key , String family , String column 
			, byte[] dataIn) throws Exception
	{
		HConnection connection = HConnectionManager.createConnection(conf);
		//HTable table = new HTable(hbaseConf, tableName);
		HTable table = (HTable)connection.getTable(tableName);
		Put put = new Put(key.getBytes());
		put.add(family.getBytes(), column.getBytes(), dataIn);
		try {
			table.put(put);
			System.out.println("插入数据条 " + key + "成功");
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("插入数据条 " + key + "失败");
			return false;
		}
	}
	
	public void getValueFromKey (String tableName , String key)
	{
		try{
			HConnection conn = HConnectionManager.createConnection(conf);
			HTable table = (HTable) conn.getTable(tableName);
			Get get = new Get(key.getBytes());
			Result rs = table.get(get);
			if (rs.rawCells().length == 0)
			{
				System.out.println("不存在关键字为" + key + "的行...");
			}
			else
			{
				for (Cell cell : rs.rawCells())
				{
					System.out.println(new String(CellUtil.cloneFamily(cell)) + 
							"  " + new String(CellUtil.cloneQualifier(cell)) + "  " + new String(CellUtil.cloneValue(cell)));
				}
			}
		}
		catch(Exception ex)
		{
			System.out.println("查询失败");
			ex.printStackTrace();
		}
	}
	
	
	
	public void getAllData(String tableName) throws Exception
	{
		HConnection conn = HConnectionManager.createConnection(conf);
		HTable table = (HTable) conn.getTable(tableName);
		Scan scan = new Scan();
		ResultScanner rs = table.getScanner(scan);
		for (Result result : rs)
		{
			for (Cell cell : result.rawCells())
			{
				System.out.println("RowName: " + new String(CellUtil.cloneRow(cell)) + " ");
				System.out.println("Timetamp: " + cell.getTimestamp() + " ");
				System.out.println("Column family: " + new String(CellUtil.cloneFamily(cell)) + " ");
				System.out.println("row name: " + new String(CellUtil.cloneQualifier(cell)) + " ");
				System.out.println("value: " + new String(CellUtil.cloneValue(cell)) + " ");
			}
		}
	}
	
	public void deleteRecord(String tableName , String key)
	{
		try
		{
			HConnection conn = HConnectionManager.createConnection(conf);
			HTable table = (HTable)conn.getTable(tableName);
			Delete delete = new Delete(key.getBytes());
			
			table.delete(delete);
			System.out.println("删除" + key +"成功...");
		}
		catch(Exception ex)
		{
			System.out.println("删除数据失败...");
			ex.printStackTrace();
		}
	}
	

	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		String[] column = {"family1" , "family2"};
		try {
			HBaseBasic hbase = new HBaseBasic();
			hbase.deleteTable("students");
			hbase.createTable("students", column);
			//hbase.getAllData("scores");
			hbase.addOneRecord("students", "id1", "family1", "name", "Jack".getBytes());
			hbase.addOneRecord("students", "id1", "family1", "grade", "gaosan".getBytes());
			//hbase.getAllTables();
			//hbase.getAllData("students");
			hbase.getValueFromKey("students", "id1");
			hbase.deleteRecord("students", "id1");
			hbase.addOneRecord("students", "id2", "family1", "name", "Holen".getBytes());
			hbase.getValueFromKey("students", "id2");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		
	}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值