import java.io.IOException;
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.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.thrift.generated.Hbase;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseAPI {
public static Configuration conf= HBaseConfiguration.create();
//创建table
public static void create(String tablename,String columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(TableName.valueOf(tablename))){
System.out.println(tablename+" exists!");
System.exit(0);
}else{
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
}
}
//通过put为已经存在的表添加一条数据
public static void put(String tablename,String rowkey,String columnFamily,String qualifier,String value) throws IOException{
HTable htable = new HTable(conf, tablename);
Put p1= new Put(Bytes.toBytes(rowkey));
p1.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier), Bytes.toBytes(value));
htable.put(p1);
System.out.println("Put complete!");
}
//通过Get获得数据
public static void get(String tablename,String rowkey,String columnFamily,String qualifier) throws IOException{
HTable htable = new HTable(conf, tablename);
Get g1 = new Get(Bytes.toBytes(rowkey));
g1.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier));
Result result=htable.get(g1);
System.out.println("Get: "+result);
}
public static void scan(String tablename,String columnFamily) throws IOException{
HTable htable = new HTable(conf, tablename);
//Scan s = new Scan();
ResultScanner rs = htable.getScanner(Bytes.toBytes(columnFamily));
for(Result r:rs){
System.out.println("Scan: "+r);
}
}
//删除table
public static void delete(String tablename) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tablename)){
admin.disableTable(tablename);
admin.deleteTable(tablename);
System.out.println(tablename+" is deleted");
}
}
public static void main(String[] args){
String tablename = "table_test";
String columnFamily = "cf1";
try{
create(tablename, columnFamily);
put(tablename, "row1", columnFamily, "test001", "succed");
put(tablename, "row1", columnFamily, "test002", "failed");
get(tablename, "row1",columnFamily,"test001");
scan(tablename,columnFamily);
delete(tablename);
}catch(Exception e){
e.printStackTrace();
}
}
}
运行结果: