通过Java 操作Hbase
一、版本:
hbase:
二、操作Hbase:
1、首先定义几个用的到的全局变量:
HBaseAdmin :主要对于表的一些设置操作
HBaseAdmin hBaseAdmin;
HTable:主要涉及到对标内容的操作
HTable hTable;
TN:测试的表名
String TN = "lillcol";
2、创建begin()方法初始化资源:
//初始化Configuration hBaseAdmin hTable
public void begin() throws Exception {
Configuration conf = new Configuration();
// conf.set("hbase.zookeeper.quorum", "node1.noce2.noce3");
hBaseAdmin = new HBaseAdmin(conf);
hTable = new HTable(conf, TN);
}
3、创建end()方法关闭资源:
//关闭hBaseAdmin hTable 资源 注意先判断是否为null
public void end() {
if (hBaseAdmin != null) {
try {
hBaseAdmin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (hTable != null) {
try {
hTable.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4、创建一个Table
注:不同版本可能会有不一样的方法,此方法适合0.9的,至于1.0是否能用后面会尝试
//创建一个新的Table
public void createTable() throws Exception {
//初始化
begin();
//首先判断表存不存在 存在删除
if (hBaseAdmin.tableExists(TN)) {
hBaseAdmin.disableTable(TN);
hBaseAdmin.deleteTable(TN);
}
//1.2以后的版本 好像不用 HTableDescriptor了
//过段时间在更新1.2以后的版本
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
//创建列族
HColumnDescriptor family1 = new HColumnDescriptor("column1");
//对表的一些设置
family1.setBlockCacheEnabled(true);
family1.setInMemory(true);
family1.setMaxVersions(2);
//添加列族
desc.addFamily(family1);
hBaseAdmin.createTable(desc);
System.out.println("create table success");
//关闭资源
end();
}
5、模拟插入数据(参考了尚学堂的学习资料 模拟数据)
// 获得手机号
public String getPhoneNum(String prefix) {
return prefix + String.format("%8d", r.nextInt(999999999));
}
//获得日期 public String getDate(String year) {
return year+ String.format( "%02d%02d%02d%02d%02d", new Object[] { r.nextInt(12) + 1, r.nextInt(29) + 1, r.nextInt(60), r.nextInt(60), r.nextInt(60) }); }
public void insertDB() throws Exception {
begin();
// 创建一个List缓存
Listputs = new ArrayList();
//为一个用户生成十条模拟数据
for (int i = 0; i < 10; i++) {
String rowKey;
String phoneNum = getPhoneNum("186");
for (int j = 0; j < 100; j++) {
String phoneData = getDate("2016");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
try {
long dataLong = sdf.parse(phoneData).getTime();
rowKey = phoneNum + (Long.MAX_VALUE - dataLong);
// System.out.println(rowKey);
Put put = new Put(rowKey.getBytes());
put.add("column1".getBytes(), "name".getBytes(), "lillcol".getBytes());
//将入list缓存
puts.add(put);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//批量添加数据
hTable.put(puts);
end();
}
6、查询数据
//查询数据
public void scandDB() throws Exception {
begin();
Scan scan = new Scan();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//设置开始rowKey 和结束rowKey
String startRowKey = "18828394827" + (Long.MAX_VALUE - sdf.parse("2016010100000").getTime());
scan.setStartRow(startRowKey.getBytes());
String stopRowKey = "18828394827" + (Long.MAX_VALUE - sdf.parse("2016020100000").getTime());
scan.setStopRow(stopRowKey.getBytes());
ResultScanner scanner = hTable.getScanner(scan);
for (Result rs : scanner) {
//rs.getColumnLatestCell("colmun1".getBytes(), "name".getBytes())) 获取到的数据需要经过CellUtil.cloneValue在转换成String
System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("colmun1".getBytes(),
"name".getBytes()))));
}
end();
}
7、带过滤器的查询
//具体的方法可以去看官网的文档
//带过滤器的查询
public void scandDB2() throws Exception {
begin();
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
PrefixFilter prefixFilter = new PrefixFilter("18827385967".getBytes());
list.addFilter(prefixFilter);
SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter(
"colmun1".getBytes(), "name".getBytes(), CompareOp.EQUAL, "lillcol".getBytes());
list.addFilter(singleColumnValueExcludeFilter);
Scan scan = new Scan();
scan.setFilter(list);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String startRowKey = "18828394827" + (Long.MAX_VALUE - sdf.parse("2016010100000").getTime());
scan.setStartRow(startRowKey.getBytes());
String stopRowKey = "18828394827" + (Long.MAX_VALUE - sdf.parse("2016020100000").getTime());
scan.setStopRow(stopRowKey.getBytes());
ResultScanner scanner = hTable.getScanner(scan);
for (Result rs : scanner) {
System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("colmun1".getBytes(),
"name".getBytes()))));
}
end();
}
8、获取某条数据
//获取某条数据
public void get() throws Exception {
begin();
// 手机号_时间戳
String rowKey = "133456789755_20170932438765";
Get get = new Get(rowKey.getBytes());
get.addColumn("column1".getBytes(), "name".getBytes());
get.addColumn("column1".getBytes(), "age".getBytes());
Result result = hTable.get(get);
KeyValue columnLatest = result.getColumnLatest("column".getBytes(), "name".getBytes());
System.out.println("----------" + new String(CellUtil.cloneValue(columnLatest)));
end();
}
9、main方法
public static void main(String[] args) throws Exception {
HbaseDemo hbaseDemo = new HbaseDemo();
System.out.println("create table main ");
hbaseDemo.createTable();
System.out.println("insert table main ");
hbaseDemo.insertDB();
}
10、完整源码
package cn.lillcol.sxt;import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import java.util.Random;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.KeyValue;import org.apache.hadoop.hbase.TableName;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.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
public class HbaseDemo {
HBaseAdmin hBaseAdmin;
HTable hTable;
String TN = "lillcol";
Random r = new Random();
// 初始化
Configuration hBaseAdmin hTable
public void begin() throws Exception {
Configuration conf = new Configuration();
//集群跑的时候不设置 本地需要设置
// conf.set("hbase.zookeeper.quorum", "node1.noce2.noce3");
hBaseAdmin = new HBaseAdmin(conf);
hTable = new HTable(conf, TN);
}
// 关闭hBaseAdmin hTable 资源 注意先判断是否为null
public void end() {
if (hBaseAdmin != null) {
try {
hBaseAdmin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (hTable != null) {
try {
hTable.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); } } }
// 创建一个新的Table
public void createTable() throws Exception {
// 初始化
begin();
// 首先判断表存不存在 存在删除
if (hBaseAdmin.tableExists(TN)) {
hBaseAdmin.disableTable(TN);
hBaseAdmin.deleteTable(TN);
}
// 1.2以后的版本 好像不用 HTableDescriptor了
// 过段时间在更新1.2以后的版本
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
// 创建列族
HColumnDescriptor family1 = new HColumnDescriptor("column1");
// 对表的一些设置
family1.setBlockCacheEnabled(true);
family1.setInMemory(true);
family1.setMaxVersions(2);
// 添加列族 1.2版本好像不支持addFamily(方法了)
desc.addFamily(family1);
hBaseAdmin.createTable(desc);
System.out.println("create table success");
// 关闭资源
end(); }
public void insert() throws Exception {
begin();
Listputs = new ArrayList();
// 手机号_时间戳
String rowKey = "133456789755_20170932438765";
Put put = new Put(rowKey.getBytes());
put.add("column1".getBytes(), "name".getBytes(), "lillcol".getBytes()); put.add("column1".getBytes(), "age".getBytes(), "20".getBytes());
puts.add(put);
if (puts.size() > 10) {
hTable.put(puts);
puts.removeAll(puts);
}
end();
}
//获取某条数据
public void get() throws Exception {
begin();
// 手机号_时间戳
String rowKey = "133456789755_20170932438765";
Get get = new Get(rowKey.getBytes());
get.addColumn("column1".getBytes(), "name".getBytes());
get.addColumn("column1".getBytes(), "age".getBytes());
Result result = hTable.get(get);
KeyValue columnLatest = result.getColumnLatest("column".getBytes(), "name".getBytes());
System.out.println("----------" + new String(CellUtil.cloneValue(columnLatest)));
end();
}
// 获得手机号
public String getPhoneNum(String prefix) {
return prefix + String.format("%8d", r.nextInt(999999999));
}
//获得日期
public String getDate(String year) {
return year + String.format( "%02d%02d%02d%02d%02d", new Object[] { r.nextInt(12) + 1, r.nextInt(29) + 1, r.nextInt(60), r.nextInt(60), r.nextInt(60) });
}
public void insertDB() throws Exception {
begin();
// 创建一个List缓存
Listputs = new ArrayList();
//为一个用户生成十条模拟数据
for (int i = 0; i < 10; i++) {
String rowKey;
String phoneNum = getPhoneNum("186");
for (int j = 0; j < 100; j++) {
String phoneData = getDate("2016");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
try {
long dataLong = sdf.parse(phoneData).getTime();
rowKey = phoneNum + (Long.MAX_VALUE - dataLong);
// System.out.println(rowKey);
Put put = new Put(rowKey.getBytes());
put.add("column1".getBytes(), "name".getBytes(), "lillcol".getBytes());
//将入list缓存
puts.add(put);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//批量添加数据
hTable.put(puts);
end();
}
//查询数据
public void scandDB() throws Exception {
begin();
Scan scan = new Scan();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//设置开始rowKey 和结束rowKey
String startRowKey = "18828394827" + (Long.MAX_VALUE - sdf.parse("2016010100000").getTime());
scan.setStartRow(startRowKey.getBytes());
String stopRowKey = "18828394827" + (Long.MAX_VALUE - sdf.parse("2016020100000").getTime());
scan.setStopRow(stopRowKey.getBytes());
ResultScanner scanner = hTable.getScanner(scan);
for (Result rs : scanner) {
//rs.getColumnLatestCell("colmun1".getBytes(), "name".getBytes())) 获取到的数据需要经过CellUtil.cloneValue在转换成String
System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("colmun1".getBytes(),
"name".getBytes()))));
}
end();
}
//带过滤器的查询
public void scandDB2() throws Exception {
begin();
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);
PrefixFilter prefixFilter = new PrefixFilter("18827385967".getBytes());
list.addFilter(prefixFilter);
SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter(
"colmun1".getBytes(), "name".getBytes(), CompareOp.EQUAL, "lillcol".getBytes());
list.addFilter(singleColumnValueExcludeFilter);
Scan scan = new Scan();
scan.setFilter(list);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String startRowKey = "18828394827" + (Long.MAX_VALUE - sdf.parse("2016010100000").getTime());
scan.setStartRow(startRowKey.getBytes());
String stopRowKey = "18828394827" + (Long.MAX_VALUE - sdf.parse("2016020100000").getTime());
scan.setStopRow(stopRowKey.getBytes());
ResultScanner scanner = hTable.getScanner(scan);
for (Result rs : scanner) {
System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("colmun1".getBytes(),
"name".getBytes()))));
}
end();
}
public static void main(String[] args) throws Exception {
HbaseDemo hbaseDemo = new HbaseDemo();
System.out.println("create table main ");
hbaseDemo.createTable();
System.out.println("insert table main ");
hbaseDemo.insertDB();
}
}
11、总结
由于刚接触hbase不久 所以如果文中内容有错误的话 欢迎指出 谢谢
此外 此文主要是本人学习hbase过程中参考尚学堂教程的一些总结!!
网友评论