1、Hbase配置及客户端远程访问Hbase设置Hbase(单机)配置及客户端运行设置一、 安装Hbase本节详细描述Hbase伪分布模式的配置方法。1、 安装Java(1)、下载JDK软件包,bin格式(2)、将软件包拷到系统目录中,加上可执行权限,执行bin文件,解压java文件(3)、把生成的文件夹复制到/usr/java/ mkdir /usr/java cp r jdk1.6.0_xx /usr/java(4)、修改/root/.bashrc 文件,在文件中添加如下代码:export JAVA_HOME=/usr/lib/java/jdk1.6.0_31export CLASSPATH=
2、.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarexport PATH=$JAVA_HOME/bin:$PATHexport JAVA_HOME CLASSPATH PATH(5)、source /root/.bashrc2、 配置Hbase(1)、下载最新的Hbase程序包,解压到/usr/Apache目录下(2)、修改Hbase_Home下conf/hbase-site.xml文件,单机配置如下:hbase.rootdirfile:/hbase数据文件存放位置,可以是本地文件系统,也可以是HDFS文件系统hbase.zookeeper.quo
3、rum 222.204.248.111 ZooKeeper服务器设置,在分布式配置中,value中可以有多个IP地址,每个都是ZooKeeper的节点。Hbase必须搭建在ZooKeeper集群环境下,通过Zookeeper获取Hbase各节点的IP地址,协调通信。 hbase.cluster.distributedtrue设置是否为分布式配置,默认为true。作为伪分布实验,可以设为true(3)、配置regionservers在HBASE_HOME/conf目录中,修改regionservers文件。该文件用于记录Hbase中regionserver的域名(IP地址)。在单机环境中为本机I
4、P地址,实验中为222.204.248.111。(4)、配置hostname以及hosts 在/etc/hostname文件中设置主机名 在/etc/hosts中设置主机名与其IP地址的对应关系。Hbase的Master节点必须配置所有Slave节点的主机名-IP地址对应关系;Slave节点只需要直到Master节点的主机名-IP地址对应关系。(5)、启动Hbase实例 若hbase-env.sh文件中export HBASE_MANAGES_ZK=true时,表示由Hbase托管Zookeeper集群,通过hbase-site.xml来配置Zookeeper参数;若为false,则由用户自己
5、管理Zookeeper集群,需要下载Zookeeper程序,自己启动。 为方便启动Hbase,可以再环境变量中在Path变量中添加HBASE_HOME/bin路径。 若环境变量设置后,直接运行start-hbase.sh,启动hbase环境;否则进入HBASE_HOME/bin目录,执行./ start-hbase.sh, 启动hbase环境。 之后,执行hbase shell进入hbase的shell模式,可以输入命令创建、查询、删除hbase表。 在web浏览器中输入http:/222.204.248.111:60010可以查看Hbase中Master节点的信息;输入http:/222.2
6、04.248.111:60030可以查看Hbase中RegionServer节点的信息。至此Hbase单机伪分布配置完成二、 客户端Java程序设置本节详细描述在任意客户端上(为安装Hbase环境)中如何通过Java远程调访问Hbase服务器(单机伪分布)。实验中Java程序通过eclispe编译运行,以下方法只说明在eclipse中的配制方法1、 准备必要jar包编译运行Hbase客户端程序需要以下几个jar包:hadoop-core-1.0.0.jar、commons-loggings-version.jar、commons-cli-vesion.jar、commons-lang-vesi
7、on.jar、commons-configration-version.jar、hbase-0.92.1.jar、zookeeper-3.4.3.jar、slf4j-api-1.5.8.jar、slf4j-log4j12-1.5.8.jar、log4j-1.2.16.jar。这些包都在HBASE_HOME/lib目录中可以找到,本版号以lib目录下为准。将以上jar包添加到eclipse中的buildpath中2、 写客户端测试代码我写了两个Java程序,分为HBaseHelper.java及PutExample.java。其中HBaseHelper.java封装了基本操作函数,如同DataA
8、ccessObject类;PutExample.java进行了简单的创建表及插入2条记录的任务。 HBaseHelper.javapackage com.yhq.ch03;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Random;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.had
9、oop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.KeyValue;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
10、.apache.hadoop.hbase.util.Bytes;public class HBaseHelper private Configuration conf = null; private HBaseAdmin admin = null; protected HBaseHelper(Configuration conf) throws IOException this.conf = conf; this.admin = new HBaseAdmin(conf); public static HBaseHelper getHelper(Configuration conf) throw
11、s IOException return new HBaseHelper(conf); public boolean existsTable(String table) throws IOException return admin.tableExists(table); public void createTable(String table, String. colfams) throws IOException createTable(table, null, colfams); public void createTable(String table, byte splitKeys,
12、String. colfams) throws IOException HTableDescriptor desc = new HTableDescriptor(table); for (String cf : colfams) HColumnDescriptor coldef = new HColumnDescriptor(cf); desc.addFamily(coldef); if (splitKeys != null) admin.createTable(desc, splitKeys); else admin.createTable(desc); public void disabl
13、eTable(String table) throws IOException admin.disableTable(table); public void dropTable(String table) throws IOException if (existsTable(table) disableTable(table); admin.deleteTable(table); public void fillTable(String table, int startRow, int endRow, int numCols, String. colfams) throws IOExcepti
14、on fillTable(table, startRow, endRow, numCols, -1, false, colfams); public void fillTable(String table, int startRow, int endRow, int numCols, boolean setTimestamp, String. colfams) throws IOException fillTable(table, startRow, endRow, numCols, -1, setTimestamp, colfams); public void fillTable(Strin
15、g table, int startRow, int endRow, int numCols, int pad, boolean setTimestamp, String. colfams) throws IOException fillTable(table, startRow, endRow, numCols, pad, setTimestamp, false, colfams); /根据其实行,结束行等参数向表中添加参数 public void fillTable(String table, int startRow, int endRow, int numCols, int pad,
16、boolean setTimestamp, boolean random, String. colfams) throws IOException HTable tbl = new HTable(conf, table); Random rnd = new Random(); for (int row = startRow; row = endRow; row+) for (int col = 0; col 0) while (res.length() pad) res = 0 + res; return res; public void put(String table, String ro
17、w, String fam, String qual, String val) throws IOException HTable tbl = new HTable(conf, table); Put put = new Put(Bytes.toBytes(row); put.add(Bytes.toBytes(fam), Bytes.toBytes(qual), Bytes.toBytes(val); tbl.put(put); tbl.close(); public void put(String table, String row, String fam, String qual, lo
18、ng ts, String val) throws IOException HTable tbl = new HTable(conf, table); Put put = new Put(Bytes.toBytes(row); put.add(Bytes.toBytes(fam), Bytes.toBytes(qual), ts, Bytes.toBytes(val); tbl.put(put); tbl.close(); public void put(String table, String rows, String fams, String quals, long ts, String
19、vals) throws IOException HTable tbl = new HTable(conf, table); for (String row : rows) Put put = new Put(Bytes.toBytes(row); for (String fam : fams) int v = 0; for (String qual : quals) String val = valsv vals.length ? v : vals.length - 1; long t = tsv ts.length ? v : ts.length - 1; put.add(Bytes.to
20、Bytes(fam), Bytes.toBytes(qual), t, Bytes.toBytes(val); v+; tbl.put(put); tbl.close(); public void dump(String table, String rows, String fams, String quals) throws IOException HTable tbl = new HTable(conf, table); List gets = new ArrayList(); for (String row : rows) Get get = new Get(Bytes.toBytes(
21、row); get.setMaxVersions(); if (fams != null) for (String fam : fams) for (String qual : quals) get.addColumn(Bytes.toBytes(fam), Bytes.toBytes(qual); gets.add(get); Result results = tbl.get(gets); for (Result result : results) for (KeyValue kv : result.raw() System.out.println(KV: + kv + , Value: +
22、 Bytes.toString(kv.getValue(); PutExample.javapackage com.yhq.ch03;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.
23、hbase.util.Bytes;public class PutExample public static void main(String args) throws IOException Configuration conf = HBaseConfiguration.create(); / co PutExample-1-CreateConf Create the required configuration. / PutExample HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable(testtable
24、); helper.createTable(testtable, colfam1); / vv PutExample HTable table = new HTable(conf, testtable); / co PutExample-2-NewTable Instantiate a new client. Put put = new Put(Bytes.toBytes(row1); / co PutExample-3-NewPut Create put with specific row. put.add(Bytes.toBytes(colfam1), Bytes.toBytes(qual
25、1), Bytes.toBytes(val1); / co PutExample-4-AddCol1 Add a column, whose name is colfam1:qual1, to the put. put.add(Bytes.toBytes(colfam1), Bytes.toBytes(qual2), Bytes.toBytes(val2); / co PutExample-4-AddCol2 Add another column, whose name is colfam1:qual2, to the put. table.put(put); / co PutExample-
26、5-DoPut Store row with column into the HBase table. 3、 配置客户端的hbase-site.xml客户端仍要配置hbase-site.xml,用于设置Zookeeper集群的IP地址,程序会读取该配置文件,连接Zookeeper集群,由Zookeeper集群协调Hbase各节点。hbase-site.xml简单的设置如下: hbase.zookeeper.quorum 222.204.248.111 Comma separated list of servers in the ZooKeeper Quorum. For example, ,. By default this is set to localhost for lo
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1