reference: http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html
192.168.122.11 192.168.122.10
+------------+ +-------------+
|master01 | |smaster |
| | | |
| | | |-+
| | | | |
+------+-----+ +-------+-----+ | +----NAT(@KVM)
| | NFS: |
-------+-------------------+------------------+
| |
+------+-----+ +-------+-----+
|knode020 | |knode021 |
|php & httpd | |php & httpd |
| | | |
| | | |
+------------+ +-------------+
192.168.122.20 192.168.122.21
1. CDH4 installation
skipped
2. Hypertable 0.9.6.4 installlation
skipped
the binary package for cdh4 : https://groups.google.com/forum/?fromgroups=#!msg/hypertable-user/dmQhx_i7HfQ/YE32E9Omv3EJ
3. php and httpd preparation
[root@knode020 rest]# yum install php mysql mysql-server php-mysql httpd
** mysql* is used for trace and debug, php and httpd is just work for REST API for HT
[root@knode020 rest]# chkconfig httpd on
[root@knode020 rest]# service httpd start
4. REST API php code for Hyptertable
the most important source in api-ht.php as follow:
class API extends REST {
public $data = "";
const DB_SERVER = "localhost";
const DB = "sys";
private $db = NULL;
private $ns = NULL;
public function __construct(){
parent::__construct();
$this->dbConnect();
}
public function __destruct(){
if($this->ns)
$this->db->namespace_close($this->ns);
}
private function dbConnect(){
$this->db = new Hypertable_ThriftClient(self::DB_SERVER, 38080);
if($this->db)
$this->ns = $this->db->namespace_open(self::DB);
}
public function processApi(){
$func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
if((int)method_exists($this,$func) > 0)
$this->$func();
else
$this->response('',404);
}
private function scanner(){
if($this->get_request_method() != "GET"){
$this->response('',406);
}
$scanner = $this->db->scanner_open($this->ns, "RS_METRICS", new Hypertable_ThriftGen_ScanSpec(array('rot_limit' => 1000)));
$cells = $this->db->scanner_get_cells_as_arrays($scanner);
if(empty($cells)){
$this->response('', 204); // If no records "No Content" status
}
$reault = array();
foreach($cells as $cell)
{
print_r(json_encode($cell));
$result[] = $cell;
}
$this->response($this->json($result), 200);
}
private function hql(){
if($this->get_request_method() != "GET"){
$this->response('',406);
}
$limit = $this->_request['limit'];
if($limit > 0)
$cells = $this->db->hql_query_as_arrays($this->ns, "select * from RS_METRICS limit $limit");
else
$cells = $this->db->hql_query_as_arrays($this->ns, "select * from RS_METRICS ");
if(empty($cells->cells)){
$this->response('', 204); // If no records "No Content" status
}
$reault = array();
foreach($cells->cells as $cell)
{
$result[] = $cell;
}
$this->response($this->json($result), 200);
}
private function json($data){ if(is_array($data)){
return json_encode($data);
}
}
}
5. test by curl
[root@knode020 rest]# curl -i "http://localhost/rest/api-ht.php?rquest=hql&limit=2"
[root@knode020 rest]# curl -i http://localhost/rest/api-ht.php?rquest=hql
[root@knode020 rest]# curl -i http://localhost/rest/api-ht.php?rquest=scanner
Appendix A: mysql REST API
Query User
[root@knode020 rest]# curl -i "http://localhost/rest/api.php?rquest=users"
HTTP/1.1 200 OK
Date: Wed, 23 Jan 2013 01:44:31 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Content-Length: 166
Connection: close
Content-Type: application/json
[{"user_id":"1","user_fullname":"Sean","user_email":"seanlo0805@gmail.com"},{"user_id":"2","user_fullname":"Jewel","user_email":"testjewel@yahoo.com.tw"}]
Login User
[root@knode020 rest]# curl -i -X POST -d "email=seanlo0805@gmail.com" -d "pwd=sean" http://localhost/rest/api.php?rquest=login
HTTP/1.1 200 OK
Date: Wed, 23 Jan 2013 03:37:05 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Content-Length: 83
Connection: close
Content-Type: application/json
{"user_id":"1","user_fullname":"Sean","user_email":"seanlo0805@gmail.com"}
Delete User
root@knode020 rest]# curl -i -X DELETE "http://localhost/rest/api.php?rquest=deleteUser&id=1"
HTTP/1.1 200 OK
Date: Wed, 23 Jan 2013 05:40:34 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Content-Length: 61
Connection: close
Content-Type: application/json
{"status":"Success","msg":"Successfully one record deleted."}
[root@knode020 rest]# curl -i "http://localhost/rest/api.php?rquest=users"
HTTP/1.1 200 OK
Date: Wed, 23 Jan 2013 05:41:14 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Content-Length: 82
Connection: close
Content-Type: application/json
[{"user_id":"2","user_fullname":"Jewel","user_email":"testjewel@yahoo.com.tw"}]
Appendix B: trace and debug REST API for hadoop
[QUERY]
curl -i "http://master01:50070/webhdfs/v1/hypertable?op=LISTSTATUS"
curl -i "http://master01:50070/webhdfs/v1/hypertable?op=GETFILESTATUS"
curl -i "http://master01:50070/webhdfs/v1/hypertable?op=GETHOMEDIRECTORY"
[CREATE FILE]
curl -i -X PUT "http://master01:50070/webhdfs/v1/hypertable/abc.txt?op=CREATE&user.name=root"
curl -i -X PUT "http://knode020:50075/webhdfs/v1/hypertable/abc.txt?op=CREATE&namenoderpcaddress=master01:9000&user.name=root"
[OPEN FILE]
curl -i -L "http://knode020:50075/webhdfs/v1/hypertable/abc.txt?op=OPEN&user.name=root&namenoderpcaddress=master01:9000"
curl -i -L "http://master01:50070/webhdfs/v1/hypertable/abc.txt?op=OPEN"
Appendix C: json 2 xml
<?php
function array_to_xml(array $arr, SimpleXMLElement $xml)
{
foreach ($arr as $k => $v) {
is_array($v)
? array_to_xml($v, $xml->addChild($k))
: $xml->addChild($k, $v);
}
return $xml;
}
if (!isset($GLOBALS['THRIFT_ROOT']))
$GLOBALS['THRIFT_ROOT'] = "/opt/hypertable/current/lib/php";
require_once $GLOBALS['THRIFT_ROOT'].'/ThriftClient.php';
$client = new Hypertable_ThriftClient("localhost", 38080);
$namespace = $client->namespace_open("sys");
//array('revs'=> 1);
$scanner = $client->scanner_open($namespace, "RS_METRICS", new
Hypertable_ThriftGen_ScanSpec(array('rot_limit' => 1000)));
$cells = $client->scanner_get_cells_as_arrays($scanner);
@header("Content-Type: text/xml");
$result=array();
foreach($cells as $cell)
{
$result[] = $cell;
}
print array_to_xml($result, new SimpleXMLElement('<root/>'))->asXML();
$client->namespace_close($namespace);
?>
沒有留言:
張貼留言