`
085567
  • 浏览: 213398 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

HBase的安装、配置、管理与编程

阅读更多

环境准备
需要环境:
PC-1 Suse Linux 9  10.192.1.1
PC-2 Suse Linux 9  10.192.1.2
PC-3 Suse Linux 9  10.192.1.3
PC-4 Suse Linux 9  10.192.1.4
其中,PC-1做namenode节点,PC-2、PC-3和PC-4做datanode节点。
并且已经安装成功Hadoop-0.20.1及以上版本。
安装包准备
需要安装包:
zookeeper-3.2.1.tar.gz(stable版本)
hbase-0.20.1.tar.gz(stable版本)
安装步骤
安装和配置ZooKeeper
HBase从0.20.0开始,需要首先安装ZooKeeper。从apache上下载zookeeper-3.2.1.tar.gz(Stable版本),解压到/home/hdfs/目录下。
(1)在namenode节点新建zookeeper目录,在该目录下新建myid文件。
(2)在zookeeper-3.2.1/conf目录下,拷贝zoo_sample.cfg为zoo.cfg。在zoo.cfg中将dataDir改为/home/hdfs/zookeeper,在文件末位添加所有的主机:
server.1=10.192.1.1:2888:3888
server.2=10.192.1.2:2888:3888
server.3=10.192.1.3:2888:3888
server.4=10.192.1.4:2888:3888
server.5=10.192.1.5:2888:3888
server.6=10.192.1.62888:3888
(3)用scp命令将namenode节点的的/home/hdfs/ zookeeper-3.2.1和/home/hdfs/ zookeeper拷贝到其余所有主机的/home/hdfs目录下。
(4)参照zoo.cfg中的配置,在各主机myid文件中写入各自的编号。如:10.192.1.1入1,10.192.1.2写入2
(5)在所有节点上执行bin/zkServer.sh start,分别启动。
执行bin/zkCli.sh -server xxx.xxx.xxx.xxx:2181,检查指定服务器是否成功启动。
安装和配置HBase
下载HBase0.20.1版本,解压到namenode节点的/home/hdfs目录下。
配置说明
(1)系统所有配置项的默认设置在hbase-default.xml中查看,如果需要修改配置项的值,在hbase-site.xml中添加配置项。
在分布式模式下安装HBase,需要添加的最基本的配置项如下:
<property>
<name>hbase.rootdir</name>
<value>hdfs://namenode.hdfs:54310/hbase</value>
<description>The directory shared by region servers.</description>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
<description>The mode the cluster will be in. Possible values are
false: standalone and pseudo-distributed setups with managed Zookeeper
true: fully-distributed with unmanaged Zookeeper Quorum (see hbase-env.sh)
</description>
</property>
(2)在conf/hbase-env.sh中修改添加配置项:
export JAVA_HOME=/usr/java/jdk1.6.0_16
export HBASE_MANAGES_ZK=false
export HBASE_CLASSPATH=/home/hdfs/hadoop-0.20.1/conf
并把~/hadoop-0.20.1/conf/hdfs-site.xml拷贝至~/hbase-3.2.1/conf/目录下。
(3)将ZooKeeper的配置文件zoo.cfg添加到HBase所有主机的CLASSPATH中。
(4)在conf/regionservers中添加hadoop-0.20.1/conf/slaves中所有的datanode节点。
启动
Hadoop、ZooKeeper和HBase之间应该按照顺序启动和关闭:启动Hadoop—>启动ZooKeeper集群—>启动HBase—>停止HBase—>停止ZooKeeper集群—>停止Hadoop。
在namenode节点执行bin/hbase-daemon.sh,启动master。执行bin/start-hbase.sh和bin/stop-hbase.sh 脚本启动和停止HBase服务。
接口说明
HBase按列存储结构化数据,支持建表、插入记录、查询记录、删除记录和索引操作等等,不支持连接和更新操作。
开发步骤
引入JAR包
在Windows客户端编写JAVA程序操作HBase,需要引入一些JAR包。需要引入的JAR如下:hadoop-0.20.1- core.jar,commons-logging-1.0.4.jar,commons-logging-api- 1.0.4.jar,zookeeper-3.2.1.jar,hbase-0.20.1.jar,log4j-1.2.15.jar。
开发模式
在分布式模式下开发,在程序中配置与HDFS和ZooKeeper的连接,即可对数据进行操作。
view plaincopy to clipboardprint?
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.awt.List;
import java.util.Map;
import java.util.NavigableMap;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.HColumnDescriptor;
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.Get;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseManager {
public static void main(String[] args) throws Exception{
HBaseManager manager = new HBaseManager();
manager.testGet();
}
public void testQueryRS()throws Exception{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1");
HTable table = new HTable(config, "commodity");
System.out.println("Get Spin's commodity info");
Scan scanner = new Scan();
scanner.addColumn(Bytes.toBytes("description"));
scanner.setMaxVersions();
ResultScanner rsScanner = table.getScanner(scanner);
System.out.println(rsScanner.toString());
Result rs = rsScanner.next();
while(null != rs){
System.out.println(rs.size());
NavigableMap<byte[],NavigableMap<byte[],NavigableMap<Long,byte[]>>> nMap = rs.getMap();
NavigableMap<byte[],NavigableMap<Long,byte[]>> columnMap = nMap.get(Bytes.toBytes("description"));
NavigableMap<Long,byte[]> qualMap = columnMap.get(new byte[]{});
if(qualMap.entrySet().size() > 0){
System.out.println("---------------------------");
for(Map.Entry<Long, byte[]> m :qualMap.entrySet())
{   
System.out.println("Value:"+ new String(m.getValue()));
}
}
rs = rsScanner.next();
}
}
public void testQueryCommodity()throws Exception{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1.203");
HTable table = new HTable(config, "commodity");
System.out.println("Get Spin's commodity info");
Get mathGet = new Get(new String("Spin").getBytes());
mathGet.addColumn(Bytes.toBytes("widgetname"));
mathGet.setMaxVersions();
Result rs = table.get(mathGet);
NavigableMap<byte[],NavigableMap<byte[],NavigableMap<Long,byte[]>>> nMap = rs.getMap();
NavigableMap<byte[],NavigableMap<Long,byte[]>> columnMap = nMap.get(Bytes.toBytes("widgetname"));
NavigableMap<Long,byte[]> qualMap = columnMap.get(new byte[]{});
if(qualMap.entrySet().size() > 0){
for(Map.Entry<Long, byte[]> m :qualMap.entrySet())
{   
System.out.println("Value:"+ new String(m.getValue()));
break;
}
}
}
public void test()throws Exception{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1");
HBaseAdmin admin = new HBaseAdmin(config);
HTable table = new HTable(config, "scores");
if (admin.tableExists("scores")){
System.out.println("drop table");
admin.disableTable("scores");
admin.deleteTable("scores");
}
System.out.println("create table");
HTableDescriptor tableDescripter = new HTableDescriptor("scores".getBytes());
tableDescripter.addFamily(new HColumnDescriptor("grade"));
tableDescripter.addFamily(new HColumnDescriptor("course"));
admin.createTable(tableDescripter);
System.out.println("add Tom's data");
Put tomPut = new Put(new String("Tom").getBytes());
tomPut.add(new String("grade").getBytes(), new byte[]{}, new String("1").getBytes());
tomPut.add(new String("grade").getBytes(), new String("math").getBytes(), new String("87").getBytes());
tomPut.add(new String("course").getBytes(), new String("math").getBytes(), new String("97").getBytes());
table.put(tomPut);
System.out.println("add Jerry's data");
Put jerryPut = new Put(new String("Jerry").getBytes());
jerryPut.add(new String("grade").getBytes(), new byte[]{}, new String("2").getBytes());
jerryPut.add(new String("grade").getBytes(), new String("math").getBytes(), new String("77").getBytes());
jerryPut.add(new String("course").getBytes(), new String("math").getBytes(), new String("92").getBytes());
table.put(jerryPut);
System.out.println("Get Tom's data");
Get tomGet = new Get(new String("Tom").getBytes());
Result tomResult = table.get(tomGet);
System.out.println(tomResult.toString());
System.out.println("Get Tom's Math grade");
Get mathGet = new Get(new String("Tom").getBytes());
mathGet.addColumn(Bytes.toBytes("grade"));
mathGet.setMaxVersions();
Result rs = table.get(mathGet);
NavigableMap<byte[],NavigableMap<byte[],NavigableMap<Long,byte[]>>> nMap = rs.getMap();
NavigableMap<byte[],NavigableMap<Long,byte[]>> columnMap = nMap.get(Bytes.toBytes("grade"));
NavigableMap<Long,byte[]> qualMap = columnMap.get(Bytes.toBytes("math"));
for(Map.Entry<Long, byte[]> m :qualMap.entrySet())
{
System.out.println("TimeStamp:"+m.getKey());
System.out.println("Value:"+ new String(m.getValue()));
}
System.out.println("Delete a column");
Delete deleteArt = new Delete(Bytes.toBytes("Tom"));
deleteArt.deleteColumn(Bytes.toBytes("grade"), Bytes.toBytes("math"));
table.delete(deleteArt);
}
public void testScanner() throws IOException{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1");
HTable table = new HTable(config, "commodity");
System.out.println("Scan commodity info");
Scan scanner = new Scan();
scanner.addColumn(Bytes.toBytes("widgetname"));
scanner.addColumn(Bytes.toBytes("filename"));
scanner.addColumn(Bytes.toBytes("description"));
scanner.addColumn(Bytes.toBytes("createtime"));
//scanner.setMaxVersions();
//scanner.setMaxVersions(4);
ResultScanner rsScanner = table.getScanner(scanner);
Result rs = rsScanner.next();
for(;null != rs; rs = rsScanner.next()){
System.out.println("rs.getRow()[" + new String(rs.getRow()) + "]");
System.out.println("[" + new String(rs.getValue(Bytes.toBytes("widgetname"))) + "]");
System.out.println("[" + new String(rs.getValue(Bytes.toBytes("filename"))) + "]");
System.out.println("[" + new String(rs.getValue(Bytes.toBytes("description"))) + "]");
String timeStr = new String(rs.getValue(Bytes.toBytes("createtime")));
System.out.println("[" + timeStr + "]");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try{
Date after = dateFormat.parse(timeStr);
System.out.println(after);
}
catch(Exception exp){
exp.printStackTrace();
}
}
}
public void testGet()throws IOException{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1");
HTable table = new HTable(config, "commodity");
Get get = new Get(new String("xxxx.wgt").getBytes());
get.addColumn(Bytes.toBytes("widgetname"));
get.addColumn(Bytes.toBytes("filename"));
get.addColumn(Bytes.toBytes("description"));
get.addColumn(Bytes.toBytes("createtime"));
get.setMaxVersions(2);
System.out.println("00000000000000");
Result dbResult = table.get(get);
System.out.println("11111111111111");
System.out.println(dbResult.size());
System.out.println("2222222222222222");
System.out.println(new String(dbResult.value()));
System.out.println("3333333333333333");
System.out.println(dbResult.containsColumn(Bytes.toBytes("description"), new byte[]{}));
System.out.println("44444444444444444");
System.out.println(dbResult.isEmpty());
System.out.println("55555555555555555");
System.out.println(dbResult.list());
System.out.println("66666666666666666");
System.out.println(dbResult.raw());
System.out.println("77777777777777777");
System.out.println(dbResult.toString());
System.out.println("88888888888888888");
System.out.println(dbResult.raw().clone());
System.out.println("99999999999999999");
}
}
本文转载自:http://hi.baidu.com/surendaxiao/blog/item/c8724b17021c0314972b433e.html

分享到:
评论

相关推荐

    HBase视频教程下载|基于微博数据应用的HBase实战开发

    课时17:使用API管理HBase之编程实战(续) 课时18:搭建分布式HBase集群之Hadoop部署 课时19:搭建分布式HBase集群之HBase部署 课时20:sqoop2部署 课时21:使用sqoop2将mysql数据导入到HBase 课时22:集群管理之...

    大数据开发之案例实践Hbase的设计及企业优化视频教程(视频+讲义+笔记+配置+代码+练习)

    │ PHoenix与Hbase表的关联使用 ├─03_笔记 │ [案例:Hbase的设计及企业优化].txt ├─04_代码 │ └─微博案例 ├─08_作业 │ [案例:Hbase的设计及企业优化].docx ├─09_资料 │ ..... └─10_软件 │ .....

    大数据技术开发环境搭建.docx

    安装虚拟机管理器 2 新建虚拟机,安装Ubuntu 3 锐捷校园认证下虚拟机Ubuntu 的联网设置 11 熟悉Ubuntu系统 13 安装SSH、配置SSH无密码登陆 20 安装Java环境 22 安装Hadoop 24 Hadoop伪分布式配置 25 启动...

    HBase常用Java API

    本节介绍与 HBase 数据存储管理相关的 Java API(基于 HBase 版本 1.2.3)。 HBase 的常用Java API HBase 主要包括 5 大类操作:HBase 的配置、HBase 表的管理、列族的管理、列的管理、数据操作等。 1)org.apache....

    大数据技术原理与应用实验

    第一章 Hadoop基础环境安装和部署 1 实验一 Hadoop基础环境搭建 1 实验二 Hadoop伪分布式环境安装 6 实验三 Hadoop完全分布式环境安装 18 实验四 Hadoop商业版安装 36 ...实验十六 HBase安装部署 123 .........

    云计算第二版

    6.7.1 HBase的安装配置 219 6.7.2 HBase的执行 220 6.7.3 Hbase编程实例 221 6.8 MapReduce编程 223 6.8.1 矩阵相乘算法设计 223 6.8.2 编程实现 224 习题 226 参考文献 226 第7章 Eucalyptus:Amazon云计算的开源...

    新版Hadoop视频教程 段海涛老师Hadoop八天完全攻克Hadoop视频教程 Hadoop开发

    02-hive的元数据库mysql方式安装配置.avi 03-hive的使用.avi 04-hive的常用语法.avi 05-hql语法及自定义函数.avi 06-hbase表结构.avi 07-hbase集群架构及表存储机制.avi 08-hbase-shell.avi 09-hbase的java ...

    2017最新大数据架构师精英课程

    122_hbase的HA配置演示-和Hadoop的HA集成 123_hbase版本机制 124_hbase-ttl-min-versions-keep-deleted-cells" @- N5 [2 s; S3 T$ H' C 125_keep-deleted-cells控制是否保留删除的shell$ V8 |; Q7 g" ]- C# j% |! y ...

    IT十八掌课程-徐培成-大数据-配套PPT

    '[IT18掌www.it18zhang.com]002.VMware下载与安装.pptx' '[IT18掌www.it18zhang.com]015.Hadoop 架构分析.pptx' '[IT18掌www.it18zhang.com]KVM.pptx' '[IT18掌www.it18zhang.com]003.Ubuntu下载与虚拟机下安装.pptx...

    Hadoop实战中文版

    Hive及Hadoop群 11.1 Hive 11.1.1 安装与配置Hive 11.1.2 查询的示例 11.1.3 深入HiveQL 11.1.4 Hive小结 11.2 其他Hadoop 相关的部分 11.2.1 HBase 11.2.2 ZooKeeper 11.2.3 Cascading 11.2.4 Cloudera ...

    Hadoop实战(陆嘉恒)译

    Hive及Hadoop群11.1 Hive11.1.1 安装与配置Hive11.1.2 查询的示例11.1.3 深入HiveQL11.1.4 Hive小结11.2 其他Hadoop 相关的部分11.2.1 HBase11.2.2 ZooKeeper11.2.3 Cascading11.2.4 Cloudera11.2.5 Katta11.2.6 ...

    大数据学习计划.pdf

    服务管理、包管理、NTP 协议时间 服务器、关系型数据库理论和 MySQL 数据库等相关知识的学习, 掌握⼤部分安装部署 Hadoop 集群操作系统层⾯的技能,为后续搭建 Hdoop 集群、对 ⽐ RDBMS 与 NoSQL 数据库打基 础。...

    基于Hadoop的大数据处理系统.pdf

    ⼀般成功安装Hadoop并配置相关环境变量(主要是 JAVA_HOME和HADOOP_HOME)后即可进⼊该模式,⽽⽆需额外配置。该模式并没有充分发挥分布式计算的优势,因为集群中只有⼀台 主机,但是该模式下可以测试Hadoop及相关...

    Hadoop实战

    20110.9 小结 206第11章 Hive及Hadoop群 20711.1 Hive 20711.1.1 安装与配置Hive 20811.1.2 查询的示例 21011.1.3 深入HiveQL 21311.1.4 Hive小结 22111.2 其他Hadoop相关的部分 22111.2.1 HBase 22111.2.2 ...

    Hadoop实战中文版.PDF

    206第11章 Hive及Hadoop群 20711.1 Hive 20711.1.1 安装与配置Hive 20811.1.2 查询的示例 21011.1.3 深入HiveQL 21311.1.4 Hive小结 22111.2 其他Hadoop相关的部分 22111.2.1 HBase 22111.2.2 ...

    大数据场景化解决方案.pdf

    数据存储与管理: ⼤数据利⽤分布式⽂件系统HDFS、HBase、Hive,实现对结构化、半结构化和⾮结构化数据的存储和管理。 数据处理与分析: 利⽤分布式并⾏编程模型和计算框架,结合机器学习和数据挖掘算法,实现对...

    project-rhino:增强了针对Apache Hadoop生态系统的数据保护

    ZooKeeper:配置管理和协调 HBase:HDFS上的面向列的数据库 蜂巢:HDFS上的数据仓库具有类似SQL的访问权限 Pig:用于Hadoop计算的高级编程语言 Oozie:编排和工作流管理 Mahout:机器学习和数据挖掘算法库 Flume:...

    基于flink的电商实时数据分析、推荐、风控项目java源码+项目使用说明.zip

    由canal 监听到Mysql的binlog 后加载到Kafka,再由Kafka流入Flink和ClickHouse,Flink做用户行为的实时计算,ClickHouse做离线计算,支持动态数据分区与规则配置(Flink广播流),支持类与Jar文件的动态编译与动态...

    【白雪红叶】JAVA学习技术栈梳理思维导图.xmind

    编程工具 eclipse myeclipse idea vi VS webstorm sublime text 版本控制 svn git 项目管理 maven Nexus Jenkins 工作软件 反编译软件 office系列 下载器 adobe系列 记录软件 思维导图 ...

Global site tag (gtag.js) - Google Analytics