1、引入所需要Jar包以及hbase-site.xml
2、创建源文件
package hbase;
/**
*
*/
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.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 HBaseTestCase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String tableName = "test";
String columnFamily = "cf";
try {
if (true == HBaseTestCase.delete(tableName)) {
System.out.println("Delete Table " + tableName + " success!");
}
HBaseTestCase.create(tableName, columnFamily);
HBaseTestCase.put(tableName, "row1", columnFamily, "column1",
"data1");
HBaseTestCase.put(tableName, "row2", columnFamily, "column2",
"data2");
HBaseTestCase.put(tableName, "row3", columnFamily, "column3",
"data3");
HBaseTestCase.put(tableName, "row4", columnFamily, "column4",
"data4");
HBaseTestCase.put(tableName, "row5", columnFamily, "column5",
"data5");
HBaseTestCase.get(tableName, "row1");
HBaseTestCase.scan(tableName);
} catch (Exception e) {
e.printStackTrace();
}
}
static Configuration cfg = HBaseConfiguration.create();
static {
System.out.println(cfg.get("hbase.master"));
}
public static void create(String tableName, String columnFamily)
throws Exception {
HBaseAdmin admin = new HBaseAdmin(cfg);
if (admin.t