hbase api 1.0前后版本示例
2016-06-22 09:12
260 查看
HBase api的操作示例,0.98.x版本以后的语法有少许变化,
下面是详细例子
public class HBaseDAO{
static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "rhel124"); //config zookeeper
}
//ddl operation use admin
//before version 1.0
public void ddlBeforeVersionOne(){
HBaseAdmin admin = null;
try{
admin = new HBaseAdmin(conf); //get admin method: new admin
if(admin.tableExists(tableName)){
throw new Exception("table is exist");
}
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
for(int i=0; i<familys.length; i++){
htd.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(htd);
System.out.println("create table success");
}finally{
if(admin != null) admin.close();
}
}
//after version 1.0
public void ddlAfterVersionOne(){
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin(); //get admin method: connection.getAdmin();
try{
if(admin.tableExists(TableName.valueOf(tableName))){
throw new Exception("table is exist");
}
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
for(int i=0; i<familys.length; i++){
htd.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(htd);
System.out.println("create table success");
}finally{
if(admin != null) admin.close();
System.out.println("create table complete, close HBaseAdmin");
}
}
//dml operation use table
//before version 1.0
public static void testHbase()throws Exception{
HTable table = new HTable(conf, "test_hbase_m");
int n = 10000000;
table.setAutoFlush(false);
List<Put> puts = new ArrayList<Put>();
long start = System.currentTimeMillis();
for(int i=1;i<=n;i++){
Put put = new Put(Bytes.toBytes(i+""));
put.add(Bytes.toBytes("0"), Bytes.toBytes("name"), Bytes.toBytes("val"+i));
puts.add(put);
if(i%300000 == 0){
System.out.println(i);
table.put(puts);
table.flushCommits();
System.out.println("insert complete:"+(System.currentTimeMillis()-start)+"ms");
puts.clear();
}
}
//get
Get get = new Get(Bytes.toBytes("1"));
get.addColumn(Bytes.toBytes("0"), Bytes.toBytes("name"));
long start = System.currentTimeMillis();
Result result = table.get(get);
System.out.println("get row:"+(System.currentTimeMillis()-start)+"ms");
System.out.println(JSON.toString(result)+":"+(System.currentTimeMillis()-start)+"ms");
//scan all
Scan scan = new Scan();
ResultScanner rs = null;
start = System.currentTimeMillis();
rs = table.getScanner(scan);
System.out.println("scan all:"+(System.currentTimeMillis()-start)+"ms");
for(Result r:rs){
for(Cell cell:r.rawCells()){
// System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
System.out.println("scan all handle complete:"+(System.currentTimeMillis()-start)+"ms");
//scan filter
RowFilter filter = new RowFilter(CompareOp.EQUAL,
new RegexStringComparator("name"));
scan.setFilter(filter);
start = System.currentTimeMillis();
rs = table.getScanner(scan);
System.out.println("scan filter:"+(System.currentTimeMillis()-start)+"ms");
for(Result r:rs){
for(Cell cell:r.rawCells()){
// System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
System.out.println("scan filter handle complete:"+(System.currentTimeMillis()-start)+"ms");
}
//after version 1.0
public static void dmlAfterVersionOne()throws Exception{
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("test_hbase_m")); //get table method: connection.getTable
BufferedMutator mutator = connection.getBufferedMutator(TableName.valueOf("test_hbase_m")); //like table, use buffer, batch commit
int n = 10000000;
List<Put> puts = new ArrayList<Put>();
long start = System.currentTimeMillis();
for(int i=1;i<=n;i++){
Put put = new Put(Bytes.toBytes(i+""));
put.addColumn(Bytes.toBytes("0"), Bytes.toBytes("name"), Bytes.toBytes("val"+i));
puts.add(put);
if(i%300000 == 0){
System.out.println(i);
// table.put(puts);
mutator.mutate(puts);
mutator.flush();
System.out.println("insert complete:"+(System.currentTimeMillis()-start)+"ms");
puts.clear();
}
}
//get
Get get = new Get(Bytes.toBytes("1"));
get.addColumn(Bytes.toBytes("0"), Bytes.toBytes("name"));
start = System.currentTimeMillis();
Result result = table.get(get);
System.out.println("get row:"+(System.currentTimeMillis()-start)+"ms");
System.out.println(JSON.toString(result)+":"+(System.currentTimeMillis()-start)+"ms");
//scan all
Scan scan = new Scan();
ResultScanner rs = null;
start = System.currentTimeMillis();
rs = table.getScanner(scan);
System.out.println("scan all:"+(System.currentTimeMillis()-start)+"ms");
for(Result r:rs){
for(Cell cell:r.rawCells()){
// System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
4000
System.out.println("scan all handle complete:"+(System.currentTimeMillis()-start)+"ms");
//scan filter
RowFilter filter = new RowFilter(CompareOp.EQUAL,
new RegexStringComparator("name"));
scan.setFilter(filter);
start = System.currentTimeMillis();
rs = table.getScanner(scan);
System.out.println("scan filter:"+(System.currentTimeMillis()-start)+"ms");
for(Result r:rs){
for(Cell cell:r.rawCells()){
// System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
System.out.println("scan filter handle complete:"+(System.currentTimeMillis()-start)+"ms");
}
}
下面是详细例子
public class HBaseDAO{
static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "rhel124"); //config zookeeper
}
//ddl operation use admin
//before version 1.0
public void ddlBeforeVersionOne(){
HBaseAdmin admin = null;
try{
admin = new HBaseAdmin(conf); //get admin method: new admin
if(admin.tableExists(tableName)){
throw new Exception("table is exist");
}
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
for(int i=0; i<familys.length; i++){
htd.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(htd);
System.out.println("create table success");
}finally{
if(admin != null) admin.close();
}
}
//after version 1.0
public void ddlAfterVersionOne(){
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin(); //get admin method: connection.getAdmin();
try{
if(admin.tableExists(TableName.valueOf(tableName))){
throw new Exception("table is exist");
}
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
for(int i=0; i<familys.length; i++){
htd.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(htd);
System.out.println("create table success");
}finally{
if(admin != null) admin.close();
System.out.println("create table complete, close HBaseAdmin");
}
}
//dml operation use table
//before version 1.0
public static void testHbase()throws Exception{
HTable table = new HTable(conf, "test_hbase_m");
int n = 10000000;
table.setAutoFlush(false);
List<Put> puts = new ArrayList<Put>();
long start = System.currentTimeMillis();
for(int i=1;i<=n;i++){
Put put = new Put(Bytes.toBytes(i+""));
put.add(Bytes.toBytes("0"), Bytes.toBytes("name"), Bytes.toBytes("val"+i));
puts.add(put);
if(i%300000 == 0){
System.out.println(i);
table.put(puts);
table.flushCommits();
System.out.println("insert complete:"+(System.currentTimeMillis()-start)+"ms");
puts.clear();
}
}
//get
Get get = new Get(Bytes.toBytes("1"));
get.addColumn(Bytes.toBytes("0"), Bytes.toBytes("name"));
long start = System.currentTimeMillis();
Result result = table.get(get);
System.out.println("get row:"+(System.currentTimeMillis()-start)+"ms");
System.out.println(JSON.toString(result)+":"+(System.currentTimeMillis()-start)+"ms");
//scan all
Scan scan = new Scan();
ResultScanner rs = null;
start = System.currentTimeMillis();
rs = table.getScanner(scan);
System.out.println("scan all:"+(System.currentTimeMillis()-start)+"ms");
for(Result r:rs){
for(Cell cell:r.rawCells()){
// System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
System.out.println("scan all handle complete:"+(System.currentTimeMillis()-start)+"ms");
//scan filter
RowFilter filter = new RowFilter(CompareOp.EQUAL,
new RegexStringComparator("name"));
scan.setFilter(filter);
start = System.currentTimeMillis();
rs = table.getScanner(scan);
System.out.println("scan filter:"+(System.currentTimeMillis()-start)+"ms");
for(Result r:rs){
for(Cell cell:r.rawCells()){
// System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
System.out.println("scan filter handle complete:"+(System.currentTimeMillis()-start)+"ms");
}
//after version 1.0
public static void dmlAfterVersionOne()throws Exception{
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("test_hbase_m")); //get table method: connection.getTable
BufferedMutator mutator = connection.getBufferedMutator(TableName.valueOf("test_hbase_m")); //like table, use buffer, batch commit
int n = 10000000;
List<Put> puts = new ArrayList<Put>();
long start = System.currentTimeMillis();
for(int i=1;i<=n;i++){
Put put = new Put(Bytes.toBytes(i+""));
put.addColumn(Bytes.toBytes("0"), Bytes.toBytes("name"), Bytes.toBytes("val"+i));
puts.add(put);
if(i%300000 == 0){
System.out.println(i);
// table.put(puts);
mutator.mutate(puts);
mutator.flush();
System.out.println("insert complete:"+(System.currentTimeMillis()-start)+"ms");
puts.clear();
}
}
//get
Get get = new Get(Bytes.toBytes("1"));
get.addColumn(Bytes.toBytes("0"), Bytes.toBytes("name"));
start = System.currentTimeMillis();
Result result = table.get(get);
System.out.println("get row:"+(System.currentTimeMillis()-start)+"ms");
System.out.println(JSON.toString(result)+":"+(System.currentTimeMillis()-start)+"ms");
//scan all
Scan scan = new Scan();
ResultScanner rs = null;
start = System.currentTimeMillis();
rs = table.getScanner(scan);
System.out.println("scan all:"+(System.currentTimeMillis()-start)+"ms");
for(Result r:rs){
for(Cell cell:r.rawCells()){
// System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
4000
System.out.println("scan all handle complete:"+(System.currentTimeMillis()-start)+"ms");
//scan filter
RowFilter filter = new RowFilter(CompareOp.EQUAL,
new RegexStringComparator("name"));
scan.setFilter(filter);
start = System.currentTimeMillis();
rs = table.getScanner(scan);
System.out.println("scan filter:"+(System.currentTimeMillis()-start)+"ms");
for(Result r:rs){
for(Cell cell:r.rawCells()){
// System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
System.out.println("scan filter handle complete:"+(System.currentTimeMillis()-start)+"ms");
}
}
相关文章推荐
- Facebook's New Real-time Messaging System: HBase to Store 135+ Billion Messages a Month
- Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
- 基于HBase Thrift接口的一些使用问题及相关注意事项的详解
- 如何解决struts2日期类型转换
- Eclipse中查看android工程代码出现"android.jar has no source attachment"的解决方案
- 基于Java实现杨辉三角 LeetCode Pascal's Triangle
- hbase shell基础和常用命令详解
- 手把手教你配置Hbase完全分布式环境
- 实战:在Java Web 项目中使用HBase
- HBase RowKey设计的那些事
- Spark中将对象序列化存储到hdfs
- HBase基本原理
- HBase中的基本概念
- 【原创】基于分布式存储的开源系统在实时数据库海量历史数据存储项目上的预研
- HBase0.96.x开发使用(一)--安装
- 基于外部ZooKeeper的GlusterFS作为分布式文件系统的完全分布式HBase集群安装指南
- 基于solr实现hbase的二级索引
- HBase伪分布式安装