2014年1月24日 星期五

圓剛 H830M 三頻電視棒

H830M-01

準備回家過年了,剛好老鄉只有一台電視可用,想著想著,就乾脆買了一隻電視棒來用 XD 原本還想在 raspberry pi 架個 streaming server 的,也著手找尋有提供 Linux driver 的電視棒,但不知不覺發現有支援 Mac OS X 的,剛好有特價!?不知不覺手就滑了 XDDD

試了一下,這支電視棒的最大缺點可能是沒有遙控器 XD 但如果是重度的 Apple 派,或許可以用 Apple Remote ?

H830M-02

至於優點就是三頻(數位電視、有線電視、廣播)、體積小、支援 Mac OS X 吧!另外有送一隻小天線。哪天在外頭沒網路又想看電視、聽廣播時,可以拿出來用用吧!


H830M-04

[MongoDB] 使用 PyMongo 把玩 MapReduce @ Ubuntu 12.04

稍微接觸了一下 MongoDB 後,對此感到無比的興奮?與 Hadoop 相比,感覺上手度滿高的,不過,使用 PyMongo 的情況下,還是需要比較適合熟悉 Javascript 的人,因為 Mapper 跟 Reducer 的撰寫仍是用 Javascript 的,更正確來說是 BSON's JavaScript code type.

既然是 MapReduce,那就先來個 word count 吧!說真的,word count 也是我學 Hadoop 時跑的第一個範例。

簡單試了一下(文字來源:Taiwan wiki - Names):

$ python import.py -
{"field":"There are various names for the island of Taiwan in use today, derived from explorers or rulers by each particular period. The former name Formosa (福爾摩沙) dates from 1544, when Portuguese sailors sighted the main island of Taiwan and named it Ilha Formosa, which means \"Beautiful Island\".[21] In the early 17th century, the Dutch East India Company established a commercial post at Fort Zeelandia (modern Anping, Tainan) on a coastal islet called \"Tayouan\" in the local Siraya language; the name was later extended to the whole island as \"Taiwan\".[22] Historically, \"Taiwan\" has also been written as 大灣, 臺員, 大員, 臺圓, 大圓 and 臺窩灣."}
{"field":"The official name of the state is the \"Republic of China\"; it has also been known under various names throughout its existence. Shortly after the ROC's establishment in 1912, while it was still located on the Asian mainland, the government used the abbreviation \"China\" (\"Zhongguó\") to refer to itself. During the 1950s and 1960s, it was common to refer to it as \"Nationalist China\" (or \"Free China\") to differentiate it from \"Communist China\" (or \"Red China\").[23] It was present at the UN under the name \"China\" until 1971, when it lost its seat to the People's Republic of China. Since then, the name \"China\" has been commonly used internationally to refer only to the People's Republic of China.[24] Over subsequent decades, the Republic of China has become commonly known as \"Taiwan\", after the island that composes most of its territory. The Republic of China participates in most international forums and organizations under the name \"Chinese Taipei\" due to diplomatic pressure from the PRC. For instance, it is the name under which it has competed at the Olympic Games since 1984, and its name as an observer at the World Health Organization.[25]"}
Import:  2
[ObjectId('52e265a69c3fe514be2534fe'), ObjectId('52e265a69c3fe514be2534ff')]


$ python map-reduce-word-count.py --show-result --delete-result
Collection(Database(MongoClient('localhost', 27017), u'db'), u'tmp_2014-01-24_131025')
{u'_id': u'1544', u'value': {u'count': 1.0}}
{u'_id': u'17th', u'value': {u'count': 1.0}}
{u'_id': u'1912', u'value': {u'count': 1.0}}
{u'_id': u'1950s', u'value': {u'count': 1.0}}
{u'_id': u'1960s', u'value': {u'count': 1.0}}
{u'_id': u'1971', u'value': {u'count': 1.0}}
{u'_id': u'1984', u'value': {u'count': 1.0}}
{u'_id': u'21', u'value': {u'count': 1.0}}
{u'_id': u'22', u'value': {u'count': 1.0}}
{u'_id': u'23', u'value': {u'count': 1.0}}
{u'_id': u'24', u'value': {u'count': 1.0}}
{u'_id': u'25', u'value': {u'count': 1.0}}
{u'_id': u';', u'value': {u'count': 1.0}}
{u'_id': u'Anping', u'value': {u'count': 1.0}}
{u'_id': u'Asian', u'value': {u'count': 1.0}}
{u'_id': u'Beautiful', u'value': {u'count': 1.0}}
{u'_id': u'China', u'value': {u'count': 12.0}}
{u'_id': u'Chinese', u'value': {u'count': 1.0}}
{u'_id': u'Communist', u'value': {u'count': 1.0}}
{u'_id': u'Company', u'value': {u'count': 1.0}}
{u'_id': u'During', u'value': {u'count': 1.0}}
{u'_id': u'Dutch', u'value': {u'count': 1.0}}
{u'_id': u'East', u'value': {u'count': 1.0}}
{u'_id': u'For', u'value': {u'count': 1.0}}


其中 mongodb-study/blob/master/tools/import.py 只是把資料輸入到 mongodb 中,預設 database = db, collection = test。比較重要的 mapper, reducer 則是寫在 mongodb-study/blob/master/tools/map-reduce-word-count.py 程式中。

而 map-reduce-word-count.py 中,比較重要的則是 mapper 與 reducer 的定義,雖然是用 pymongo ,但這邊仍是用 Javascript 描述的,這大概是 pymongo 的最大缺點吧?印象中翻到 Java 版的 MongoDB 操作,可直接用 Java 來撰寫,這點影響還滿大的,簡言之,用 Pymongo 要撰寫 mapper、reducer 就是得先會一點 Javascript 才行。

使用 pymongo 撰寫 mapper 的方式,有一個小提醒就是 this.field 等於可以取到原先 input 的資料,但是,這邊的 this.field 需要強制轉型,這樣才能接著用 split 切字出來,另外,也可以自定 func 來呼叫使用,如此一來,就變成只剩字串處理的技巧了 :)

mapper = Code (
"""
function() {
var func = {
'author':function() {
return 'changyy';
}
};
(“”+this.field).split(/[\s\[\],\(\)"\.]+/).forEach(function(v){
//emit(func.author(), 1 );
if(v && v.length )
emit(v, {'count':1});
} );
}
"""
)


reducer = Code(
"""
function(key, value) {
var total = 0;
for(var i = 0 ; i < value.length ; ++i ) {
total += value[i].count;
}
return {'count':total};
}
"""
)

[Linux] Nginx 提供 localhost 不受 authorization 限制 @ Ubuntu 12.004

最近用 nginx 架設服務時,採用 HttpAuthBasicModule 限制存取時必須認證才行,然而,偶爾還是希望某些特定 IP 可以不受限制,研究一下是可行的:

http://nginx.org/en/docs/http/ngx_http_core_module.html#satisfy

syntax: satisfy all | any;
default:
satisfy all;
context: http, server, location

Allows access if all (all) or at least one (any) of the ngx_http_access_module, ngx_http_auth_basic_module or ngx_http_auth_request_module modules allow access.


因此,就只需在特定的 location 中,加上上述敘述即可:

location ^~ /service/ {
satisfy any;

# ngx_http_access_module
allow 127.0.0.1/32;
deny all;

# ngx_http_auth_basic_module
auth_basic "Restricted Area";
auth_basic_user_file    htpasswd;

# …
}

2014年1月23日 星期四

阿里雲初體驗

AYCloud00

中國發展快速,至今共有「阿里雲」、「騰訊雲」、「沃雲」等,先來試試阿里雲吧!目前可以申請五天免費試用,並且綁在淘寶帳號,當然,最重要的金流都弄妥了。

AYCloud10

AYCloud11

申請測試需要一點時間,過程中可能會有電話照會等,作業系統共有 Aliyun linux、CentOS 、Debian、Ubuntu、Windows Server 可以選擇,此例採用 Ubuntu 12.04 64Bit,最後建立起的機器 root 帳密會用簡訊通知,最便宜的一檯是一個月 55 RMB,更多細節可直接去逛逛 http://buy.aliyun.com/。未來就只需在阿里雲網站登入->我的阿里雲 -> 管理控制台:

AYCloud02

接著從「雲服務器ECS」進去:

AYCloud03

界面面跟 AWS EC2 的配置差不多,左邊選單,點一點可以看到目前運行列表:

AYCloud04

AYCloud05

接著會顯示"遠端登入"方式:

AYCloud06

顯示硬碟資訊:

AYCloud07

對硬碟進行 snapshot:

AYCloud08

顯示製作進度:

AYCloud09

disk snapshot 做完後,可以在做鏡像管理(開啟新機器時的選擇鏡像來源):

AYCloud10

AYCloud11

至於網路方面,從臺北 ping 青島的機器,晚上大概 290 ms 的反應時間,早上約 410ms。由於免費試用網路速度最高只能拉到 5Mbps ,若願意在測試的話,可以再加錢,最多調到 200Mbs。

AYCloud12

此外,阿里雲還有提供「雲盾」服務:

AYClod12

AYCloud13

AYCloud14

AYCloud15

以上就是簡單的把玩。

2014年1月22日 星期三

[Linux] MongoDB 與 PyMongo 初體驗 @ Ubuntu 12.04, Linode

架設:

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
$ sudo apt-get update

http://www.mongodb.org/downloads
$ sudo apt-get install mongodb-10gen=2.4.9
$ echo "mongodb-10gen hold" | sudo dpkg --set-selections

$ sudo service mongodb restart
mongodb stop/waiting
mongodb start/running, process ######

$ mongo
MongoDB shell version: 2.4.9
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user


防火牆存取限制:

$ sudo iptables --list-rules
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT

$ sudo vim /etc/init.d/iptables-rule.sh
#!/bin/sh

# BIN
BIN_IPTABLES=`which iptables`

# reset rules
$BIN_IPTABLES -F
$BIN_IPTABLES -X
$BIN_IPTABLES -Z

# init policies
#$BIN_IPTABLES -P INPUT DROP
#$BIN_IPTABLES -P OUTPUT ACCEPT
#$BIN_IPTABLES -P FORWARD ACCEPT

# mongo db
$BIN_IPTABLES -A INPUT -j ACCEPT -p tcp --destination-port 27017 -s 127.0.0.1,IP1,IP2,IP3
# mongo db drop all
$BIN_IPTABLES -A INPUT -j REJECT -p tcp --destination-port 27017

$ sudo chmod 775 /etc/init.d/iptables-rule.sh
$ sudo update-rc.d -f iptables-rule.sh defaults


一些 mongo 常用指令:

顯示所有的 databases (SQL: show databases)
> show dbs

使用指定 collection (SQL: use dbname)
> use dbname

顯示目前 databases 中的所有 collections (SQL: show tables)
> show collections

更多對照指令:
SQL to MongoDB Mapping Chart
PHP: SQL to Mongo Mapping Chart


安裝 pymongo 套件:

$ git clone git://github.com/mongodb/mongo-python-driver.git pymongo
$ cd pymongo
$ sudo python setup.py install


使用 pymongo 新增範例:

from pymongo import MongoClient

client = MongoClient()
database = client[‘dbname’] # SQL: Database Name
collection = database[‘table’]   # SQL: Table Name

item = {"author":"changyy"}
collection.insert(item)