public function __construct($params) { $servers = $params['servers']; $sentTimeout = empty($params['send_timeout']) ? 5000 : $params['send_timeout']; //默认5秒 $recvTimeout = empty($params['recv_timeout']) ? 5000 : $params['recv_timeout']; //默认5秒 $succ = false; $i = -1; $retry = 3; //取消随机选择thrft server,而是按照优先级选择,如果专门提供给web的server挂了 //再选择爬虫使用的server //shuffle($servers); while (!$succ && $retry--) { $i = ($i + 1) % count($servers); $server = $servers[$i]; try { $socket = new TSocket($server['host'], $server['port']); $socket->setSendTimeout($sentTimeout); $socket->setRecvTimeout($recvTimeout); $this->_transport = new TBufferedTransport($socket); $protocol = new TBinaryProtocol($this->_transport); parent::__construct($protocol); $this->_transport->open(); $succ = true; } catch (Exception $e) { if ($retry == 1) { $logParams = array('host' => $server['host'], 'port' => $server['port'], 'sent_timeout' => $sentTimeout, 'recv_timeout' => $recvTimeout, 'message' => $e->getMessage()); $ci =& get_instance(); $ci->log->log('error', 'connect to thrift server failed', $logParams); throw $e; } } } }
require_once 'thrift/lib/HBase/Types.php'; use Thrift\Transport\TSocket; use Thrift\Transport\TBufferedTransport; use Thrift\Protocol\TBinaryProtocol; use Hbase\HbaseClient; //use Hbase\ColumnDescriptor; use Hbase\Mutation; use Hbase\TScan; $socket = new TSocket('localhost', 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); try { $transport->open(); $table = 'note'; $filter = "QualifierFilter(>=, 'binary:1500')"; // greater than 1500 $scan = new TScan(array('startRow' => 'rowkey-1', 'stopRow' => 'rowkey-2', 'filterString' => $filter, 'sortColumns' => true)); $scanid = $client->scannerOpenWithScan($table, $scan, null); $rowresult = $client->scannerGet($scanid); // print_r($rowresult); echo "\nrow: {$rowresult[0]->row}, cols: \n\n"; // 위에 TScan에 sortColumns 옵션을 안주면 $rowresult[0]->columns 로 접근. $values = $rowresult[0]->sortedColumns; // asort($values); foreach ($values as $k => $v) { echo " {$k} => {$v->value}\n";
use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TBufferedTransport; use Thrift\Transport\TSocket; include_once 'vendor/autoload.php'; // "vendor/apache/Hbase/THBaseService.php", // "vendor/apache/Hbase/Types.php", include_once "gen-php/Hbase/Hbase.php"; include_once "gen-php/Hbase/Types.php"; $socket = new TSocket('222.73.243.182', '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(); echo nl2br("listing tables...\n"); $tables = $client->getTableNames(); sort($tables); foreach ($tables as $name) { echo nl2br(" found: {$name}\n"); } $columns = array(new ColumnDescriptor(array('name' => 'entry:', 'maxVersions' => 10)), new ColumnDescriptor(array('name' => 'unused:'))); $t = "table1"; echo "creating table: {$t}\n"; try { $client->createTable($t, $columns); } catch (AlreadyExists $ae) { echo "WARN: {$ae->message}\n"; }
require_once $GLOBALS['THRIFT_ROOT'] . '/Types.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Hbase.php'; use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TSocket; use Thrift\Transport\TSocketPool; use Thrift\Transport\TFramedTransport; use Thrift\Transport\TBufferedTransport; use Hbase\HbaseClient; //define host and port $host = '192.168.56.56'; $port = 9090; $socket = new Thrift\Transport\TSocket($host, $port); $transport = new TBufferedTransport($socket); $protocol = new TBinaryProtocol($transport); // Create a calculator client $client = new HbaseClient($protocol); $transport->open(); //echo "Time: " . $client -> time(); $tables = $client->getTableNames(); sort($tables); foreach ($tables as $name) { echo $name . "\r\n"; } //create a fc and then create a table $columns = array(new \Hbase\ColumnDescriptor(array('name' => 'id:', 'maxVersions' => 10)), new \Hbase\ColumnDescriptor(array('name' => 'name:')), new \Hbase\ColumnDescriptor(array('name' => 'score:'))); $tableName = "student"; /* try { $client->createTable($tableName, $columns); } catch (AlreadyExists $ae) { var_dump( "WARN: {$ae->message}\n" );
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 'thrift/lib/HBase/Hbase.php'; require_once 'thrift/lib/HBase/Types.php'; use Thrift\Transport\TSocket; use Thrift\Transport\TBufferedTransport; use Thrift\Protocol\TBinaryProtocol; use Hbase\HbaseClient; use Hbase\Mutation; use Hbase\TScan; $socket = new TSocket('localhost', 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); try { $transport->open(); $table = 'tablename'; $rowkey = 'rowkey'; $column = 'columnfamily:column'; print_r($client->getRow($table, $rowkey, null)); print_r($client->get($table, $rowkey, $column, null)); $transport->close(); } catch (TException $e) { print 'TException: ' . $e->__toString() . ' Error: ' . $e->getMessage() . "\n"; }
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 'thrift/lib/HBase/Hbase.php'; require_once 'thrift/lib/HBase/Types.php'; use Thrift\Transport\TSocket; use Thrift\Transport\TBufferedTransport; use Thrift\Protocol\TBinaryProtocol; use Hbase\HbaseClient; //use Hbase\ColumnDescriptor; use Hbase\Mutation; use Hbase\TScan; $socket = new TSocket('localhost', 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); try { $transport->open(); // column family는 character 하나로 주는 게 좋지만, 여기서는 예제로. $data = array(new mutation(array('column' => 'columnfamily:column', 'value' => 'column value'))); $client->mutaterow('table_name', 'rowkey-1', $data, null); $transport->close(); } catch (TException $e) { print 'TException: ' . $e->__toString() . ' Error: ' . $e->getMessage() . "\n"; }