centos6.4安装hbase

hbase集群版

安装好hadoop并创建好目录
./bin/hadoop fs -mkdir /hbase

所有hbase节点同步时间
ntpdate ntp.api.bz
hwclock -w


下载hbase
以下在hbase master节点操作
http://mirrors.cnnic.cn/apache/hbase/stable/hbase-1.1.2-bin.tar.gz
tar zxvf hbase-1.1.2-bin.tar.gz
mv hbase-1.1.2 /home/hadoop
chown -R hadoop.hadoop /home/hadoop/hbase-1.1.2
su - hadoop

修改配置文件
cd hbase-1.1.2/conf

vim regionservers
添加

slave1
slave2
slave3

vim hbase-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>hbase.rootdir</name>
    <value>hdfs://master1:9000/hbase</value>
  </property>
  <property>
    <name>hbase.cluster.distributed</name>
    <value>true</value>
  </property>
  <property>
    <name>hbase.zookeeper.quorum</name>
    <value>192.168.20.211,192.168.20.212,192.168.20.213</value>
  </property>
</configuration>


vim hbase-env.sh


复制代码到regionservers节点
scp -r hbase-1.1.2/ slave1:/home/hadoop/
scp -r hbase-1.1.2/ slave2:/home/hadoop/
scp -r hbase-1.1.2/ slave3:/home/hadoop/


启动hbase集群
/home/hadoop/hbase-1.1.2/bin/start-hbase.sh


访问hbase web页
http://192.168.20.221:16010


停止hbase集群
/home/hadoop/hbase-1.1.2/bin/stop-hbase.sh

进入hbase shell
/home/hadoop/hbase-1.1.2/bin/hbase shell



单机版

安装jdk

http://blog.csdn.net/u013619834/article/details/38894649


下载

wget http://apache.fayea.com/apache-mirror/hbase/stable/hbase-0.98.5-hadoop2-bin.tar.gz


安装

tar zxvf hbase-0.98.5-hadoop2-bin.tar.gz
mv hbase-0.98.5-hadoop2 /usr/local/hbase
mkdir -p /hbase/data

cd /usr/local/hbase
vim conf/hbase-site.xml
添加
<configuration>
    <property>
        <name>hbase.rootdir</name>
        <value>file:///hbase/data</value>
    </property>
</configuration>

启动hbase
./bin/start-hbase.sh

用shell连接Hbase
./bin/hbase shell

查看帮助
help

查看hbase版本
version

查看状态
status

创建表
create 't1', 'c1'

列出所有的表
list

插入第1条数据
put 't1', 'row1', 'c1:a', 'value1'

插入第2条数据
put 't1', 'row2', 'c1:a', 'value2'

插入第2条数据
put 't1', 'row3', 'c1:b', 'value3'

scan表
scan 't1',{LIMIT=>1}

查看数据
get 't1', 'row1'

查总数
count 't1'

删除表
disable 't2'
drop 't2'

更新一条记录(就是重写一遍进行覆盖)

put 't1', 'row1', 'c1:a', 'value1'


单表导出

/home/hadoop/hbase/bin/hbase org.apache.hadoop.hbase.mapreduce.Driver export resources_picture_common resources_picture_common


单表导入(需要提前创建表结构)

/home/hadoop/hbase/bin/hbase org.apache.hadoop.hbase.mapreduce.Driver import resources_picture_common resources_picture_common/



在HBASE上安装启动thrift并使用python通过thrift连接hbase进行测试

yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel

wget http://ftp.gnu.org/gnu/bison/bison-2.5.tar.gz
cd bison-2.5
./configure
make
make install

tar zxvf thrift-0.9.2.tar.gz
cd thrift-0.9.2
./configure --prefix=/usr/local/thrift --with-python --with-lua=no
make
make install

添加环境变量
echo "export PATH=/usr/local/thrift/bin:\$PATH" >>/etc/profile.d/app.sh
source /etc/profile

查看thrift版本
thrift -version

让thrift支持hbase
cd ~
thrift --gen py /usr/local/src/hbase-0.98.8/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
会在当前目前下创建一个目录gen-py


启动thrift服务:
./bin/hbase thrift -p 9090 start




在客户端进行测试
pip install thrift

把gen-py目录复制到python相关目录中
cp -r gen-py/hbase /usr/local/python276/lib/python2.7/site-packages

PYTHON连接Hbase的代码
#!/usr/bin/env python

import sys
sys.path.append('/usr/local/python276/lib/python2.7/site-packages/hbase')

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase

from hbase.ttypes import *

transport = TSocket.TSocket('192.168.3.109', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)

transport.open()

print(client.getTableNames())


在HBASE上安装启动thrift并使用PHP通过thrift连接hbase进行测试



让thrift支持hbase
cd ~
thrift --gen py /usr/local/src/hbase-0.98.8/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
会在当前目前下创建一个目录gen-py

复制thrift源码中的thrift中的/usr/local/src/thrift-0.9.2/lib/php和gen-py到项目的目录下


php代码如下

<?php

# Change this to match your thrift root.
$GLOBALS['THRIFT_ROOT'] = 'Hbase/thrift/lib/Thrift';

require_once($GLOBALS['THRIFT_ROOT'].'/Thrift.php');

# Something is wrong with this. Is this the PHP way of doing things?
# Old versions of thrift seemingly worked with just a couple includes.
require_once($GLOBALS['THRIFT_ROOT'].'/Type/TMessageType.php');
require_once($GLOBALS['THRIFT_ROOT'].'/Type/TType.php');
require_once($GLOBALS['THRIFT_ROOT'].'/Exception/TException.php');
require_once($GLOBALS['THRIFT_ROOT'].'/Factory/TStringFuncFactory.php');
require_once($GLOBALS['THRIFT_ROOT'].'/StringFunc/TStringFunc.php');
require_once($GLOBALS['THRIFT_ROOT'].'/StringFunc/Core.php');
require_once($GLOBALS['THRIFT_ROOT'].'/Transport/TSocket.php');
require_once($GLOBALS['THRIFT_ROOT'].'/Transport/TBufferedTransport.php');
require_once($GLOBALS['THRIFT_ROOT'].'/Protocol/TBinaryProtocol.php');

require_once($GLOBALS['THRIFT_ROOT'].'/../../../../Hbase/Hbase.php');
require_once($GLOBALS['THRIFT_ROOT'].'/../../../../Hbase/Types.php');

use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
use Thrift\Protocol\TBinaryProtocol;
use Hbase\HbaseClient;
use Hbase\ColumnDescriptor;
use Hbase\Mutation;


$socket = new TSocket('192.168.2.104', 9090);

$socket->setSendTimeout(10000); // Ten seconds (too long for production, but this is just a demo ;)
$socket->setRecvTimeout(20000); // Twenty seconds
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new HbaseClient($protocol);

$transport->open();


$tableName = 'ktapinginxlog';

//查询表的描述
$descriptors = $client->getColumnDescriptors($tableName);
asort( $descriptors );
foreach ( $descriptors as $col ) {
  //echo( "  column: {$col->name}, maxVer: {$col->maxVersions}\n" );
}

//获取表一行数据
$row_name = '00120';
$fam_col_name = 'a:uri';
$arr = $client->get($tableName, $row_name, $fam_col_name, array());
//print_r($arr);


//scan table
//http://blog.csdn.net/aylazhang/article/details/8563895
//http://blog.csdn.net/zhumin726/article/details/8552748
//http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/filter
$filter ="(PrefixFilter('1') AND (PrefixFilter('17'))";
$scan = new \Hbase\TScan();
$scan->filterString=$filter;
$scanner = $client->scannerOpenWithScan($tableName, $scan, array());

while (TRUE) {
    $get_arr = $client->scannerGetList($scanner,1);
    
    if(!$get_arr) {
        break;
    }
    print_r($get_arr);
    echo "<br>";
}


//$client->scannerClose($scan);


// echo "----scanner get ------\r\n";
// $startRow = '1';
// $columns = array ('column' => 'a', );
// $scan = $client->scannerOpen($tableName, $startRow, $columns, array());
// $nbRows = 1000;
// $arr = $client->scannerGetList($scan, $nbRows);
// print_r($arr);



$transport->close();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值