顯示具有 Hbase 標籤的文章。 顯示所有文章
顯示具有 Hbase 標籤的文章。 顯示所有文章

2011年8月15日 星期一

[Hbase]重開機後Hbase內的資料消失的問題


每次電腦重開後,把Hbase重新啟動後
會發現裡面的資料竟然都不見了
害我一直傻傻的測試是不是刪到資料檔了
後來搜尋一下發現,竟然是自動被清除的

原來Hbase預設是把資料檔放在/tmp裡面
所以重開機就被清掉了




在hbase/conf目錄裡有個預設組態檔hbase-default.xml
裡面的hbase.rootdir預設為file:///tmp/hbase-${user.home}/hbase
除非特地去備份它,不然重開機資料就全消失了
不想備份就直接把資料檔存放到永續存放的磁碟空間

編輯hbase-site.xml
$ vim hbase/conf/hbase-site.xml
value可以自由設定路徑,description非必填,name則是固定的屬性名稱
<property>
    <name>hbase.rootdir</name>
    <value>file:///${user.home}/hbase/dbstored</value>
     <description>The directory shared by region servers.
      Should be fully-qualified to include the filesystem to use.
     </description>
  </property>
hbase系統讀取設定檔的優先權順序是hbase-site.xml高於hbase-default.xml,若想保留原本的預設值,一般是建議直接放到hbase-site.xml就可以了

[Hbase]hadoop+hbase+thrift重啟指令流程


$ /opt/hadoop/bin/start-all.sh
$ /opt/hbase/bin/start-hbase.sh
$ /opt/hbase/bin/hbase-daemon.sh start thrift

[Hbase]使用PHP存取Hbase


首先必須與Hbase建立連線
<?php
header ("Content-Type:text/html; charset=utf-8");
//include thrift路徑
$GLOBALS['THRIFT_ROOT'] = '/var/www/hbase/thrift';
require_once( $GLOBALS['THRIFT_ROOT'].'/Thrift.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'].'/packages/Hbase/Hbase.php' );


//建立連線
$socket = new TSocket( 'YourIP', 9090 );
$socket->setSendTimeout( 10000 ); // Ten seconds
$socket->setRecvTimeout( 20000 ); // Twenty seconds
$transport = new TBufferedTransport( $socket );
$protocol = new TBinaryProtocol( $transport );
$client = new HbaseClient( $protocol );

//啟動
$transport->open();

//程式碼寫在這邊

$transport->close();
?>
接下來是對資料庫會使用到的基本操作

1.新增資料表
        //定義columns 的物件結構,一個Column需要new出一個ColumnDescriptor物件
        $columns = array(
                new ColumnDescriptor( array(
                        'name' => 'name:'
                ) ),
                new ColumnDescriptor( array(
                        'name'=> 'scores:',
                ) )
        );


        $t = "test";//Table name
        echo( "creating table: {$t}\n" );
        try
        {
                $client->createTable( $t, $columns );
        }
        catch ( AlreadyExists $ae )
        {
                echo( "WARN: {$ae->message}\n" );
        }

2.查閱所有資料表
        echo( "全部的資料表有...\n" );
        $tables = $client->getTableNames();
        sort( $tables );
        foreach ( $tables as $name )
        {
                echo $name."\n";
        }
3.列出 table內的column family
        $t = "test";//資料表名稱
        echo( "column families in {$t}:\n" );
        $descriptors = $client->getColumnDescriptors( $t );
        asort( $descriptors );
        foreach ( $descriptors as $col )
        {
                echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" );
        }
4.新增資料
        $t = "test"; //Table name
        $row = "abc"; //row name
        $valid = "apple";
        $mutations = array(
        new Mutation( array(
                'column' => 'nama:',
                'value' => $valid
                 ) )
        );
        $client->mutateRow( $t, $row, $mutations );
        echo "新增成功";
5.讀取資料
        include "ThriftLib.php";
        $table_name = 'test';//Table name
        $row_name = 'abc';//row name
        $fam_col_name = 'name:';//column name
        $arr = $client->get($table_name, $row_name , $fam_col_name);
        foreach ( $arr as $k=>$v )
        {
                //// $k = TCell物件
                echo "value = {$v->value} , <br> ";
                echo "timestamp = {$v->timestamp} <br>";
        }
6.刪除資料表
        $name = "test"; //要刪除的資料表
        if($client->isTableEnabled($name))
        {
                echo "關閉".$name."資料表\n";
                $client->disableTable($name);
        }
        echo "刪除中...\n";
        $client->deleteTable($name);
        echo "刪除完成";
有問題再留言吧@@