2013年1月24日 星期四

Implementation of REST API for Hypertable


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);
?>



2013年1月15日 星期二

auto_ptr and smart_ptr




//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <memory>
#include <string>
#include <iostream>
//---------------------------------------------------------------------------
class A
{
        std::string m_strAAA;
public:
        A():m_strAAA(""),x(0){std::cout <<"A:" << m_strAAA << std::endl;}
        A(const A &a):m_strAAA(a.m_strAAA),x(a.x){std::cout <<"A2:" << m_strAAA << std::endl;}
        A(const std::string &str):m_strAAA(str),x(0){std::cout <<"A3:" << m_strAAA << std::endl;}
        void print(){std::cout <<"[" << m_strAAA << "]"<< std::endl;}
        ~A(){std::cout <<"~A:" << m_strAAA << std::endl;}
        const std::string & GetAAA(){return m_strAAA;}
        int x;
};

class RC
{
    private:
    int count; // Reference count

    public:
    RC():count(0){}   // <-- initialize counter
    void AddRef()
    {
        // Increment the reference count
std::cout << "add_ref" << std::endl;
        count++;
    }

    int Release()
    {
std::cout << "release" << std::endl;
        // Decrement the reference count and
        // return the reference count.
        return --count;
    }
};
template < typename T > class SP
{
private:
    T*    pData;       // pointer
    RC* reference; // Reference count

public:
    SP() : pData(0), reference(0)
    {
        // Create a new reference
        reference = new RC();
        // Increment the reference count
        reference->AddRef();
    }

    SP(T* pValue) : pData(pValue), reference(0)
    {
        // Create a new reference
        reference = new RC();
        // Increment the reference count
        reference->AddRef();
    }

    SP(const SP<T>& sp) : pData(sp.pData), reference(sp.reference)
    {
        // Copy constructor
        // Copy the data and reference pointer
        // and increment the reference count
        reference->AddRef();
    }

    ~SP()
    {
std::cout << "content will be destructed!! [" << pData->GetAAA() << "]" << std::endl;
        // Destructor
        // Decrement the reference count
        // if reference become zero delete the data
        if(reference->Release() == 0)
        {
            delete pData;
            delete reference;
        }
    }

    T& operator* ()
    {
        return *pData;
    }

    T* operator-> ()
    {
        return pData;
    }
    
    SP<T>& operator = (const SP<T>& sp)
    {
        // Assignment operator
        if (this != &sp) // Avoid self assignment
        {
            // Decrement the old reference count
            // if reference become zero delete the old data
            if(reference->Release() == 0)
            {
                delete pData;
                delete reference;
            }

            // Copy the data and reference pointer
            // and increment the reference count
            pData = sp.pData;
            reference = sp.reference;
            reference->AddRef();
        }
        return *this;
    }
};
int main(int argc, char* argv[])
{
/*test auto_prt*/
        std::auto_ptr<A> aa;
        A a;
        {
        std::auto_ptr<A> aa1(new A("aa1"));
        aa1->print();
        aa=aa1;
        if(aa1.get()==0)
                std::cout << "aa1 ptr transfer to aa !!" << std::endl;
        }

        {
        std::auto_ptr<A> aa2(new A("aa2"));
        aa2->print();
        if(aa2.get()==0)
                std::cout << "aa2 ptr transfer to aa !!" << std::endl;
        }


/*test smart pointer*/
    SP<A> p(new A("Scott"));
    p->print();
    {
        SP<A> q = p;
        q->print();

        SP<A> r;
        r = p;
        r->print();
        
        // Destructor of q will be called here..
        // Destructor of r will be called here..
    }
    p->print();


    // Destructor of p will be called here 
    // and A pointer will be deleted



        return 0;
}
//---------------------------------------------------------------------------

結果顯示
/*test auto_prt*/
A:
A3:aa1
[aa1]
aa1 ptr transfer to aa !!
A3:aa2
[aa2]
~A:aa2

/*test smart pointer*/
A3:Scott
add_ref
[Scott]
add_ref
[Scott]
add_ref
release
add_ref
[Scott]
content will be destructed!! [Scott]  --> q
release
content will be destructed!! [Scott]  --> r
release
[Scott]
content will be destructed!! [Scott]  --> p

/* after return 0; */
release
~A:Scott
~A:
~A:aa1



reference: http://www.codeproject.com/Articles/15351/Implementing-a-simple-smart-pointer-in-c
http://caterpillar.onlyfun.net/Gossip/CppGossip/autoPtr.html


2012年12月22日 星期六

JRE install in CentOS6.2

You may be need a java environment after you Installed a CentOS . Some website with Java but your browser didn't support it by default.
1) download JRE and install it
    jre-XuYY-linux-x64.rpm from http://java.com/en/download/index.jsp
    #my download file is :   jre-7u10-linux-x64.rpm
    yum localinstall  jre-7u10-linux-x64.rpm -y
2) environment setting
   vi /etc/profile
    :
    :

   export JAVA_HOME=/usr/java/jre1.7.0_10
   export PATH=$JAVA_HOME/bin:$PATH
   export CLASSPATH=$JAVA_HOME/lib:.


3) Java library for Browser


   [root@localhost Downloads]# find / -name libnpjp2.so
       /usr/java/jre1.7.0_10/lib/amd64/libnpjp2.so

   [root@localhost Downloads]# mkdir -p /opt/google/chrome/plugins   #(google chrome)
   [root@localhost Downloads]# cd /opt/google/chrome/plugins

   [root@localhost Downloads]# mkdir -p /usr/lib/mozilla/plugins   #(mozilla firefox)
   [root@localhost Downloads]# cd /usr/lib/mozilla/plugins

   [root@localhost plugins]# ln -s /usr/java/jre1.7.0_10/lib/amd64/libnpjp2.so


DONE

文章分類