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.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.util.Bytes;


public class HBaseClient {
private static Configuration configuration =HBaseConfiguration.create();
static{
System.out.println(configuration.get("hbase.master"));
}
public static void main(String[] args) throws Exception {
//HBaseClient.createTable("users", "info", 3);
//HBaseClient.putTable("mytable", "s-"+ String.valueOf(new Date().getTime()), "cf", "col","xxx");
//HBaseClient.getTable("mytable", "s-1528369637196");
HBaseClient.getValueForResult("mytable", "s-1528369637196", "cf", "col");


}

//创建表

public static void createTable(String tableName,String columnFamily,Integer versionNum) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
HBaseAdmin admin = new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
System.out.println(tableName+"已存在!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFamily);
if (versionNum != null) {
hColumnDescriptor.setMaxVersions(versionNum);
}
hTableDescriptor.addFamily(hColumnDescriptor);
admin.close();
System.out.println(tableName+"创建成功!");
}

}

//向表里增加内容

public static void putTable(String tableName,String row,String columnFamily,String column,String data) throws IOException{
HTable table = new HTable(configuration, tableName);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),data.getBytes());
table.put(put);
table.close();
System.out.println("put--->row:" + row + ",columnFamily:" + columnFamily + ",column:" + column + ",data:" + data);

}

//获取表里一行数据

public static Result getTable(String tableName,String row) throws IOException{
HTable hTable = new HTable(configuration, tableName);
Get get = new Get(Bytes.toBytes(row));
Result result = hTable.get(get);
System.out.println("Get: " + result);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
String cloneFamily = Bytes.toString(CellUtil.cloneFamily(cell));
String cloneQualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String cloneValue = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println("cloneFamily: "+cloneFamily);
System.out.println("cloneQualifier: "+cloneQualifier);
System.out.println("cloneValue: "+cloneValue);
}
hTable.close();
return result;

}

//获取表里一列数据

public static String getValueForResult(String tableName, String row , String columnFamily, String column) throws Exception {
        HTable table = new HTable(configuration, tableName);
        Get get = new Get(Bytes.toBytes(row));
        Result result = table.get(get);
        System.out.println("Get: " + result);
        byte[] value = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
        String m = new String(value);  System.out.println("Found row: " + m);
        table.close();
        return m;
    }
}

猜你喜欢

转载自blog.csdn.net/csdn_hzx/article/details/80684981