Hbase配置及客户端远程访问Hbase设置docxWord格式文档下载.docx
《Hbase配置及客户端远程访问Hbase设置docxWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《Hbase配置及客户端远程访问Hbase设置docxWord格式文档下载.docx(9页珍藏版)》请在冰豆网上搜索。
href="
configuration.xsl"
<
configuration>
property>
name>
hbase.rootdir<
/name>
value>
file:
///<
PATH>
/hbase<
/value>
description>
数据文件存放位置,可以是本地文件系统,也可以是HDFS文件系统
/description>
/property>
hbase.zookeeper.quorum<
222.204.248.111<
ZooKeepeffi务器设置,在分布式配置中,value中可以有多个IP地址,每个都是
ZooKeepe的节点。
Hbase必须搭建在ZooKeeper集群环境下,通过Zookeepe获取Hbase各节点的IP地址,协调通信。
/description>
hbase.cluster.distributed<
true<
设置是否为分布式配置,默认为true。
作为伪分布实验,可以设为true
/configuration>
(3)、配置regionservers
在HBASE_HOME/conf目录中,修改regionservers文件。
该文件用于记录Hbase中regionserver的域名(IP地址)。
在单机环境中为本机IP地址,实验中为222.204.248.111。
(4)、配置hostname以及hosts
在/etc/hostname文件中设置主机名
在/etc/hosts中设置主机名与其IP地址的对应关系。
Hbase的Master节点必
须配置所有Slave节点的主机名-IP地址对应关系;
Slave节点只需要直到Master节点的主机名-IP地址对应关系。
(5)、启动Hbase实例
若hbase-env.sh文件中exportHBASE_MANAGES_ZK=true时,表示由Hbase托管Zookeeper集群,通过hbase-site.xml来配置Zookeeper参数;
若为false,则由用户自己管理Zookeeper集群,需要下载Zookeeper程序,自己启动。
为方便启动Hbase,可以再环境变量中在Path变量中添加HBASE_HOME/bin路径。
若环境变量设置后,直接运行start-hbase.sh启动hbase环境;
否则进入
HBASE_HOME/bin目录,执行./start-hbase.sh,启动hbase环境。
之后,执行hbaseshell进入hbase的shell模式,可以输入命令创建、查询、删除hbase表o
在web浏览器中输入http:
//222.204.248.111:
60010可以查看Hbase中Master节点的信息;
输入http:
60030可以查看Hbase中RegionServer节点的信息。
至此Hbase单机伪分布配置完成
二、客户端Java程序设置
本节详细描述在任意客户端上(为安装Hbase环境)中如何通过Java远程调访问Hbase服务器(单机伪分布)。
实验中Java程序通过eclispe编译运行,以下方法只说明在eclipse中的配制方法
1、准备必要jar包
编译运行Hbase客户端程序需要以下几个jar包:
hadoop-core-1.0.0.ja、
commons-loggings-version.jar、commons-cli-vesion.jar、
commons-lang-vesion.ja、commons-configration-version.jar、hbase-0.92.1.ja、zookeeper-3.4.3.ja、slf4j-api-1.5.8.jar、slf4j-log4j12-1.5.8.jar、Iog4j-1.2.16.jar。
这些包都在HBASE_HOME/lib目录中可以找到,本版号以lib目录下为准。
将以上jar包添加到eclipse中的buildpath中
2、写客户端测试代码
我写了两个Java程序,分为HBaseHelper.java及PutExample.java。
其中HBaseHelper.java圭寸装了基本操作函数,女口同DataAccessObject类;
PutExample.java进行了简单的创建表及插入2条记录的任务。
HBaseHelper.java
packagecom.yhq.ch03;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Random;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.hbase.HColumnDescriptor;
importorg.apache.hadoop.hbase.HTableDescriptor;
importorg.apache.hadoop.hbase.KeyValue;
importorg.apache.hadoop.hbase.client.Get;
importorg.apache.hadoop.hbase.client.HBaseAdmin;
importorg.apache.hadoop.hbase.client.HTable;
importorg.apache.hadoop.hbase.client.Put;
importorg.apache.hadoop.hbase.client.Result;
importorg.apache.hadoop.hbase.util.Bytes;
publicclassHBaseHelper{
privateConfigurationconf=null;
privateHBaseAdminadmin=null;
protectedHBaseHelper(Configurationconf)throwsIOException{this.conf=conf;
this.admin=newHBaseAdmin(conf);
}
throws
publicstaticHBaseHelpergetHelper(Configurationconf)IOException{
returnnewHBaseHelper(conf);
publicbooleanexistsTable(Stringtable)throwsIOException{returnadmin.tableExists(table);
publicvoidcreateTable(Stringtable,String...colfams)throwsIOException{createTable(table,null,colfams);
publicvoidcreateTable(Stringtable,byte[][]splitKeys,String...colfams)throwsIOException{
HTableDescriptordesc=newHTableDescriptor(table);
for(Stringcf:
colfams){
HColumnDescriptorcoldef=newHColumnDescriptor(cf);
desc.addFamily(coldef);
if(splitKeys!
=null){
admin.createTable(desc,splitKeys);
}else{
admin.createTable(desc);
publicvoiddisableTable(Stringtable)throwsIOException{admin.disableTable(table);
publicvoiddropTable(Stringtable)throwsIOException{
if(existsTable(table)){
disableTable(table);
admin.deleteTable(table);
publicvoidfillTable(Stringtable,intstartRow,intendRow,intnumCols,String...colfams)
throwsIOException{fillTable(table,startRow,endRow,numCols,-1,false,colfams);
publicvoidfillTable(Stringtable,intstartRow,intendRow,intnumCols,booleansetTimestamp,String...colfams)throwsIOException{
fillTable(table,startRow,endRow,numCols,-1,setTimestamp,colfams);
publicvoidfillTable(Stringtable,intstartRow,intendRow,intnumCols,intpad,booleansetTimestamp,String...colfams)throwsIOException{
fillTable(table,startRow,endRow,numCols,pad,setTimestamp,false,colfams);
//根据其实行,结束行等参数向表中添加参数publicvoidfillTable(Stringtable,intstartRow,intendRow,intnumCols,intpad,booleansetTimestamp,booleanrandom,String...colfams)throwsIOException{
HTabletbl=newHTable(conf,table);
Randomrnd=newRandom();
for(introw=startRow;
row<
=endRow;
row++){
for(intcol=0;
col<
numCols;
col++){
Putput=newPut(Bytes.toBytes("
row-"
+padNum(row,pad)));
StringcolName="
col-"
+padNum(col,pad);
Stringval="
val-"
+(random?
Integer.toString(rnd.nextInt(numCols)):
padNum(row,pad)+"
."
+padNum(col,pad));
if(setTimestamp){put.add(Bytes.toBytes(cf),Bytes.toBytes(colName),col,Bytes.toBytes(val));
}else{put.add(Bytes.toBytes(cf),Bytes.toBytes(colName),
Bytes.toBytes(val));
}tbl.put(put);
}tbl.close();
〃用以下算法形成数字值,pad为位数,高位用0补齐
publicStringpadNum(intnum,intpad){Stringres=Integer.toString(num);
if(pad>
0){
while(res.length()<
pad){res="
0"
+res;
returnres;
publicvoidput(Stringtable,Stringrow,Stringfam,Stringqual,Stringval)throwsIOException{
Putput=newPut(Bytes.toBytes(row));
put.add(Bytes.toBytes(fam),Bytes.toBytes(qual),Bytes.toBytes(val));
tbl.put(put);
tbl.close();
publicvoidput(Stringtable,Stringrow,Stringfam,Stringqual,longts,Stringval)throwsIOException{
put.add(Bytes.toBytes(fam),Bytes.toBytes(qual),ts,Bytes.toBytes(val));
tbl.put(put);
tbl.close();
publicvoidput(Stringtable,String[]rows,String[]fams,String[]quals,long[]ts,String[]vals)throwsIOException{
for(Stringrow:
rows){
for(Stringfam:
fams){
intv=0;
for(Stringqual:
quals){
Stringval=vals[v<
vals.length?
v:
vals.length-1];
longt=ts[v<
ts.length?
ts.length-1];
put.add(Bytes.toBytes(fam),Bytes.toBytes(qual),t,Bytes.toBytes(val));
v++;
publicvoiddump(Stringtable,String[]rows,String[]fams,String[]quals)throwsIOException{
List<
Get>
gets=newArrayList<
();
Getget=newGet(Bytes.toBytes(row));
get.setMaxVersions();
if(fams!
=null){for(Stringfam:
quals){get.addColumn(Bytes.toBytes(fam),Bytes.toBytes(qual));
}}
}gets.add(get);
Result[]results=tbl.get(gets);
for(Resultresult:
results){
for(KeyValuekv:
result.raw()){System.out.println("
KV:
"
+kv+"
Value:
+Bytes.toString(kv.getValue()));
PutExample.javapackagecom.yhq.ch03;
importorg.apache.hadoop.hbase.HBaseConfiguration;
publicclassPutExample{
publicstaticvoidmain(String[]args)throwsIOException{
Configurationconf=HBaseConfiguration.create();
//coPutExample-1-CreateConfCreatetherequiredconfiguration.
//AAPutExample
HBaseHelperhelper=HBaseHelper.getHelper(conf);
helper.dropTable("
testtable"
);
helper.createTable("
"
colfam1"
//vvPutExample
HTabletable=newHTable(conf,"
//coPutExample-2-NewTableInstantiateanewclient.
row1"
));
//coPutExample-3-NewPutCreateputwithspecificrow.
put.add(Bytes.toBytes("
),Bytes.toBytes("
qual1"
),
Bytes.toBytes("
val1"
//coPutExample-4-AddCol1Addacolumn,whosenameis"
colfam1:
totheput.
qual2"
val2"
//coPutExample-4-AddCol2Addanothercolumn,whosenameis"
table.put(put);
//coPutExample-5-DoPutStorerowwithcolumnintotheHBasetable.
3、配置客户端的hbase-site.xml
客户端仍要配置hbase-site.xml,用于设置Zookeeper集群的IP地址,程序会读取该配置文件,连接Zookeeper集群,由Zookeeper集群协调Hbase各节点。
hbase-site.xml简单的设置如下:
xmlversion="
?
xml-stylesheettype="
href="
configuration.xsl"
configuration>
property>
hbase.zookeeper.quorum<
/name>
value>
222.204.248.111<
/value>
description>
CommaseparatedlistofserversintheZooKeeperQuorum.For
"
hostl.mydomain.com,host2.mydomain.com,host3.mydomain.com"
.
Bydefaultthisissettolocalhostforlocalan
pseudo-distributedmodesofoperation.Fora
fully-distributedsetup,thisshouldbesettoafulllist
ofZooKeeperquorumservers.IfHBASEMANAGESZKissetin
hbase-env.shthisisthelistofserverswhichwewill
start/stopZooKeeperon.
/property>
/config