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();
}
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值