thrift api 使用: HTML

在這一篇文章中,
我們將介紹如何使用HTML透過PHP讀取hbase,

我們安裝的目錄結構為:
hadoop                     /opt/hadoop
hbase                        /opt/hbase
網頁根目錄             /var/www/
hbase的php目錄      /var/www/hbase
thrift php                  /var/www/hbase/thrift
php存放目錄           /var/www/php

測試程式之前,請先確定hbase , hadoop 都有正常運作中
$ bin/hbase thrift start 尚在執行

以下是我們的範例程式(example.php):

    <!DOCTYPE html>
    <html>
    <meta charset="utf-8">
    <head>
    <script>
    function getUser()
    {
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
        if(xmlhttp != null)
        {
            xmlhttp.onreadystatechange = stateChanged;
            xmlhttp.open("Get","php/example.php",true);
            xmlhttp.send();
        }
    }
    else
    {
        alert('does not support XMLHttpRequest');
    }
    }

    function stateChanged()
    {
    if(xmlhttp.readyState == 4)
    {
        if(xmlhttp.status = 200)
        {
            var point  = xmlhttp.responseText;
            Update_User(point);
        }
    }
    }

    function Update_User(point)
    {
    document.write("JSON : <br>" + point + "<br> <br>");

    var cart = JSON.parse(point);
    var key, i = 0;
    for(i=0 ; i<cart.length; i++) {
        document.write("USER: " + i + ", ");
        document.write("MAC: " + cart[i].User + ", ");
        document.write("X: " + cart[i].Xlocation + ", ");
        document.write("Y: " + cart[i].Ylocation + "<br>");
    }
    }

    </script>

    </head>

    <body>
    <script>
    getUser();
    setInterval(function(){getUser()}, 5000);
    </script>
    </body>

    </html>

在example.html這個網頁中,
會定期讀取UserTable的資料, 並且顯示於網頁上,
在網頁的一開始, 我們先宣告第一個javascript function: getUser(),
在這個function中, 我們宣告PHP(example.php)的位置,
並且透過xmlhttp的物件開啟example.php,

function getUser()
{
    if(window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
        if(xmlhttp != null)
        {
            xmlhttp.onreadystatechange = stateChanged;
            xmlhttp.open("Get","php/example.php",true);
            xmlhttp.send();
        }
    }
    else
    {
        alert('does not support XMLHttpRequest');
    }
}

在stateChanged()中, 則是利用變數(var point)儲存JSON字串,
並且將所得到的字串放入Update_User()中,

function stateChanged()
{
    if(xmlhttp.readyState == 4)
    {
        if(xmlhttp.status = 200)
        {
            var point  = xmlhttp.responseText;
            Update_User(point);
        }
    }
}

最後, 在Update_User()程式中,
我們將所得到的JSON字串先印出來,
並且根據字串的大小以FOR迴圈印出所讀取的內容,
注意, 由於是HTML的格式, 換行符號是: <br>
而不是我們常用的: '\n'

function Update_User(point)
{
    document.write("JSON : <br>" + point + "<br> <br>");
 
    var cart = JSON.parse(point);
    var key, i = 0;
    for(i=0 ; i<cart.length; i++) {
        document.write("USER: " + i + ", ");
        document.write("MAC: " + cart[i].User + ", ");
        document.write("X: " + cart[i].Xlocation + ", ");
        document.write("Y: " + cart[i].Ylocation + "<br>");
    }
}

在HTML的body中,
我們每隔5秒(5000ms)執行一次getUser()指令,
將所得到的結果顯示於網頁上:

<body>
<script>
getUser();
setInterval(function(){getUser()}, 5000);
</script>
</body>


以下是在網頁上的顯示結果:
JSON :
[{"User":"3C:E0:72:B6:29:B9","Xlocation":"-1.6371960645819712","Ylocation":"-0.6972200627495914"},{"User":"5C:FF:35:77:43:F4","Xlocation":"55.018162315153376","Ylocation":"38.813627069491766"},{"User":"B8:B4:2E:32:54:2D","Xlocation":"4.5085225496644235","Ylocation":"7.778994967910233"},{"User":"C4:43:8F:F3:EB:D6","Xlocation":"52.80050407884881","Ylocation":"23.093696604459726"},{"User":"DC:C7:93:DA:2B:7C","Xlocation":"9.865865062060452","Ylocation":"26.721484611594647"}]

USER: 0, MAC: 3C:E0:72:B6:29:B9, X: -1.6371960645819712, Y: -0.6972200627495914
USER: 1, MAC: 5C:FF:35:77:43:F4, X: 55.018162315153376, Y: 38.813627069491766
USER: 2, MAC: B8:B4:2E:32:54:2D, X: 4.5085225496644235, Y: 7.778994967910233
USER: 3, MAC: C4:43:8F:F3:EB:D6, X: 52.80050407884881, Y: 23.093696604459726
USER: 4, MAC: DC:C7:93:DA:2B:7C, X: 9.865865062060452, Y: 26.721484611594647

參考資料:
http://blog.wu-boy.com/2011/04/%E4%BD%A0%E4%B8%8D%E5%8F%AF%E4%B8%8D%E7%9F%A5%E7%9A%84-json-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%B4%B9/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
http://www.wibibi.com/info.php?tid=346

留言

熱門文章

LTE筆記: RSRP, RSSI and RSRQ

[WiFi] WiFi 網路的識別: BSS, ESS, SSID, ESSID, BSSID

LTE筆記: 波束成型 (beamforming) 和天線陣列