Hbase基本操作示例

Hadoop Hbase通过行关键字、列(列族名:列名)和时间戳的三元组确定一个存储单元(cell),即由

{row key, column family, column name, timestamp} 可以唯一确定一个存储值,即一个键值对:

{row key, column family, column name, timestamp} -> value


下面演示了Hbase的基本操作。包括

1、创建表

2、删除表

3、添加记录

4、删除记录

5、查询记录等


忘记的时候可以过来看看。

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;

public class HBaseOperation {

	// 相关属性
	private Configuration conf;
	private HBaseAdmin admin;

	public HBaseOperation(Configuration conf) throws IOException {
		this.conf = HBaseConfiguration.create(conf);
		this.conf.set("hbase.zookeeper.quorum", "namenode");
		this.conf.set("hbase.zookeeper.property.clientPort", "2181");
		this.admin = new HBaseAdmin(this.conf);
	}

	public HBaseOperation() throws IOException {
		Configuration cnf = new Configuration();
		this.conf = HBaseConfiguration.create(cnf);
		this.conf.set("hbase.zookeeper.quorum", "namenode");
		this.conf.set("hbase.zookeeper.property.clientPort", "2181");
		this.admin = new HBaseAdmin(this.conf);
	}

	/**
	 * 创建表
	 * @param tableName		表名
	 * @param colFamilies	<span style="white-space:pre">	</span>列族
	 * @throws IOException
	 */
	public void createTable(String tableName, String colFamilies[])
			throws IOException {

		if (this.admin.tableExists(tableName)) { // 表已经存在
			System.out.println("Table: " + tableName + " already exists !");
			this.admin.deleteTable(tableName); // 删除表
		}

		HTableDescriptor dsc = new HTableDescriptor(tableName); // 新建表描述
		int len = colFamilies.length;
		for (int i = 0; i < len; i++) {
			HColumnDescriptor family = new HColumnDescriptor(colFamilies[i]);
			dsc.addFamily(family); // 在表描述中添加列族
		}
		admin.createTable(dsc); // 创建表
		System.out.println("create table " + tableName + " ok.");
	}

	/**
	 * 删除一张表
	 * @param tableName
	 * @throws IOException
	 */
	public void deleteTable(String tableName) throws IOException {
		if (this.admin.tableExists(tableName)) {
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println("delete table " + tableName + " ok.");
		} else {
			System.out.println("Table Not Exists !");
		}
	}

	/**
	 * 添加记录
	 * @param tableName	 表名字
	 * @param rowkey 	 行关键字
	 * @param family  	 列族
	 * @param qualifier 列名
	 * @param value		值
	 * @throws IOException
	 */
	public void insertRecord(String tableName, String rowkey, String family,
			String qualifier, String value) throws IOException {

		HTable table = new HTable(this.conf, tableName);
		Put put = new Put(rowkey.getBytes());
		put.add(family.getBytes(), qualifier.getBytes(), value.getBytes());
		table.put(put);

		System.out.println("insert recored " + rowkey + " to table "
				+ tableName + " ok.");
		table.close();
	}

	/**
	 * 添加一组记录
	 * @param tableName
	 * @param putList
	 * @throws IOException
	 */
	public void insertRecordList(String tableName, List<Put> putList)
			throws IOException {
		HTable table = new HTable(this.conf, tableName);

		for (int i = 0; i < putList.size(); i++) {
			Put put = putList.get(i);
			table.put(put);
		}
		putList.clear();
		table.close();
	}

	/**
	 * 删除一行数据
	 * @param tableName	表名
	 * @param rowkey	行关键字
	 * @throws IOException
	 */
	public void deleteRecord(String tableName, String rowkey)
			throws IOException {

		HTable table = new HTable(this.conf, tableName);
		Delete del = new Delete(rowkey.getBytes());
		table.delete(del);
		System.out.println("del recored " + rowkey + " ok.");
		table.close();
	}

	/**
	 * 获取一条记录
	 * @param tableName	表名
	 * @param rowkey	行关键字
	 * @return
	 * @throws IOException
	 */
	public Result getOneRecord(String tableName, String rowkey)
			throws IOException {
		HTable table = new HTable(this.conf, tableName);
		Get get = new Get(rowkey.getBytes());		//get对象
		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()));
		}
		table.close();
		return rs;
	}

	/**
	 * 获取一张表的所有数据
	 * @param tableName
	 * @return
	 * @throws IOException
	 */
	public List<Result> getAllRecord(String tableName) throws IOException {

		HTable table = new HTable(this.conf, tableName);
		Scan scan = new Scan();			//Scan对象
		ResultScanner scanner = table.getScanner(scan);
		List<Result> list = new ArrayList<Result>();
		for (Result r : scanner) {
			list.add(r);
		}
		scanner.close();
		table.close();

		for (Result r : scanner) {
			for (KeyValue kv : r.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()));
			}
		}
		return list;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值