2012年12月24日 星期一

[hadoop]Hadoop 小象安裝測試在 Ubuntu

小象要開始跑

在了解大概 hadoop 能做什麼之後,就來安裝試試吧。Michael G. Noll 寫了幾份非常好的教學。 這裡就按照他的教學,一步步重做一遍。就從他的 Running Hadoop On Ubuntu Linux (Single-Node Cluster) 開始讓小象跑。

http://www.michael-noll.com/tutorials/running-hadoop-on-ubuntu-linux-single-node-cluster/

而我這篇是讓我自己這個新手能夠不忘記我是怎麼安裝的筆記。

安裝 ubuntu 在 vmware 裡,完成 vmware-tool 安裝。

這一步是原來教學沒有的步驟。為了大家可以在公司、或自家的電腦試用,所以用 vmware 裝一個虛擬電腦。首先要注意,vmware-tool 的安裝跟 vmware 版本有關。vmware-tool 最重要的是可以讓 host 系統 及 guest 系統用「拖拉」、「複製貼上」的方式,交換檔案及文字。我使用的版本是 vmware 8,在裝好 ubuntu 12.04 之後,vmware-tool 可使用隨附的 8.8.0 版安裝。(vmware 7 可裝 ubuntu 11.04 配合 6.0.2。)在確認完 vmware 版本之後,就不用擔心之後會有問題了。

http://www.ubuntu.com/download/desktop 下載 12.04 LTS,下載 iso 檔。選擇 desktop 是因為有桌面可用,比較方便。

將 ubuntu 安裝完畢,先加入一個使用者 hduser,這是之後專門執行 hadoop 程式的使用者。

$ sudo addgroup hadoop

$ sudo adduser --ingroup hadoop hduser

hduser 要加入 sudoer 名單中,才能安裝 vmware-tool。

先用安裝者的帳號執行

$ sudo visudo

再把 hduser (ALL:ALL)=ALL 加在 root (ALL:ALL)=ALL 底下。

或把 hduser 加到 admin 的 group 裡面。(首先要確定有 admin 的 group。可參考)

$ sudo usermod -g admin hduser

然後切換使用者為 hduser。

接下來,在 vmware 的「VM」選單,按下「install VMware tool」,在虛擬電腦裡,會 mount 一個檔案。點兩下,會使用 archie manager 開啟檔案,把裡面的目錄「vmware-tools-distrib」拖到桌面上。

啟動 terminal,切換到 ~/Desktop/vmware-tools-distrib,執行

$ sudo ./vmware-install.pl

中間經過一連串的問題,都是按 enter 通過。如果有因為任何的錯誤而停下,都是因為 vmware-tools 的版本與 ubuntu 版本不合導致。我花了很多冤枉時間,才知道是版本問題。vmware 出 vmware-tools 程式一定是可以用才釋出,把版本弄對就不用白花時間。

安裝完畢,重開機一次。

設定 SSH

因為 hadoop 使用 ssh 管理它的 node,接下來要設定 ssh。

首先,產生 ssh key。

$ su - hduser # 換成 root 權限
hduser@ubuntu:~$ ssh-keygen -t rsa -P ""
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hduser/.ssh/id_rsa):
Created directory '/home/hduser/.ssh'.
Your identification has been saved in /home/hduser/.ssh/id_rsa.
Your public key has been saved in /home/hduser/.ssh/id_rsa.pub.
The key fingerprint is:
db:35:f4:ae:e3:79:48:d3:95:fa:2d:22:a8:43:5c:dd hduser@ubuntu
The key's randomart image is:
...
hduser@ubuntu:~$

第二行是產生一個不用密碼的 RSA key pari。這樣就不用 hadoop 與 node 溝通時,都要人去打密碼。

再來,把新產生的 key 放到已認證的 key 中。

hduser@ubuntu:~$ cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys

因為 desktop 版沒有 ssh server,因此,要加裝 openssh-server



hduser@ubuntu:~$ sudo apt-get install openssh-server


測試一下能不能連上 ssh server。因為這版的 server 預設使用 ECDSA 所以,ssh 的指令要強迫使用 rsa。



hduser@ubuntu:~$ ssh -oHostKeyAlgorithms='ssh-rsa' localhost

The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is a3:99:7f:2b:8e:92:34:20:59:2f:2d:10:94:c9:60:74.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
Welcome to Ubuntu 12.10 (GNU/Linux 3.5.0-17-generic i686)

* Documentation:  https://help.ubuntu.com/

222 packages can be updated.
52 updates are security updates.

Last login: Mon Dec 24 00:56:30 2012 from localhost
hduser@ubuntu:~$ exit
logout
Connection to localhost closed.
hduser@ubuntu:~$


另一個解決辦法是產生 ECDSA,並使用 ECDSA。


hduser@ubuntu:~$ ssh-keygen -t ecdsa -P ""
Generating public/private ecdsa key pair.
Enter file in which to save the key (/home/hduser/.ssh/id_ecdsa):
Your identification has been saved in /home/hduser/.ssh/id_ecdsa.
Your public key has been saved in /home/hduser/.ssh/id_ecdsa.pub.
The key fingerprint is:
27:64:63:de:16:4b:97:f4:58:96:11:46:6d:a2:24:7e hduser@ubuntu
The key's randomart image is:


因為後續的過程中,按照這個教學的話,hadoop 會自然的使用 ECDSA,所以,還是要用 ECDSA 的方式把 key 加到 .ssh/authorized_keys 裡面。


hduser@ubuntu:~$ cat $HOME/.ssh/id_ecdsa.pub >> $HOME/.ssh/authorized_keys


Noll 先生說,如果有遇到問題,要檢查一下在 /etc/ssh/sshd_config 裡:


  • PubkeyAuthentication 應該是 yes。
  • AllowUsers 如果有打開,則要把 hduser 加進去。
  • 如果 ssh server 的設定有異動,要強迫 ssh server 重載設定。 $ sudo /etc/init.d/ssh reload

安裝 java


java 至少要用到 ,我們可以使用指令來檢查 java 版本



hduser@ubuntu:~$ java -version
The program 'java' can be found in the following packages:
* default-jre
* gcj-4.6-jre-headless
* gcj-4.7-jre-headless
* openjdk-7-jre-headless
* openjdk-6-jre-headless
Try: sudo apt-get install <selected package>


新安裝的 ubuntu 12.04 desktop LTS 沒有裝 java runtime。要自己安裝。



hduser@ubuntu:~$ sudo apt-get install default-jre



hduser@ubuntu:~$ java -version
java version "1.7.0_09"
OpenJDK Runtime Environment (IcedTea7 2.3.3) (7u9-2.3.3-0ubuntu1~12.10.1)
OpenJDK Client VM (build 23.2-b09, mixed mode, sharing)
hduser@ubuntu:~$


 


安裝小象


從 Apache 下載 hadoop,目前穩定版是 1.0.4,到 http://www.apache.org/dyn/closer.cgi/hadoop/common/ 它會給你最近的 mirror 站點。下載 1.0.4 版的 hadoop-1.0.4.tar.gz。

 image

使用 firefox 下載,預設會放到 ~/Downloads 裡面。點兩下 hadoop-1.0.4.tar.gz,archive manager 會打開該壓縮檔,把裡面的目錄 hadoop-1.0.4 拉到桌面,改名為 hadoop,再移到 /usr/local 裡面去。使用指令


hduser@ubuntu:~$ sudo mv Desktop/hadoop/ /usr/local/
[sudo] password for hduser:
hduser@ubuntu:~$ ls -l /usr/local
total 36
drwxr-xr-x  2 root   root   4096 Oct 17 07:56 bin
drwxr-xr-x  2 root   root   4096 Oct 17 07:56 etc
drwxr-xr-x  2 root   root   4096 Oct 17 07:56 games
drwxr-xr-x 14 hduser hadoop 4096 Dec 24 01:09 hadoop
drwxr-xr-x  2 root   root   4096 Oct 17 07:56 include
drwxr-xr-x  4 root   root   4096 Oct 17 07:59 lib
lrwxrwxrwx  1 root   root      9 Dec 13 10:10 man -> share/man
drwxr-xr-x  2 root   root   4096 Oct 17 07:56 sbin
drwxr-xr-x  7 root   root   4096 Oct 17 08:00 share
drwxr-xr-x  2 root   root   4096 Oct 17 07:56 src


接下來要設定 .bashrc。(ubuntu 預設是用 bash。)使用指令開啟 .bashrc 來改。


hduser@ubuntu:~$ gedit .bashrc


在檔案的最後加進以下的設定。(原文中的 HADOOP_HOME 的設定已經要改用 HADOOP_PREFIX。)


################# for hadoop settings ##############
# Set Hadoop-related environment variables
export HADOOP_PREFIX=/usr/local/hadoop

# Set JAVA_HOME (we will also configure JAVA_HOME directly for Hadoop later on)
export JAVA_HOME=/usr/lib/jvm/default-java

# Some convenient aliases and functions for running Hadoop-related commands
unalias fs &> /dev/null
alias fs="hadoop fs"
unalias hls &> /dev/null
alias hls="fs -ls"

# If you have LZO compression enabled in your Hadoop cluster and
# compress job outputs with LZOP (not covered in this tutorial):
# Conveniently inspect an LZOP compressed file from the command
# line; run via:
#
# $ lzohead /hdfs/path/to/lzop/compressed/file.lzo
#
# Requires installed 'lzop' command.
#
lzohead () {
    hadoop fs -cat $1 | lzop -dc | head -1000 | less
}

# Add Hadoop bin/ directory to PATH
export PATH=$PATH:$HADOOP_PREFIX/bin

 


其他使用者要用 hadoop 的,也要更新這個 ~/.bashrc


設定 hadoop -- 1


第一個要處理的是 hadoop-env.sh


hduser@ubuntu:~$ gedit /usr/local/hadoop/conf/hadoop-env.sh


把裡面的


# export JAVA_HOME=/usr/lib/j2sdk1.5-sun


的底下,加上


export JAVA_HOME=/usr/lib/jvm/default-java


再來要把 ipv6 關掉。檔案的最後加上

export HADOOP_OPTS=-Djava.net.preferIPv4Stack=true

 

設定 hadoop -- 2


接下來是 core-site.xml,這是設定 hadoop 要在真實檔案系統的位置。因此,先建立一個目錄給 hadoop 使用。


hduser@ubuntu:~$ sudo mkdir -p /app/hadoop/tmp
[sudo] password for hduser:
hduser@ubuntu:~$ ls -l /app/hadoop/
total 4
drwxr-xr-x 2 root root 4096 Dec 24 02:02 tmp
hduser@ubuntu:~$ sudo chown hduser:hadoop /app/hadoop/tmp
hduser@ubuntu:~$ ls -l /app/hadoop/
total 4
drwxr-xr-x 2 hduser hadoop 4096 Dec 24 02:02 tmp
hduser@ubuntu:~$ sudo chmod 750 /app/hadoop/tmp
hduser@ubuntu:~$ ls -l /app/hadoop/
total 4
drwxr-x--- 2 hduser hadoop 4096 Dec 24 02:02 tmp
hduser@ubuntu:~$


把下列的文字,加到 /usr/local/hadoop/conf/core-site.xml 的 <configuration> ... </configuration> 中間:

<!-- In: conf/core-site.xml -->
<property>
<name>hadoop.tmp.dir</name>
<value>/app/hadoop/tmp</value>
<description>A base for other temporary directories.</description>
</property>

<property>
<name>fs.default.name</name>
<value>hdfs://localhost:54310</value>
<description>The name of the default file system. A URI whose
scheme and authority determine the FileSystem implementation. The
uri's scheme determines the config property (fs.SCHEME.impl) naming
the FileSystem implementation class. The uri's authority is used to
determine the host, port, etc. for a filesystem.</description>
</property>

把下列的文字,加到 /usr/local/hadoop/conf/mapred-site.xml 的 <configuration> ... </configuration> 中間:

<!-- In: conf/mapred-site.xml -->
<property>
<name>mapred.job.tracker</name>
<value>localhost:54311</value>
<description>The host and port that the MapReduce job tracker runs
at. If "local", then jobs are run in-process as a single map
and reduce task.
</description>
</property>

把下列的文字,加到 /usr/local/hadoop/conf/hdfs-site.xml 的 <configuration> ... </configuration> 中間:

<!-- In: conf/hdfs-site.xml -->
<property>
<name>dfs.replication</name>
<value>1</value>
<description>Default block replication.
The actual number of replications can be specified when the file is created.
The default is used if replication is not specified in create time.
</description>
</property>

 


如果要對設定檔多了解,可以到以下連結查看:


http://wiki.apache.org/hadoop/GettingStartedWithHadoop


http://hadoop.apache.org/core/docs/current/api/overview-summary.html


格式化 HDFS 的檔案系統


Noll 先生在這非常強調,不要對正在使用中的系統做格式化的動作。該系統資料會消失。

我們這個新系統要啟用,則必須先格式化檔案系統。



hduser@ubuntu:~$ /usr/local/hadoop/bin/hadoop namenode -format


結果如下:


hduser@ubuntu:~$ /usr/local/hadoop/bin/hadoop namenode -format
12/12/24 02:14:23 INFO namenode.NameNode: STARTUP_MSG:
/************************************************************
STARTUP_MSG: Starting NameNode
STARTUP_MSG:   host = ubuntu/127.0.1.1
STARTUP_MSG:   args = [-format]
STARTUP_MSG:   version = 1.0.4
STARTUP_MSG:   build = https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1.0 -r 1393290; compiled by 'hortonfo' on Wed Oct  3 05:13:58 UTC 2012
************************************************************/
12/12/24 02:14:23 INFO util.GSet: VM type       = 32-bit
12/12/24 02:14:23 INFO util.GSet: 2% max memory = 19.33375 MB
12/12/24 02:14:23 INFO util.GSet: capacity      = 2^22 = 4194304 entries
12/12/24 02:14:23 INFO util.GSet: recommended=4194304, actual=4194304
12/12/24 02:14:24 INFO namenode.FSNamesystem: fsOwner=hduser
12/12/24 02:14:24 INFO namenode.FSNamesystem: supergroup=supergroup
12/12/24 02:14:24 INFO namenode.FSNamesystem: isPermissionEnabled=true
12/12/24 02:14:24 INFO namenode.FSNamesystem: dfs.block.invalidate.limit=100
12/12/24 02:14:24 INFO namenode.FSNamesystem: isAccessTokenEnabled=false accessKeyUpdateInterval=0 min(s), accessTokenLifetime=0 min(s)
12/12/24 02:14:24 INFO namenode.NameNode: Caching file names occuring more than 10 times
12/12/24 02:14:25 INFO common.Storage: Image file of size 112 saved in 0 seconds.
12/12/24 02:14:25 INFO common.Storage: Storage directory /app/hadoop/tmp/dfs/name has been successfully formatted.
12/12/24 02:14:25 INFO namenode.NameNode: SHUTDOWN_MSG:
/************************************************************
SHUTDOWN_MSG: Shutting down NameNode at ubuntu/127.0.1.1
************************************************************/
hduser@ubuntu:~$


啟動 cluster


到了要啟動系統的時候,使用指令:

hduser@ubuntu:~$ /usr/local/hadoop/bin/start-all.sh
看到以下的 log 就代表成功了。


hduser@ubuntu:~$ /usr/local/hadoop/bin/start-all.sh
starting namenode, logging to /usr/local/hadoop/libexec/../logs/hadoop-hduser-namenode-ubuntu.out
localhost: starting datanode, logging to /usr/local/hadoop/libexec/../logs/hadoop-hduser-datanode-ubuntu.out
localhost: starting secondarynamenode, logging to /usr/local/hadoop/libexec/../logs/hadoop-hduser-secondarynamenode-ubuntu.out
starting jobtracker, logging to /usr/local/hadoop/libexec/../logs/hadoop-hduser-jobtracker-ubuntu.out
localhost: starting tasktracker, logging to /usr/local/hadoop/libexec/../logs/hadoop-hduser-tasktracker-ubuntu.out
hduser@ubuntu:~$


 

利用 jps 來看是否已啟動


Noll 先生說可以使用 jps 來看啟動是否成功。但新裝的 ubuntu 沒有這東西。



The program 'jps' can be found in the following packages:
* openjdk-6-jdk
* openjdk-7-jdk
Ask your administrator to install one of them


於是,安裝 openjdk-7-jdk



sudo apt-get install openjdk-7-jdk




再試一次



hduser@ubuntu:~$ jps
7516 TaskTracker
7287 SecondaryNameNode
7839 DataNode
12038 Jps
6491 NameNode
6901 JobTracker
hduser@ubuntu:~$

 


也可以用 netstat 來看監聽的 port 是否有開。



hduser@ubuntu:~$ sudo netstat -plten | grep java


停止 single-node cluster

使用這個指令:


hduser@ubuntu:~$ /usr/local/hadoop/bin/stop-all.sh


會看到:



hduser@ubuntu:~$ /usr/local/hadoop/bin/stop-all.sh
stopping jobtracker
localhost: Agent admitted failure to sign using the key.
localhost: stopping tasktracker
stopping namenode
localhost: Agent admitted failure to sign using the key.
localhost: stopping datanode
localhost: Agent admitted failure to sign using the key.
localhost: no secondarynamenode to stop
hduser@ubuntu:~$


原篇照我的方式走,真的太長。因此下一篇來驗證,這個系統真的可以用。

2012年12月20日 星期四

[Android]Market 進不去

送修的 HTC Desire 回來了,要開始安裝 App,於是點了 Market 按鈕,居然發生 404

訊息顯示:

The requested URL /intl/%locale%/mobile/android/market-tos.html was not found on this server. That’s all we know.

嗯。你不知道,我也不知道,然後呢?

花了一、兩個小時爬 google。找到以下連結。

http://forum.xda-developers.com/showthread.php?t=767992

做法很簡單,(1)先把語系切到英文。(2)按一下 Market。讓它跑過去。(3)再切回原來的語系。

好了。要怪誰呢?

2012年12月10日 星期一

[bigdata]小象幫幫忙,Hadoop 能幫什麼忙?

Hadoop 文獻探討

微軟巨量資料策略轉向,全面支援Hadoop

http://www.ithome.com.tw/itadm/article.php?c=77576

微軟表示,全面支援 Apache Hadoop,這消息說明了:

Hadoop 夠熱門,讓微軟無法抵擋。俗話說:「打不動,就加入」,現在微軟就加入了。
這不是第一個微軟採納的開放軟體。jQuery,在2008年加入微軟的 visual studio。
並且 ASP .NET MVC, ASP .NET AJAX 都將採用 jQuery 的技術。(舊消息)

最近熱門關鍵字,從雲端,換成 Big Data,有翻譯成「巨量資料」、有翻譯成「海量資料」,
到底這是什麼東西?為什麼幾乎每個介紹文章都會講到 Hadoop ?為什麼講雲端的時候有的也會提到 Hadoop?說來話長。

何謂Big Data?這問題目前沒辦法正面回答,因為個人認為現在這東西還在成長變化中,
也不是一個標準,所以無法用文字嚴格定義。網路上多數的文章會這麼說:

「因為雲端時代的來臨,資料量增加的速度非常快,所以需要一些新方法來應付這些資料。
巨量資料的特性有 3V,Volume(資料量大),Volecity(產生速度快),Variety(資料多樣性高)
在合理時間及資源下,巨量資料的技術才能夠應付這些需求。」

其實,這還是很模糊的描述。所以,只能知道,巨量資料並不是突然跑出來的東西。但「巨量資料」這個名詞是為了方便討論某些情況下,使用某些技術而產生的新名詞。巨量資料並不是每一個人或每一個企業都會面臨到的,有能力產生上面描述的 3V 的資料,才會有需要使用到巨量資料的技術。

因此,接下來,我們反過來,從被微軟納入解決方案核心的 Hadoop 來看。看 Hadoop 能夠解決什麼問題,至少可以了解,巨量資料大概是什麼東西。

Hadoop 提供的核心功能有二,名字叫 HDFS 以及 MapReduce。HDFS 是 Hadoop Distributed File System 的縮寫。MapReduce 則是 Hadoop 採用分散式計算的技術。也就是,它基本上要解決的問題有兩個,一個是儲存,一個是計算。

在 HDFS 的設計概念是這樣的。在一群電腦裡面,找一台電腦當老大,叫做 Master Node。它必須負責管理所有的電腦(包含自己)的資料的存放狀態、控制讀寫動作。所以裝了一個程式叫 Name Node 來做管理這件事。也裝了一個 Data Node 的程式來放資料。其他的電腦,叫做 Worker Node,裝了一個 Data Node 的程式來放資料。放資料的規則是,先把一個檔案,切成數個小塊(block),每塊為 64 MB,然後, Name Node 會讓檔案塊,分散到各個不同的 Data Node 去,而且,還會讓整個電腦叢集裡,每一個檔案塊,都有三份。若是 Name Node 發現檔案塊遺失或損壞,會開始尋找其他的 Data Node 上的副本來複製,使得整個電腦叢集維持每個檔案塊的數量都有三份。

從以上的設計慨念裡,可以知道:(1)它不怕單一資料存放區的硬體壞掉,壞掉照常工作,並自動回復、(2)它不怕單一檔案的大小超過一個磁碟區的大小、(3)資料存放區的大小,可以動態增加。(註1)

註1:http://www.classcloud.org/cloud/wiki/Hadoop_Lab8

要講 MapReduce 的設計概念,先講解一下,MapReduce 是什麼。在 functional language 裡面可常見到這個字。它其實是兩個英文字連起來的。也就是 Map 及 Reduce。
Map 是一種具有某種行為特色的函數,三言兩語說不清,我舉一個例子來說明。假設我有一個數列A,1~10,我想要得到另一個數列B,它裡面的數字是數列A的每個數字的 2 倍,相當於B = f(A),f(x): y=2x。撇開數學式不看。寫程式會這樣寫(C#):

int[] A = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] B = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //初值化的數列
static int f(int x) { return 2 * x; }

for (int i = 0; i < A.Length; i++)
{
    B[i] = f(A[i]);
}

這程式裡面看到,數列A、數列B,還有函數 f。剩下來的是那個迴圈,它的動作是把每個數列A的數值,經過函數 f 得到的值,放到數列B 對應的位置。只要是可以完成這一類事情的函數,就叫做 Map 函數。這種函數的特色就是可以平行運算。以上述的例子,把 map 函數寫出來,程式再改寫一下會成為:

int[] A = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] B = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //初值化的數列
static int f(int x) { return 2 * x; }

static int[] map(Func<int, int> _func, int[] _a)
{
    int[] _b = new int[_a.Length];
    for (int i = 0; i < _a.Length; i++)
    {
        _b[i] = _func(_a[i]);
    }
    return _b;
}

B = map( f, A)

Reduce 也是一種具備某種行為特色的函數。也是要舉一個例子來說明。假設一個數列A,1~10,要計算從 1 加到 10,程式會寫成:

int f2(int x, int y){ return x + y; }
int[] A = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int C = 0;
for (int i = 0; i < A.Length; i++)
{
    C = f2(C, A[i]);
}

其中,把迴圈部份依然可以改寫成函數,整個程式變成:

int f2(int x, int y){ return x + y; }
static int reduce(Func<int, int, int> _func, int[] _a)
{
    int _c = 0;
    for (int i = 0; i < _a.Length; i++)
    {
        _c = _func(_c, _a[i]);
    }
    return _c;
}

int[] A = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int C = 0;
C = reduce(f2, A);

凡是可以完成這一類工作的函數,都叫做 Reduce 函數。

所以,MapReduce 的技術,是已經在 Hadoop 中準備好可供分散式計算呼叫的 Map,以及 Reduce 的函數。而使用的人,要把計算的函數,寫成可被 Map 或 Reduce 呼叫。如此就可以利用分散式運算來縮短運算時間。例如,最簡單的分散式運算版的 Map,就不用迴圈,改成分給 10 個 Node 各自運算,等全部得到值,放入數列,再回傳數列,依照 Map 的概念來寫程式,就不怕平行化運算時會出問題了。

了解了 MapReduce 的意思之後,就可以看懂介紹 MapReduce 的示意圖了。要用 MapReduce 技術來計算,首先要把程式,寫成可被 Map,Reduce 使用的樣子。在叢集中,Master Node 還是當老大,負責管理所有的 Job,所以安裝一個叫 Job Tracker 的程式,而 Task Tracker 程式,則是裝在所有可以當計算的電腦裡。當運算開始,Master Node 分配 Map 函數要做的 Task 給 Worker Node,分配時會盡量交給資料在附近的 Node,如此會減少資料傳輸時間。Map 函數計算完的結果會存下來。等到的 Map 程式的結果出來,Reduce 程式可緊接著執行,即可將最終結果算出來。

總和來看,Hadoop 核心所提供的兩個功能,解決了檔案存放的問題、解決了系統擴張的問題、解決了系統備援的問題、解決了分散式運算的問題。依此來推測, Hadoop 可以被大家接受成為巨量資料的主流技術,巨量資料的主要需求及適用範圍即是這些東西。但是巨量資料的所有難題,用 Hadoop 就可以解決了嗎?其實並沒有。HDFS 與 MapReduce 只解決了基本的問題,我們可以從Apache 基金會 Hadoop 相關的其他專案的功能來了解 Hadoop 本身做不到的事。

(1) HBase,是專門用在 Hadoop 檔案系統上的資料庫系統。以此推知,Hadoop 沒有資料庫系統。

(2) Hive,是建置在 HDFS 上的分散式資料倉儲系統,可讓使用者以慣用的 SQL 語法,存取檔案中的大型資料集。以此推知,Hadoop 沒有 SQL 語法查詢功能。

(3) Pig,是 Script 語言,可用來撰寫 MapReduce 程式,不懂 Java 也可以使用這個語言開發 MapReduce 程式。以此推知,MapReduce 程式在 Hadoop 系統是用 Java 撰寫,要效能好,就要用 Java 來寫。

(4) ZooKeeper,是監控和協調 Hadoop 分散式運作的集中式服務,提供各個伺服器的配置和運作狀態資訊,用於提供不同 Hadoop 系統角色之間的工作協調。

(5) Mahout,立即可用的 MapReduce 函式庫,常見的數值分析方法,業集分類和篩選方法,都有對應的函數可以呼叫。

這些專案的功能,可以來解決巨量資料的其他需求,而這些需求,不是每個人都需要,所以不在核心功能之中。也可以想見巨量資料的需求,還是因人而異,而且差異很大。

希望這篇有把 Hadoop 能做什麼說清楚,也希望有把巨量資料的其中一個面向說清楚。

2012年11月22日 星期四

[aspx]HttpUtility.UrlDecode 與 Server.UrlDecode 真的不一樣

我以為我經過上次的錯誤,中文亂碼可以不會再出現在我的生活中。

直到我的膝蓋中了一箭。 (這裡)

找了一陣子,原來有人,以前的程式碼用了Server.UrlDecode

HttpUtility.UrlDecode 與 Server.UrlDecode 真的不一樣。

我寫了以下的程式來證明:

Function WriteOutput(ByVal str As String) As String
    Response.AppendHeader("Content-Type", "text/html;charset=UTF-8")
    Dim bs As Byte() = System.Text.Encoding.UTF8.GetBytes(str)
    Response.ContentEncoding = System.Text.Encoding.UTF8
    Response.OutputStream.Write(bs, 0, bs.Length)
    Return "OK"
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    WriteOutput("System.Text.Encoding.Default.EncodingName:" + System.Text.Encoding.Default.EncodingName + "<br/>")
    WriteOutput("System.Text.Encoding.Default.WebName:" + System.Text.Encoding.Default.WebName + "<br/>")
    WriteOutput("HttpUtility.UrlDecode:" + HttpUtility.UrlDecode("%e4%b8%ad") + "<br/>")
    WriteOutput("Server.UrlDecode:" + Server.UrlDecode("%e4%b8%ad") + "<br/>")
End Sub

結果這就是不一樣:

image

附帶說明一下,這個現象不一定會在你的伺服器出現。在我的開發環境,如下。

image

我現在還找不到確切指出設定不同的地方。

我只看到兩個 EncodingName 所出現的字串是不同的。

據網路消息,說 Server.UrlDecode 是用系統編碼。可是我還沒辦法找到如何顯示現在系統編碼。

2012年11月20日 星期二

[aspx]不會用設定,不要怪微軟!(原題:好心的微軟幫了倒忙。中文亂碼解決實錄)

更新:要看最後解法,請直接跳最下面

有史以來解決中文亂碼最長的一次。從我2000年開始,踩進可怕的中文混沌中,
從沒有這麼令人生氣的一次了。

原因就是微軟幫倒忙!微軟好心幫忙但是我還是得告訴它我的 charset。

「絕聖棄智,民利百倍。絕仁棄義,民復孝慈。絕巧棄利,盜賊無有。」--老子

故事是這樣開始的。要求是:
「我需要用 post 方法,傳送一筆記錄的資料,而網頁伺服器接到之後,幫忙下 SQL 插入資料。」

你看多簡單的一句話!結果發生亂碼,沒關係,那我就寫個測試的程式,看是傳過去的字就亂了,還是 SQL 執行完才錯。

好,簡化過的發送端程式如下:

Function SendPutRequest(ByVal keyname As String, ByVal keyvalue As String)
    Dim uristr As String = "http://localhost:1490/WebSite1/poid_fail.aspx"
    Dim req As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(uristr)
    req.Method = "POST"

    Dim param As String = "a=put"
    param += "&" & "key" & "=" & keyname
    param += "&" & "value" & "=" & keyvalue
    Dim bs As Byte() = Encoding.UTF8.GetBytes(param)
    req.ContentType = "application/x-www-form-urlencoded"
    req.ContentLength = bs.Length

    Using reqStream As System.IO.Stream = req.GetRequestStream()
        reqStream.Write(bs, 0, bs.Length)
    End Using
    Using wr As System.Net.WebResponse = req.GetResponse()
        Dim rs(wr.ContentLength) As Byte
        Dim expectcount As Integer = wr.ContentLength
        Dim readcount As Integer = 0
        Response.ContentEncoding = System.Text.Encoding.UTF8
        readcount += wr.GetResponseStream().Read(rs, 0, wr.ContentLength)
        Response.Write(Encoding.UTF8.GetChars(rs))
    End Using
    Return "OK"
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    SendPutRequest("DataName", "中文測試")
End Sub

發送端,就只是一個假裝用 form 打出資料的 request。其中也很簡單的一個 keyname 跟 keyvalue,傳送至伺服端

而,接收端程式(簡化版)很快就寫好了:

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim act As String
        Dim insertSql As String = ""
        Try
            act = Request.Form("a")
            If act = "put" Then
                Dim keyname As String
                keyname = Request.Form("key")
                Dim keyvalue As String
                keyvalue = Request.Form("value")
                insertSql = "insert into [poidata] ($columns$) values ($values$)"
                insertSql = insertSql.Replace("$columns$", keyname).Replace("$values$", keyvalue)
                ExecuteSql(insertSql)
            End If
        Catch ex As Exception
            insertSql = ex.Message
        End Try
        Dim contentbody As String = insertSql
        Response.AppendHeader("Content-Type", "text/json")
        Response.ContentEncoding = System.Text.Encoding.UTF8
        Response.OutputStream.Write(Encoding.UTF8.GetBytes(contentbody), 0, System.Text.Encoding.UTF8.GetByteCount(contentbody))
    End Sub
</script>

在開發環境很正常。發送端與接收端都是在同一個電腦上,沒有問題。

image 

把接收端程式丟到真的伺服器去,就變亂碼!:

image

好。很正常的,因為值有中文,沒有 encode 所以變亂碼,我可以接受。
而我還特別把接收端的程式的 response,以 Encoding.UTF8.GetBytes 轉成 byte,然後用 write byte 方式,這是最保險,照字串byte回傳回來的保險方式。

那我 encode 吧。

把傳送端 encode。

param += "&" & "key" & "=" & HttpUtility.UrlEncode(keyname)

param += "&" & "value" & "=" & HttpUtility.UrlEncode(keyvalue)

結果長一模一樣。奇怪了。

image

那我接收端 decode 看看,結果一樣。

那來檢查兩邊的 default encoding 好了。
把接收端的 default encoding 回傳回來看看。

Dim contentbody As String = insertSql + "<br />" + System.Text.Encoding.Default.EncodingName

在開發環境,看起來是這樣

image

把程式丟到真的伺服器上,嗯,都是 big5 啊!

image

啊,等等,不對勁耶,為什麼真的伺服器不是寫「繁體中文 (Big5)」傳回來,而是用「Chinese Traditional (Big5)」這是搞什麼鬼?

後來,同事看我搞很久,決定傳授我妙方。
原本,在 IIS 的設定,我是使用虛擬目錄的方式,指到網站程式所放的目錄。
而他的經驗是,在 IIS 上,指定用「網路應用程式」
他也幫我弄好了。只是,網路位址會移動,而我,只是改別人的程式,不太想去改位址這種事。
如果,只想研究到這裡的人,就去改成「網路應用程式」試試吧。

因為沒辦法、因為不死心,我決定來試試強迫接到 encode 過的字串為目標,
想確定傳過去的 byte 沒有變動。
所以,接收端先不 decode,就接到什麼回寫什麼看看。
再來,因為傳送端我 encode 一次不夠,我再多 encode 一次看看。

param += "&" & "value" & "=" & HttpUtility.UrlEncode(HttpUtility.UrlEncode(keyvalue))

很賭氣的作法。UrlEncode 這種 encode 法,作兩次 encode,要得回原來的字,就要 decode 兩次。只要我拿到不是亂碼的字串,算清楚encode次數,就知道是不是 byte 被動到了。

image

很好,這是好的開始,這看來像是 encode 一次的字串。
那我接收端,就 decode 一次試試。

keyvalue = HttpUtility.UrlDecode(Request.Form("value"))

image

耶,看來好了。

而實際把組好的 SQL 字串執行下去,資料庫裡,也就正常的中文了。

回頭討論,我的傳送端 encode 兩次,我的接收端 decode 一次。
這代表,有某個地方幫我 decode 一次。而且在我的程式執行之前。
按照我的推斷,那一次的 decode 的時候,參考不知道哪裡的編碼,在那裡字串亂掉了。
明明就看不懂中文,還硬要轉,當然就搞亂了。
這從 default encoding name 可以看得出來,雖然都是 Big5,但是一個用中文表示,一個用英文表示,明白的指出,兩個的執行環境是不同的,而這個執行環境的不同,就會導致亂碼。
而我,用兩次 encode,讓我的字串被自動 decode (也就是第一次,不是我的程式做的) 之後,成為 encode 一次的狀態,這時是 ascii 的字串表示式,用這個字串,讓我的程式來 decode 回 .net 裡面的字串。由於資訊保持完整,所以可以完美地解回 .net 字串。問題才得以解決。
確切幫忙 decode 一次的程式我沒有找到,不過應該不脫離 iis,asp .net 這個範圍。
所以這次幫倒忙就算在微軟的頭上!

我想也許會有高手更了解 IIS 或 asp .net 的可以用更漂亮的方式解決這問題吧?
我的確是不了解所以硬幹啊。

更新:

因為無可避免地會被 decode 一次,所以指定正確 charset 一定是正解。指定的方式即在 request 的 Content-Type 裡。如下:

req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"

接收端程式,不用需 decode,微軟會按指定編碼幫你搞定。

它就是要幫你解,你只好一定要告訴他怎麼解。

2012年10月29日 星期一

[asp]使用VS2008,怎麼對 asp debug

如果使用VS2008,若直接對 asp 按下 debug,會出現

image

經過搜尋,解決方法是,

(1)設定iis,使得該頁可以直接使用。

image

(2)打開VS2008,開啟網站(檔案->開啟->網站),選擇網站所在的目錄

image

(3)附加至處理序(偵錯->附加至處理序),選擇 w3wp.exe (適用於 iis 6)

image

按下附加之後,VS2008,就會進入偵錯模式。

(4)瀏覽器跑一下 asp 網頁,方案總管會出現指令碼文件。

image

(5)雙擊 asp 文件,打開文件後,即可設中斷點,開始正式 debug。

image

 

結尾:

美中不足,不能邊改邊debug,你可以看到這文件是唯讀的。

[asp] 最近又用到 asp,幾個筆記需要記

判斷 access 資料庫某個欄位值為 null

我以為用 if cursor.fields(i).value = null then…可以判斷。我錯了。

要用 if isnull(cursor.fields(i).value) then … 才行。

 

到底權限怎麼開才能寫入?

首先,我的 os 是 windows 7,iis是 7 (應該吧…)

如果是 asp .net 的程式,IIS_USRS 這個使用者要開啟權限到寫入。

如果是 asp 的程式,IUSR 這個使用者要開啟權限到寫入。

 

如何存取utf-8 編碼的文字檔內容?

asp 直接 openTextFile 是不對的。它會按執行環境的預設值讀寫檔案。

我有改過CODEPAGE="65001",無效。

目前使用 ADODB.Stream 可。

有人提供小函數使用。

Sub Save2File (sText, sFile)
    Dim oStream
    Set oStream = CreateObject("ADODB.Stream")
    With oStream
        .Open
        .CharSet = "utf-8"
        .WriteText sText
        .SaveToFile sFile, 2
    End With
    Set oStream = Nothing
End Sub

Function ReadUtf8File(sFile)

    Set objStream = CreateObject("ADODB.Stream")
    With objStream
        .Charset = "utf-8"
        .Type=2
        .mode=3
        .Open
        .loadfromfile sFile
        ReadUtf8File=.readtext
        .Close
    End With
    Set objStream = Nothing

End Function

使用 javascript 的 escape 的文字,asp 怎麼解?

我基本上放棄了。試了幾次都不成功。這個問題很大,需要前後程式配合(html, javascript, asp)。因此會另外再寫詳細的文章。

簡單來說,我後來轉用 htmlencode。那 javascript 怎麼做 htmlencode 或 htmldecode?

以下有兩個小函數可以使用

function htmldecode(strHtmlEncode){
    var div =  document.createElement('div');
    div.innerHTML = strHtmlEncode;
    return div.innerText
}

function htmlencode(strHTML)
{
    var div = document.createElement('div');
    div.innerText = strHTML;
    return div.innerHTML;
}

asp 的程式,直接取值就好,不用解。