手机版
你好,游客 登录 注册
背景:
阅读新闻

Java操作HBase

[日期:2014-03-02] 来源:Linux社区  作者:englishsname [字体:大 中 小]

Java操作HBase

package Hbase;


import org.apache.Hadoop.conf.Configuration;


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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableDescriptors;
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.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;


public class HBaseAPP {


private static final String TABLE_NAME = "table1";
private static final String FAMILY_NAME = "family1";
private static final String ROW_KEYS = "rowKey1";


public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://hadoop1:9000/hbase");
// 使用eclipse时必须添加这个,否则无法定位
conf.set("hbase.zookeeper.quorum", "hadoop1");
// HBaseAdmin用来创建表 删除表使用 alt+shift+l 抽取变量
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
//创建表
//createTable(hBaseAdmin);
//删除表
//deleteTable(hBaseAdmin);
// HTable 插入 查询 使用

HTable hTable = new HTable(conf, TABLE_NAME);
//插入记录
//putRecord(hTable);
//查询记录
//getRecord(hTable);
//扫面
//scanRecord(hTable);
}

 


private static void scanRecord(HTable hTable) throws IOException {
Scan scan = new Scan();
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());
System.out.println(result+"\t"+new String(value));
}
}

 


private static void getRecord(HTable hTable) throws IOException {
Get get = new Get(ROW_KEYS.getBytes());
Result result = hTable.get(get);
byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());
System.out.println(new String(value));
}

 


private static void putRecord(HTable hTable) throws IOException {
Put put = new Put(ROW_KEYS.getBytes());
put.add(FAMILY_NAME.getBytes(), "age".getBytes(), "28".getBytes());
hTable.put(put);
hTable.close();
}

 


private static void deleteTable(final HBaseAdmin hBaseAdmin)
throws IOException {
hBaseAdmin.disableTable(TABLE_NAME);
hBaseAdmin.deleteTable(TABLE_NAME);
}


private static void createTable(HBaseAdmin hBaseAdmin) throws IOException {
if (!(hBaseAdmin.tableExists(TABLE_NAME))) {
HTableDescriptor hTableDescriptor = new HTableDescriptor(TABLE_NAME);
HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);
hTableDescriptor.addFamily(family);
hBaseAdmin.createTable(hTableDescriptor);
}
}
}

HBase 的详细介绍:请点这里
HBase 的下载地址:请点这里

相关阅读:

Hadoop+HBase搭建云存储总结 PDF http://www.linuxidc.com/Linux/2013-05/83844.htm

HBase 结点之间时间不一致造成regionserver启动失败 http://www.linuxidc.com/Linux/2013-06/86655.htm

Hadoop+ZooKeeper+HBase集群配置 http://www.linuxidc.com/Linux/2013-06/86347.htm

Hadoop集群安装&HBase实验环境搭建 http://www.linuxidc.com/Linux/2013-04/83560.htm

基于Hadoop集群的HBase集群的配置 http://www.linuxidc.com/Linux/2013-03/80815.htm‘

Hadoop安装部署笔记之-HBase完全分布模式安装 http://www.linuxidc.com/Linux/2012-12/76947.htm

单机版搭建HBase环境图文教程详解 http://www.linuxidc.com/Linux/2012-10/72959.htm

linux
相关资讯       Hbase 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款