HBase的Java接口基本操作:创建表插入查询删除

package captain.hbase;

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.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.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;

/**
 * HBase的Java接口基本操作演示。
 * */
public class HBaseDemo {

	public static void main(String[] args) throws Exception {
		//得到一个HBase的配置对象。
		Configuration conf = HBaseConfiguration.create();
		//配置zookeeper。这是伪分布式HBase下的配置。zookeeper所在的主机。
		conf.set("hbase.zookeeper.quorum", "hadoop");
		
		//创建一个HBase表。
		createHBaseTable(conf);
		//向HBase表中插入一行记录。
		putOneRecord(conf);
		//向HBase表中插入多行记录。
		putSomeRecords(conf);
		//查询HBase表中的一行记录。
		getOneRecord(conf);
		//查询HBase表中的多行记录。
		getSomeRecords(conf);
		//删除HBase表中的一行记录。
		deleteOneRecord(conf);
	}
	private static void deleteOneRecord(Configuration conf) throws Exception {
		//创建HBase表对象。
		HTable table = new HTable(conf, "people");
		
		//创建一个Delete对象,指定需要删除的记录对应的行键。
		Delete delete = new Delete(Bytes.toBytes("rk999"));
		//从HBase表中删除一行记录。
		table.delete(delete);
		
		//关闭HBase表。
		table.close();
	}
	private static void getSomeRecords(Configuration conf) throws Exception {
		//创建HBase表对象。
		HTable table = new HTable(conf, "people");
		
		//创建Scan对象,指定startRowKey和StopRowKey(注意是按字典顺序排列)。
		Scan scan = new Scan(Bytes.toBytes("rk29"), Bytes.toBytes("rk30"));
		//从HBase表中查询多行记录,返回一个迭代器ResultScanner。
		ResultScanner rs = table.getScanner(scan);
		
		//遍历迭代器ResultScanner
		for(Result result : rs){
			String value = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
			System.out.println(value);
		}
		
		//关闭HBase表。
		table.close();
	}
	private static void getOneRecord(Configuration conf) throws Exception {
		//创建HBase表对象。
		HTable table = new HTable(conf, "people");
		
		//创建一个Get对象,使用行键初始化。
		Get get = new Get(Bytes.toBytes("rk1000"));
		//查询指定行键对应的一行记录。
		Result result = table.get(get);
		
		//得到该行记录中指定列簇和列标识符的值value。
		String value = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
		System.out.println(value);
		
		//关闭HBase表。
		table.close();
	}
	private static void putSomeRecords(Configuration conf) throws Exception {
		// 创建HBase表对象。
		HTable table = new HTable(conf, "people");
		
		//创建一个长度为100的list集合存放Put对象。
		List<Put> puts = new ArrayList<Put>(100);
		
		for(int i = 1; i <= 1002; i++){
			//创建一个Put对象,使用行键初始化。
			Put put = new Put(Bytes.toBytes("rk" + i));
			//将列簇、列标识符和值添加到该行键对应的记录中。
			put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("captain"));
			put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("25"));
			put.add(Bytes.toBytes("info"), Bytes.toBytes("gender"), Bytes.toBytes("male"));
			put.add(Bytes.toBytes("data"), Bytes.toBytes("love"), Bytes.toBytes("yes"));
			//将put对象,即一行记录,添加到list集合中。
			puts.add(put);
			if(i % 100 == 0){
				table.put(puts);
				puts = new ArrayList<Put>(100);
			}
		}
	
		//向HBase表中插入多行记录。
		table.put(puts);
		//关闭HBase表对象。
		table.close();
		
	}
	private static void putOneRecord(Configuration conf) throws Exception {
		//创建HBase表对象。
		HTable table = new HTable(conf, "people");
		
		//创建一个Put对象,使用行键初始化。
		Put put = new Put(Bytes.toBytes("rk0001"));
		//将列簇、列标识符和值添加到该行键对应的记录中。
		put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("captain"));
		put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("25"));
		put.add(Bytes.toBytes("info"), Bytes.toBytes("gender"), Bytes.toBytes("male"));
		put.add(Bytes.toBytes("data"), Bytes.toBytes("love"), Bytes.toBytes("yes"));
		
		//向HBase表中插入一行记录。
		table.put(put);
		//关闭HBase表对象。
		table.close();
		
	}
	private static void createHBaseTable(Configuration conf) throws Exception {
		
		//创建一个HBase的客户端对象。
		HBaseAdmin admin = new HBaseAdmin(conf);
		
		//创建一个HBase表结构的描述对象。
		HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("people"));
		
		//创建一个HBase表列簇的描述对象。
		HColumnDescriptor hcd_info = new HColumnDescriptor("info");
		//通过列簇的描述对象设置该列簇的最大版本数,默认均为1。
		hcd_info.setMaxVersions(3);
		//创建一个HBase表列簇的描述对象。
		HColumnDescriptor hcd_data = new HColumnDescriptor("data");
		
		//通过表结构的描述对象将创建的列簇添加到该表结构描述对象中。
		htd.addFamily(hcd_info);
		htd.addFamily(hcd_data);
		
		//通过客户端对象创建HBase表。
		admin.createTable(htd);
		//关闭客户端对象。
		admin.close();
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值