package test;
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.util.Bytes;
public class HBaseTestCase {
static Configuration cfg=null;
static{
cfg=HBaseConfiguration.create();
cfg.setStrings("hbase.zookeeper.quorum", "192.168.211.129");
}
public static void createTable(String tableName,String columnFamily) throws Exception{
HBaseAdmin admin=new HBaseAdmin(cfg);
if(admin.tableExists(tableName)){
System.out.println("table Exists");
System.exit(0);
}
else{
HTableDescriptor tableDesc=new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println("create table success");
}
}
public static void addData(String tableName,String row,
String columnFamily,String column,String data) throws Exception{
HTable table=new HTable(cfg,tableName);
Put p=new Put(Bytes.toBytes(row));
p.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
table.put(p);
}
public static void getData(String tableName,String row,
String columnFamily,String column) throws IOException{
HTable table=new HTable(cfg,tableName);
Get g=new Get(Bytes.toBytes(row));
Result rowResult=table.get(g);
byte[] ret=rowResult.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
}
public static void main(String[] args) throws Exception {
createTable("mydb","info");
addData("mydb", "firstrow", "info", "c1", "hellodata");
}
}
让hbase运行在Standalone 模式,该客户端程序运行在window7的eclipse中。
我的配置:window7中 C:\windows\system32\drivers\etc\hosts 文件追加一行 192.168.211.129 namenode(我的虚拟机地址)
ubuntu linux中 /etc/hosts 文件中增加一行192.168.211.129 namenode
hbase的conf/regionservers文件中的localhost替换成namenode
hbase的conf/hbase-site.xml增加下面的配置
<property>
<name>hbase.rootdir</name>
<value>file:///home/ming/hbase/hbasedata</value>
</property>
执行 bin/start-hbase.sh
然后在eclipse中运行上面的程序