HBase增删改查客户端

需要讲解的内容基本都写在代码里了,直接贴代码

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.util.Bytes;
import org.apache.log4j.Logger;

import com.css.service.DtsService;

/**
 * 数据传输服务客户端
 * @version DTS 1.0
 * @time 2018-11-14
 * @author 一朝风月
 */
public class DtsClient implements DtsService {
	// Logger对象
	private Logger log = Logger.getLogger(DtsClient.class);
	// 连接配置
	private static final Configuration config = HBaseConfiguration.create();
	// 设置日期格式
	private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 默认构造器--无参构造器
	 */
	public DtsClient() {
		super();
	}

	/**
	 * 初始化连接配置
	 * @param quorum 集群成员列表
	 * @param zkPort ZooKeeper对外提供服务端口
	 * @param master 集群主机名
	 */
	public DtsClient(String quorum, String zkPort, String master){
		config.set("hbase.zookeeper.quorum",quorum);
		config.set("hbase.zookeeper.property.clientPort", zkPort);
		config.set("hbase.master", master);
	}

	/**
	 * 创建表
	 * @param tableName 表名称
	 * @param families 列族
	 */
	@Override
	public boolean createTable(String tableName, String[] families) {
		boolean result = false;
		try{
			HBaseAdmin admin = new HBaseAdmin(config);
			HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
			for(int i = 0; i < families.length; i++){
				tableDescriptor.addFamily(new HColumnDescriptor(families[i]));
			}
			log.info(df.format(new Date()) + "--" + "开始创建" + tableName);
			admin.createTable(tableDescriptor);
			log.info(df.format(new Date()) + "--" + tableName+"表创建完成!");
			result = true;
			admin.close();
		} catch (MasterNotRunningException e1){
			log.error(Thread.currentThread().getName() + "master主机没有运行", e1);
		} catch (ZooKeeperConnectionException e2){
			log.error(Thread.currentThread().getName() + "zookeeper没有连接", e2);
		} catch (IOException e3){
			log.error(Thread.currentThread().getName() + "IOExcetpion读写异常", e3);
		}
		return result;
	}

	/**
	 * 删除表
	 * @param tableName 表名称
	 */
	@Override
	public boolean deleteTable(String tableName) {
		boolean result = false;

		try {
			HBaseAdmin admin = new HBaseAdmin(config);
			log.info(df.format(new Date()) + "--" + tableName + "被禁用");
			admin.disableTable(tableName);
			log.info(df.format(new Date()) + "--" + tableName + "被删除");
			admin.deleteTable(tableName);
			result = true;
			admin.close();
		} catch (IOException e) {
			log.error(Thread.currentThread().getName() + "--" +df.format(new Date()) + "--" + "IO读写失败", e);
		}
		return result;
	}

	@Override
	public boolean modifyTable() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 查询某一属性的值
	 * @param tableName 表名称
	 * @param rowNo 行号
	 * @param family 列名
	 * @param property 属性名
	 */
	@Override
	public String select(String tableName, String rowNo, String family, String property) {
		String resultValue = "";
		// 批量查询效率值得商榷
		try{
			HTable hTable = new HTable(config, tableName);
			Get g = new Get(Bytes.toBytes(rowNo));
			Result result = hTable.get(g);
			byte [] value = result.getValue(Bytes.toBytes(family),Bytes.toBytes(property));
			resultValue = Bytes.toString(value);
			hTable.close();
		} catch (IOException e){
			log.error("IO读写异常");
			log.error(Thread.currentThread().getName(), e);
		}
		return resultValue;
	}
	
	/**
	 * 插入一条记录
	 * @param tableName 表名称
	 * @param rowNo 行号
	 * @param family 列名
	 * @param property 属性名
	 * @param value 属性值
	 */
	@Override
	public boolean insert(String tableName, String rowNo, String family, String property, String value) {
		boolean result = false;
		try{
			HTable hTable = new HTable(config, tableName);
			Put p = new Put(Bytes.toBytes(rowNo)); 
			p.add(Bytes.toBytes(family),
					Bytes.toBytes(property),Bytes.toBytes(value));
			hTable.put(p);
			log.info(df.format(new Date()) + "--" +"插入数据完成!");
			result = true;
			hTable.close();
		} catch (IOException e) {
			log.error("IO读写异常");
			log.error(Thread.currentThread().getName(), e);
		}
		return result;
	}
	
	/**
	 * 更新一条记录
	 * @param tableName 表名称
	 * @param rowNo 行号
	 * @param family 列名
	 * @param property 属性名
	 * @param value 属性值
	 */
	@Override
	public boolean update(String tableName, String rowNo, String family, String property, String value) {

		boolean result = false;
		try{
			HTable hTable = new HTable(config, tableName);
			Put p = new Put(Bytes.toBytes(rowNo)); 
			p.add(Bytes.toBytes(family),
					Bytes.toBytes(property),Bytes.toBytes(value));
			hTable.put(p);
			log.info(df.format(new Date()) + "--" +"更新数据完成!");
			result = true;
			hTable.close();
		} catch (IOException e) {
			log.error("IO读写异常");
			log.error(Thread.currentThread().getName(), e);
		}
		return result;
	}
	
	/**
	 * 删除某条记录
	 * @param tableName 表名称
	 * @param rowNo 行号
	 * @param family 列名
	 * @param property 属性
	 */
	@Override
	public boolean delete(String tableName, String rowNo, String family, String property) {
		boolean result = false;
		try{
			HTable table = new HTable(config, tableName);
			Delete delete = new Delete(Bytes.toBytes(rowNo));
			delete.deleteColumn(Bytes.toBytes(family), Bytes.toBytes(property));
			table.delete(delete);
			log.info("删除某条记录");
			table.close();
		} catch (IOException e) {
			log.error("IO读写异常");
			log.error(Thread.currentThread().getName(), e);
		}
		return result;
	}
	
	/**
	 * 测试方法
	 * @param args
	 */
	public static void main(String[] args) {
		String quorum = "master,slave1,slave2";
		String zkPort = "2181";
		String master = "192.168.225.100:16000";
		DtsClient dc = new DtsClient(quorum, zkPort, master);
		String[] families = {"teacher", "student"};
		dc.createTable("school", families);
		dc.insert("school", "row1", "teacher", "name", "zhangsanfeng");
		dc.insert("school", "row1", "teacher", "age", "30");
		dc.insert("school", "row1", "student", "name", "songyuanqiao");
		dc.insert("school", "row1", "student", "age", "18");
		
		String name = dc.select("school", "row1", "student", "name");
		String age = dc.select("school", "row1", "student", "age");
		
		System.out.println(name + ": " + age);
		
		dc.update("school", "row1", "student", "name", "moshenggu");
		name = dc.select("school", "row1", "student", "name");
		age = dc.select("school", "row1", "student", "age");
		
		System.out.println(name + ": " + age);
		
		dc.delete("school", "row1", "student", "name");
		dc.delete("school", "row1", "student", "age");

		dc.deleteTable("school");
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_37624828/article/details/84070207