Hbase 操作记录[php thrift]

Hbase 操作记录[php thrift]
scanscanner:一个对象,可以通过下列方法直接获得:

public function scannerOpen($tableName, $startRow, $columns);

public function scannerOpenWithStop($tableName, $startRow, $stopRow, $columns);

public function scannerOpenWithPrefix($tableName, $startAndPrefix, $columns);

public function scannerOpenTs($tableName, $startRow, $columns, $timestamp);

public function scannerOpenWithStopTs($tableName, $startRow, $stopRow, $columns, $timestamp);

 

此时需要提供表名,开始行,结束行,开始行前缀,版本号等等条件。开始行和结束行为相同时可以定位到唯一一条,columns可以为空数组,开始行和结束行可以为不存在的rowkey,比如我的表中rowkey结构是userid_timestamp,我就可以用userid_0和userid_9999999999这样的rowkey取得这个用户的所有数据。
scanner也可以通过:

scannerOpenWithScan($tableName, $scan);

方法获得,此时你需要自己new一个$scan对象,$scan是一个定义$startRow,$stopRow,$filter(过滤器)等的对象,此时你可以更灵活的使用$filter来扫描表,但是最好要用$startRow,$stopRow限定范围,否则thrift很容易超时。

例子:($hbase是一个hbase的连接对象)
$type = 0;

$filter =”SingleColumnValueFilter(‘m’,’type’,=,’binary:{$type}’)”;

$scan = new TScan();

$scan->filterString=$filter;

$scan->startRow=$uid.’_0′;

$scan->stopRow=$uid.’_999999999′;

$scanner = $hbase->scannerOpenWithScan($this->tablename, $scan);

$get_arr = $hbase->scannerGetList($scanner,Config::MAXN_MSG);

$hbase->scannerClose($scanner);

此处我用 $scan->startRow=$uid.’_0′;        $scan->stopRow=$uid.’_999999999′;    限定了扫描的范围,避免整表扫描
注意事项:
SingleColumnValueFilter过滤器在手册里的说明参数位置是错误的,正确的应该为:
Syntax: SingleColumnValueFilter(‘<family>’, ‘<qualifier>’, <compare operator>, ‘<comparator>’, ‘<filterIfColumnMissing_boolean>, <latest_version_boolean>)
Syntax: SingleColumnValueFilter(‘<family>’, ‘<qualifier>, <compare operator>, ‘<comparator>’)
Example: “SingleColumnValueFilter (‘FamilyA’, ‘Column1’, <=, ‘abc’,‘true, false)”
Example: “SingleColumnValueFilter (‘FamilyA’, ‘Column1’, <=, ‘abc’)”

 

注意使用时<family>是不用带冒号的。
下面的方法是scanner对象的简化使用方法

public function get($tableName, $row, $column);

public function getVer($tableName, $row, $column, $numVersions);

public function getVerTs($tableName, $row, $column, $timestamp, $numVersions);

public function getRow($tableName, $row);

public function getRowWithColumns($tableName, $row, $columns);

public function getRowTs($tableName, $row, $timestamp);

public function getRowWithColumnsTs($tableName, $row, $columns, $timestamp);

public function getRows($tableName, $rows);

public function getRowsWithColumns($tableName, $rows, $columns);

public function getRowsTs($tableName, $rows, $timestamp);

public function getRowsWithColumnsTs($tableName, $rows, $columns, $timestamp);

下面是一些数据库操作方法:
public function showTable() {

$tables = $this->hbase->_dbh->getTableNames();

asort($tables);

foreach($tables as $name)        {

echo $name.”<br>”;

}

}

public function showColumnFamilies($tablename) {

echo(“column families in {$tablename}:\n”);

$descriptors = $this->hbase->_dbh->getColumnDescriptors($tablename);

asort($descriptors);

foreach($descriptors as $col)        {

echo(“column:{$col->name},maxVer:{$col->maxVersions}<br>”);

}

}

public function getallcolumn($uid)    {

$arr = $this->hbase->_dbh->getRow(self::user_table , $uid);

}
public function getallmessage($uid)    {

$columns = array (‘column’ => ‘bhvr:’);

$arr = $this->hbase->_dbh->getRowsWithColumns($t, array($uid), $columns);

echo ‘<pre>’;        print_r($arr);

}

public function scan1($tableName)    {

$startRow = 13222120124960963;

$scan = $this->hbase->_dbh->scannerOpen($tableName, $startRow, $columns);

$nbRows = 1;

$arr = $this->hbase->_dbh->scannerGetList($scan, $nbRows);

var_dump(‘count of result :’.count($arr));

foreach ($arr as $k => $TRowResult) {

echo ‘<pre>’;

print_r($TRowResult);

}

$this->hbase->_dbh->scannerClose($scan);

}

public function createTable($tablename,$columnFamilies)    {

foreach ($columnFamilies as $cf) {

$columns[] = new ColumnDescriptor(array(‘name’ => $cf.’:’));

}

echo(“creating table: {$tablename}\n”);

try        {

$this->hbase->_dbh->createTable( $tablename, $columns );

}        catch (AlreadyExists $ae)        {

echo( “WARN: {$ae->message}\n” );

}

}

public function delTable($tablename)    {

if($this->hbase->_dbh->isTableEnabled($tablename))        {

echo “关闭”.$tablename.”资料表\n”;

$this->hbase->_dbh->disableTable($tablename);

}

echo “刪除中…\n”;

$this->hbase->_dbh->deleteTable($tablename);

echo “刪除完成”;

}