代码如下:
<?php
class Monitor_Hbase{
private $rest_host = "http://10.99.90.39:8130/";//rest地址
private $ch;
function __construct(){ }
function post($url, $data){
$ch = curl_init();
$header_str = array("Content-Type: application/json");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'put');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_HTTPHEADER , $header_str );
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1); $output = curl_exec($ch);
curl_close($ch);
return $output;
} function rest($url){
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Accept: application/json"));
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
} function get_range_data($table, $startRow, $endRow){
$startRow = base64_encode($startRow);
$endRow = base64_encode($endRow);
$data = sprintf('{"startRow":"%s","endRow":"%s"}',$startRow,$endRow);
$output = $this->post($this->rest_host.$table."/scanner",$data);
$output_array = explode("\r\n",$output);
$location = "";
foreach($output_array as $item){
if(strpos($item, ":")>-1){
$tmp = explode(": ",$item);
if($tmp[0] === 'Location'){
$location = $tmp[1];
} }
}
$result = $this->rest($location);
$f = ($this->prase_data(json_decode($result, true)));
return $f;
} function get_data($table, $key, $coloum){
$param = urlencode($table).'/'.urlencode($key);
if(!empty($coloum)){
$param = $param.'/'.urlencode($coloum);
}
$url = $this->rest_host.$param;
$output = $this->rest($url);
return $this->prase_data(json_decode($output,true));
} function prase_data($raw){
$result = array();
$rows = $raw['Row'];
//$raw = $raw['Row'][0]['Cell'];
foreach($rows as $row){
$key = base64_decode($row['key']);
foreach ($row['Cell'] as $item){
$col = base64_decode($item['column']);
$value = base64_decode($item['$']);
$result[$key][$col]=$value;
}
}
return $result;
} } $a = new Monitor_Hbase();
$b = $a->get_range_data('map_mobile_lighttpd_slowcount','mobile_all_201411111005','mobile_all_201411111030');
//$b = $a->get_data("nuomi_lixian_daily_count","20141026_CMNET_2G_Android_5.2.1_*","");
//$b = $a->get_data("nuomi_lixian_daily_count","20141016_ALL_CMNET_2G_Android_5.2.0","");
var_dump($b);