hbase 0.98 java_在集群中Java 通过调用API操作HBase 0.98

package com.wan.hbase;

import java.io.IOException;

import org.apache.Hadoop.conf.Configuration;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.CellUtil;

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.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.util.Bytes;

public class SimpleHBase {

public static void main(String[] args) {

Configuration

configuration=HBaseConfiguration.create();

String

tableName="student";

createTable(configuration,

tableName);

// addData(configuration,

tableName);

// getData(configuration,

tableName);

// getAllData(configuration,

tableName);

// deleteDate(configuration,

tableName);

// dropTable(configuration,

tableName);

}

public static void createTable(Configuration configuration,String

tableName){

HBaseAdmin admin;

try {

admin = new

HBaseAdmin(configuration);

if(admin.tableExists(tableName)){

admin.disableTable(tableName);

admin.deleteTable(tableName);

System.out.println(tableName+"is exist ,delete ......");

}

HTableDescriptor

tableDescriptor=new

HTableDescriptor(TableName.valueOf(tableName));

tableDescriptor.addFamily(new

HColumnDescriptor("info"));

tableDescriptor.addFamily(new

HColumnDescriptor("address"));

admin.createTable(tableDescriptor);

System.out.println("end create

table");

} catch

(MasterNotRunningException e) {

// TODO Auto-generated catch

block

e.printStackTrace();

} catch

(ZooKeeperConnectionException e) {

// TODO Auto-generated catch

block

e.printStackTrace();

} catch (IOException e)

{

// TODO Auto-generated catch

block

e.printStackTrace();

}

}

public static void dropTable(Configuration configuration,String

tableName){

HBaseAdmin admin;

try {

admin = new

HBaseAdmin(configuration);

if(admin.tableExists(tableName)){

admin.disableTable(tableName);

admin.deleteTable(tableName);

System.out.println(tableName+"delete success!");

}else{

System.out.println(tableName+"Table does not exist!");

}

} catch

(MasterNotRunningException e) {

// TODO Auto-generated catch

block

e.printStackTrace();

} catch

(ZooKeeperConnectionException e) {

// TODO Auto-generated catch

block

e.printStackTrace();

} catch (IOException e)

{

// TODO Auto-generated catch

block

e.printStackTrace();

}

}

public static void addData(Configuration configuration,String

tableName){

HBaseAdmin admin;

try {

admin = new

HBaseAdmin(configuration);

if(admin.tableExists(tableName)){

HTable table=new

HTable(configuration, tableName);

Put put=new

Put(Bytes.toBytes("zhangsan"));

put.add(Bytes.toBytes("info"), Bytes.toBytes("age"),

Bytes.toBytes("28"));

table.put(put);

System.out.println("add

success!");

}else{

System.out.println(tableName+"Table does not exist!");

}

} catch

(MasterNotRunningException e) {

// TODO Auto-generated catch

block

e.printStackTrace();

} catch

(ZooKeeperConnectionException e) {

// TODO Auto-generated catch

block

e.printStackTrace();

} catch (IOException e)

{

// TODO Auto-generated catch

block

e.printStackTrace();

}

}

public static void deleteDate(Configuration configuration,String

tableName){

HBaseAdmin admin;

try {

admin=new

HBaseAdmin(configuration);

if(admin.tableExists(tableName)){

HTable table=new

HTable(configuration, tableName);

Delete delete=new

Delete(Bytes.toBytes("zhangsan"));

table.delete(delete);

System.out.println("delete

success!");

}else{

System.out.println("Table

does not exist!");

}

} catch

(MasterNotRunningException e) {

// TODO Auto-generated catch

block

e.printStackTrace();

} catch

(ZooKeeperConnectionException e) {

// TODO Auto-generated catch

block

e.printStackTrace();

} catch (IOException e)

{

// TODO Auto-generated catch

block

e.printStackTrace();

}

}

public static void getData(Configuration configuration,String

tableName){

HTable table;

try {

table = new

HTable(configuration, tableName);

Get get=new

Get(Bytes.toBytes("zhangsan"));

Result

result=table.get(get);

for(Cell

cell:result.rawCells()){

System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+"

");

System.out.println("Timetamp:"+cell.getTimestamp()+" ");

System.out.println("column

Family:"+new String(CellUtil.cloneFamily(cell))+" ");

System.out.println("row

Name:"+new String(CellUtil.cloneQualifier(cell))+" ");

System.out.println("value:"+new String(CellUtil.cloneValue(cell))+"

");

}

} catch (IOException e)

{

// TODO Auto-generated catch

block

e.printStackTrace();

}

}

public static void getAllData(Configuration configuration,String

tableName){

HTable table;

try {

table=new

HTable(configuration, tableName);

Scan scan=new

Scan();

ResultScanner

results=table.getScanner(scan);

for(Result

result:results){

for(Cell

cell:result.rawCells()){

System.out.println("RowName:"+new

String(CellUtil.cloneRow(cell))+" ");

System.out.println("Timetamp:"+cell.getTimestamp()+"

");

System.out.println("column

Family:"+new String(CellUtil.cloneFamily(cell))+" ");

System.out.println("row

Name:"+new String(CellUtil.cloneQualifier(cell))+" ");

System.out.println("value:"+new

String(CellUtil.cloneValue(cell))+" ");

}

}

} catch (IOException e)

{

// TODO Auto-generated catch

block

e.printStackTrace();

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值