【Hbase】Java调用Hbase接口实现数据库操作

继上一篇文章

我们首先来替换下hbase的lib目录下hadoop相关的jar包,换成2.6的

在eclipse的java project中,直接引入hbase的lib下的所有jar

省的到时候这个类找不到,那个类找不到的,不然你就一个一个的jar去添加,尝试,找个最小集

下面看下java代码的实现吧

HBaseUtil

package hbase;

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

	private Configuration conf;

	private HBaseAdmin admin;

	public HBaseUtil() throws IOException {
		Configuration cnf = new Configuration();
		this.conf = HBaseConfiguration.create(cnf);
		this.admin = new HBaseAdmin(this.conf);
	}

	public HBaseUtil(Configuration conf) throws IOException {
		this.conf = HBaseConfiguration.create(conf);
		this.admin = new HBaseAdmin(this.conf);
	}

	public void createTable(String tableName, String columnFamily[]) {
		try {
			if (this.admin.tableExists(tableName)) {
				System.out
						.println("Table : " + tableName + " already exists !");
			} else {
				HTableDescriptor td = new HTableDescriptor(tableName);
				int len = columnFamily.length;
				for (int i = 0; i < len; i++) {
					HColumnDescriptor family = new HColumnDescriptor(
							columnFamily[i]);
					td.addFamily(family);
				}
				admin.createTable(td);
				System.out.println(tableName + " 表创建成功!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(tableName + " 表创建失败!");
		}
	}

	public void deleteTable(String tableName) {
		try {
			if (this.admin.tableExists(tableName)) {
                                admin.disableTable(tableName);
				admin.deleteTable(tableName);
				System.out.println(tableName + " 表删除成功!");
			} else {
				System.out.println(tableName + " 表不存在!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(tableName + " 表删除失败!");
		}
	}

	public void insertRecord(String tableName, String rowKey,
			String columnFamily, String qualifier, String value) {
		try {
			HTable table = new HTable(this.conf, tableName);
			Put put = new Put(rowKey.getBytes());
			put.add(columnFamily.getBytes(), qualifier.getBytes(),
					value.getBytes());
			table.put(put);
			System.out.println(tableName + " 表插入数据成功!");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(tableName + " 表插入数据失败!");
		}
	}

	public void deleteRecord(String tableName, String rowKey) {
		try {
			HTable table = new HTable(this.conf, tableName);
			Delete del = new Delete(rowKey.getBytes());
			table.delete(del);
			System.out.println(tableName + " 表删除数据成功!");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(tableName + " 表删除数据失败!");
		}
	}

	public Result getOneRecord(String tableName, String rowKey) {
		try {
			HTable table = new HTable(this.conf, tableName);
			Get get = new Get(rowKey.getBytes());
			Result rs = table.get(get);
			System.out.println(tableName + " 表获取数据成功!");
			return rs;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

	}

	public List<Result> getAllRecords(String tableName) {
		try {
			HTable table = new HTable(this.conf, tableName);
			Scan scan = new Scan();
			ResultScanner scanner = table.getScanner(scan);
			List<Result> list = new ArrayList<Result>();
			for (Result r : scanner) {
				list.add(r);
			}
			scanner.close();
			System.out.println(tableName + " 表获取所有记录成功!");
			return list;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}
以上工具类是包含了所有的表的操作,下面再来看一个测试类

HBaseDemo

package hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;




public class HbaseDemo {

	public static void main(String[] args) {
		Configuration config = new Configuration();
		config.set("hbase.zookeeper.quorum", "com23.authentication,com22.authentication,com21.authentication");
		config.set("hbase.zookeeper.property.clientPort", "2181");
		//config.set("hbase.master", "192.168.11.179:6000");

		try {
			HBaseUtil  hbase = new HBaseUtil(config);
			String tableName = "flume";
			String[] columnFamily = {"chiwei","lining","taotao"};
			hbase.createTable(tableName, columnFamily);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

大家自行去测试吧,下面说下代码原理

HBaseConfiguration

对hbase进行配置

Configuration cnf = new Configuration();
this.conf = HBaseConfiguration.create(cnf);
this.admin = new HBaseAdmin(this.conf);

Configuration config = new Configuration();
config.set("hbase.zookeeper.quorum", "com23.authentication,com22.authentication,com21.authentication");
config.set("hbase.zookeeper.property.clientPort", "2181");
   

HBaseAdmin

管理Hbase数据库表信息的接口

返回值 函数 描述
      void addColumn(String tableName, HColumnDescriptor column) 向一个已经存在的表添加咧
checkHBaseAvailable(HBaseConfiguration conf) 静态函数,查看HBase是否处于运行状态
createTable(HTableDescriptor desc) 创建一个表,同步操作
deleteTable(byte[] tableName) 删除一个已经存在的表
enableTable(byte[] tableName) 使表处于有效状态
disableTable(byte[] tableName) 使表处于无效状态
HTableDescriptor[] listTables() 列出所有用户控件表项
void modifyTable(byte[] tableName, HTableDescriptor htd) 修改表的模式,是异步的操作,可能需要花费一定的时间
boolean tableExists(String tableName) 检查表是否存在


1、创建表

public void createTable(String tableName, String columnFamily[]) {
		try {
			if (this.admin.tableExists(tableName)) {
				System.out
						.println("Table : " + tableName + " already exists !");
			} else {
				HTableDescriptor td = new HTableDescriptor(tableName);
				int len = columnFamily.length;
				for (int i = 0; i < len; i++) {
					HColumnDescriptor family = new HColumnDescriptor(
							columnFamily[i]);
					td.addFamily(family);
				}
				admin.createTable(td);
				System.out.println(tableName + " 表创建成功!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(tableName + " 表创建失败!");
		}
	}
HTableDescriptor表的名字及其表的列族

返回值 函数 描述
void addFamily(HColumnDescriptor) 添加一个列族
HColumnDescriptor removeFamily(byte[] column) 移除一个列族
byte[] getName() 获取表的名字
byte[] getValue(byte[] key) 获取属性的值
void setValue(String key, String value) 设置属性的值
1、判断表是否存在

2、实例化表的描述符

3、依次添加列族

4、创建表


2、删除表

public void deleteTable(String tableName) {
		try {
			if (this.admin.tableExists(tableName)) {
				admin.deleteTable(tableName);
				System.out.println(tableName + " 表删除成功!");
			} else {
				System.out.println(tableName + " 表不存在!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(tableName + " 表删除失败!");
		}
	}
通过HBaseAdmin接口来删除表

3、插入一行记录

public void insertRecord(String tableName, String rowKey,
			String columnFamily, String qualifier, String value) {
		try {
			HTable table = new HTable(this.conf, tableName);
			Put put = new Put(rowKey.getBytes());
			put.add(columnFamily.getBytes(), qualifier.getBytes(),
					value.getBytes());
			table.put(put);
			System.out.println(tableName + " 表插入数据成功!");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(tableName + " 表插入数据失败!");
		}
	}
实例化一个put对象,rowkey按照是每行的一个关键字,相当于主键,然后put添加列族和列修饰符以及列内容,最后调用table的put方法

4、查询一条记录

public Result getOneRecord(String tableName, String rowKey) {
		try {
			HTable table = new HTable(this.conf, tableName);
			Get get = new Get(rowKey.getBytes());
			Result rs = table.get(get);
			System.out.println(tableName + " 表获取数据成功!");
			return rs;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
查询需要实例化Get对象,通过rowkey主键来查询

Result rs = hbase.getOneRecord("flume", "love");
			for (Cell cell : rs.rawCells()) {
				System.out.println(new String(cell.getRow()) + " "
						+ new String(cell.getFamily()) + " "
						+ new String(cell.getQualifier()) + " "
						+ new String(cell.getValue()));
			}

5、删除一条记录

public void deleteRecord(String tableName, String rowKey) {
		try {
			HTable table = new HTable(this.conf, tableName);
			Delete del = new Delete(rowKey.getBytes());
			table.delete(del);
			System.out.println(tableName + " 表删除数据成功!");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(tableName + " 表删除数据失败!");
		}
	}
实例化Delete对象,通过rowkey删除

6、查询所有记录

public List<Result> getAllRecords(String tableName) {
		try {
			HTable table = new HTable(this.conf, tableName);
			Scan scan = new Scan();
			ResultScanner scanner = table.getScanner(scan);
			List<Result> list = new ArrayList<Result>();
			for (Result r : scanner) {
				list.add(r);
			}
			scanner.close();
			System.out.println(tableName + " 表获取所有记录成功!");
			return list;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
通过实例化Scanner对象,逐条添加到list集合中
List<Result> list = hbase.getAllRecords("flume");
			Iterator<Result> it = list.iterator();
			while(it.hasNext()) {
				Result rs = it.next();
				for (Cell cell : rs.rawCells()) {
					System.out.println(new String(cell.getRow()) + " "
							+ new String(cell.getFamily()) + " "
							+ new String(cell.getQualifier()) + " "
							+ new String(cell.getValue()));
				}
			}



你可能感兴趣的:(【Hbase】Java调用Hbase接口实现数据库操作)