5.8 Java操作HBase

/**
* <p>内容描述:操作HBase</p>
* @author lvjie
* @date 2017年7月7日 上午11:54:20
*/
public class UseHbase {
public static String TN = "tab1";
/**
* 创建表
* @param tb
* @throws IOException
*/
public void createTab(String tb,HBaseAdmin hBaseAdmin) throws IOException{
//查看表是否存在,存在就废弃,删除
if(hBaseAdmin.tableExists(TN)) {
hBaseAdmin.disableTable(TN);//废弃表
hBaseAdmin.deleteTable(TN);//删除表
}
//定义表名
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
//定义列族
HColumnDescriptor family = new HColumnDescriptor("cf");
family.setInMemory(true);
family.setMaxVersions(1);
desc.addFamily(family);
hBaseAdmin.createTable(desc);
}
/**
* 插入数据
* @param htable
* @throws Exception
*/
public void insertDB(HTable htable) throws Exception {
//rowkey设计
Put put = new Put("18612341234_15525353434".getBytes());
put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
htable.put(put);
}
/**
* 查询 某些cell
* @throws Exception
*/
public void getDB(HTable htable) throws Exception {
// 参数:rowkey设计
Get get = new Get("18612341234_15525353434".getBytes());
get.addColumn("cf".getBytes(), "name".getBytes());
Result rs = htable.get(get);
Cell cell = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
System.out.println(new String(CellUtil.cloneValue(cell)));
}
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "node1");
try {
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
HTable htable = new HTable(conf, TN);
UseHbase use = new UseHbase();
//use.createTab(TN,hBaseAdmin);
//use.insertDB(htable);
use.getDB(htable);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

猜你喜欢

转载自blog.csdn.net/u011418530/article/details/80654060
5.8