HBase的java操作

package com.ccse.hbase;

import java.io.IOException;

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

	//声明静态配置  
	static Configuration cfg = HBaseConfiguration.create();
	static {
		cfg.setStrings("hbase.zookeeper.quorum", "chaoren1");
	}
	
	//创建一张表,通过HBaseAdmin HTableDescriptor来创建
	public static void create(String tableName, String... columnFamilies) throws IOException {
		//管理HBase的,主要负责DLL的操作
		HBaseAdmin admin = new HBaseAdmin(cfg);
		if (admin.tableExists(tableName)) {
			System.out.println("Table Exists");
			System.exit(0);
		} else {
			HTableDescriptor tableDesc = new HTableDescriptor(tableName);
			for (String columnFamily : columnFamilies) {
				tableDesc.addFamily(new HColumnDescriptor(columnFamily));
			}
			admin.createTable(tableDesc);
			System.out.println("create table success!");
			System.out.println("表是否存在:" + admin.tableExists(tableName));
			admin.close();
		}
	}
	
	//添加一条数据,通过HTable Put为已经存在的表添加数据
	public static void put(String tableName, String row, String columnFamily, String column, String data) throws IOException {
		//管理表中数据的,主要负责DML操作
		HTable table = new HTable(cfg, tableName);
		Put put1 = new Put(Bytes.toBytes(row));
		put1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
		table.put(put1);
		System.out.println("put '" + row + "','" + columnFamily + ":" + column + "','" + data + "'");
		table.close();
	}
	
	public static void get(String tableName, String row) throws IOException {
		HTable table = new HTable(cfg, tableName);
		Get get = new Get(Bytes.toBytes(row));
		Result result = table.get(get);
		System.out.println("Get:" + result);
		System.out.println("result:" + Bytes.toString(result.getValue(Bytes.toBytes("row1"), 
				Bytes.toBytes("cl1"))));
		table.close();
	}
	
	//显示所有数据,通过HTable Scan来获取已有表的信息
	public static void scan(String tableName) throws IOException {
		HTable table = new HTable(cfg, tableName);
		Scan scan = new Scan();
		ResultScanner rs = table.getScanner(scan);
		for (Result r : rs) {
			System.out.println(r);
		}
	}
	
	//删除表
	public static boolean delete(String tableName) throws IOException {
		HBaseAdmin admin = new HBaseAdmin(cfg);
		if (admin.tableExists(tableName)) {
			try {
				admin.disableTable(tableName);
				admin.deleteTable(tableName);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		admin.close();
		return true;
	}
	
	public static void main(String[] args) {
		String tableName = "hbase_tb";
		String columnFamily = "cf";
		try {
			HBaseTest.create(tableName, columnFamily);
			HBaseTest.put(tableName, "row1", columnFamily, "cl1", "data");
			HBaseTest.get(tableName, "row1");
			HBaseTest.scan(tableName);
			if (HBaseTest.delete(tableName)) {
				System.out.println("Delete table " + tableName + " success!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波哥的技术积累

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值