eclipse连接hbase,并对hbase进行基本操作

6 篇文章 0 订阅
1 篇文章 0 订阅

最近有一个大数据的作业,首先安装hbase,然后使用eclipse去操作hbase,并对数据库进行基本操作,但是对于hbase不是太了解,不是很理解在hbase这种列族型数据库如何联系实体,类似于关系型数据库一样,所以我只创建了几个数据库,然后分别对它们进行操作,有看到大佬的博客说了如何进行一对多,多对多的关联,实质为:在每一个记录的列族中进行横向添加,然后一级一级的往下联系起来,但是官方又说最好不要有太长的列族元子元素,很疑惑,so give up。
对于hbase数据库基本操作
代码如下:

package name1;

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.KeyValue;
import org.apache.hadoop.hbase.HTableDescriptor;
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.HTablePool;
//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.client.Table;
 
public class HDFShbase {
	private HBaseAdmin admin = null;
	// 定义配置对象HBaseConfiguration
	private HBaseConfiguration cfg = null;
	public HDFShbase() throws Exception {
		Configuration HBASE_CONFIG = new Configuration();
	
 
		HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.56.100");
 
		HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
 
		cfg = new HBaseConfiguration(HBASE_CONFIG);
	
		admin = new HBaseAdmin(cfg);
	}
 
	// 创建一张表,指定表名,列族
	public void createTable(String tableName, String columnFarily)
			throws Exception {
 
		if (admin.tableExists(tableName)) {
			System.out.println(tableName + "存在!");
			System.exit(0);
		} else {
			HTableDescriptor tableDesc = new HTableDescriptor(tableName);
			
			tableDesc.addFamily(new HColumnDescriptor(columnFarily));
			admin.createTable(tableDesc);
			System.out.println("创建表成功!");
		}
	}
 
	// Hbase获取所有的表信息
	public List getAllTables() {
		List<String> tables = null;
		if (admin != null) {
			try {
				HTableDescriptor[] allTable = admin.listTables();
				if (allTable.length > 0)
					tables = new ArrayList<String>();
				for (HTableDescriptor hTableDescriptor : allTable) {
					tables.add(hTableDescriptor.getNameAsString());
					System.out.println(hTableDescriptor.getNameAsString());
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return tables;
	}
 
	// Hbase中往某个表中添加一条记录
	public boolean addOneRecord(String table, String key, String family,
			String col, byte[] dataIn) {
		HTablePool tp = new HTablePool(cfg, 1000);
		Table tb = (Table) tp.getTable(table);
		Put put = new Put(key.getBytes());
		put.add(family.getBytes(), col.getBytes(), dataIn);
		try {
			tb.put(put);
			System.out.println("插入数据条" + key + "成功!!!");
			return true;
		} catch (IOException e) {
			System.out.println("插入数据条" + key + "失败!!!");
			return false;
		}
	}
 
	// Hbase表中记录信息的查询
	public void getValueFromKey(String table, String key) {
		HTablePool tp = new HTablePool(cfg, 1000);
		
		Table tb = (Table) tp.getTable(table);
		Get get = new Get(key.getBytes());
		try {
			Result rs = tb.get(get);
			if (rs.raw().length == 0) {
				System.out.println("不存在关键字为" + key + "的行!!");
 
			} else {
				for (KeyValue kv : rs.raw()) {
					System.out.println(new String(kv.getKey()) + " "
							+ new String(kv.getValue()));
				}
 
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
	// 显示所有数据,通过HTable Scan类获取已有表的信息
	public void getAllData(String tableName) throws Exception {
		HTable table = new HTable(cfg, tableName);
		Scan scan = new Scan();
		ResultScanner rs = table.getScanner(scan);
		for (Result r : rs) {
			for (KeyValue kv : r.raw()) {
				System.out.println(new String(kv.getKey())
						+ new String(kv.getValue()));
			}
		}
	}
 
	// Hbase表中记录信息的删除
	public boolean deleteRecord(String table, String key) {
		HTablePool tp = new HTablePool(cfg, 1000);
		Table tb = (Table) tp.getTable(table);
		Delete de = new Delete(key.getBytes());
		try {
			tb.delete(de);
			System.out.println("删除success");
			return true;
		} catch (IOException e) {
			System.out.println("删除记录" + key + "异常!!!");
			return false;
		}
	}
 
	// Hbase中表的删除
	public boolean deleteTable(String table) {
		try {
			if (admin.tableExists(table)) {
				admin.disableTable(table);
				admin.deleteTable(table);
				System.out.println("删除表" + table + "!!!");
			}
			return true;
		} catch (IOException e) {
			System.out.println("删除表" + table + "异常!!!");
			return false;
		}
	}
 
	// 测试函数
	public static void main(String[] args) {
		try {
			HDFShbase hbase = new HDFShbase();
			//hbase.createTable("student1", "fam1");
			//System.out.println("create success");
			hbase.getAllTables();
 
			 //hbase.addOneRecord("student1","id1","fam1","name1","Jack".getBytes());
			 //hbase.addOneRecord("student1","id1","fam1","address","HZ".getBytes());
			 //hbase.getValueFromKey("student1","id1");
			//hbase.getAllData("student1");
			hbase.getAllData("student1");
			hbase.deleteRecord("student1", "id1");
			hbase.getAllData("student1");
			//hbase.deleteTable("student1");
			//hbase.getAllTables();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

大佬的关系设计url:hbase关联性设计

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值