2013-08-12

Not-so-standard HBase with Filters in Clojure

A more-or-less customised version of Hadoop/HBase is used in where I work. One day, I really wanted to use HBase filters and began to code like everyone else:
(:import [org.apache.hadoop.hbase.util Bytes]
         [org.apache.hadoop.hbase.client HTable]
         [org.apache.hadoop.hbase.filter
          RowFilter
          CompareFilter$CompareOp
          RegexStringComparator])
; ...
(defn daily-row-filter
  []
  (RowFilter. (CompareFilter$CompareOp/EQUAL
                (RegexStringComparator. "^[0-9a-fA-F-]{36}:[0-9]{8}$"))))
; ...
(hb/with-scanner [results (hb/scan ht :filter (daily-row-filter)
                            ; ...
)])
The programme happily compiled on my laptop, but I always got a class not found in CompareFilter$CompareOp/EQUAL Frustrated, I checked my colleague's code in Java and found what he wrote was:
import org.apache.hadoop.hbase.filter.CompareOp;
// ...
new RowFilter(CompareOp.EQUAL, new RegexStringComparator(...));
WHAT? It turned out that our HBase deviated from the standard. Here's the solution. In project.clj, add this not-so-standard hbase.jar:
            :resource-paths [ "/usr/lib/hbase/hbase.jar" ]
This way, you can successfully lein jar. The downside is that you cannot compile it in a standard environment any more. In core.clj, do this:
(:import
            [org.apache.hadoop.hbase.filter
            RowFilter
            CompareOp
            RegexStringComparator])

(defn daily-row-filter
  []
  (RowFilter. CompareOp/EQUAL
              (RegexStringComparator. "^[0-9a-fA-F-]{36}:[0-9]{8}$")))

  (hb/with-scanner [results (hb/scan ht
                                     :filter (daily-row-filter) ] ; ...
  )
Hope I can help someone, or at least I'm helping myself of the future.

2013-08-09

MSP430 Lauchpad + Forth 初開始

Prolog: There are tons of articles about Forth on MSP430 in English. I would thus like to write some introductory in Chinese.

小弟長久以來在FigTaiwan論壇上潛水,一直沒有產出,深感抱歉。今晚來寫點介紹性的文章,給軟體業的朋友們參考。

Forth (符式) 是一個 postfix (正式名稱叫逆波蘭表示法) 的雙堆疊語言,詳細的介紹網路上很多了,本篇的重點在如何在 Linux 下,把一個 Forth 環境裝到 MSP430 Lauchpad 上面。

1. 按照這張圖,把 TX/RD 設成硬體 UART:

用 USB 線把 MSP430 Lauchpad 和電腦接起來,dmesg 可以看到以下字樣:

[55571.993498] usb 3-1: Product: Texas Instruments MSP-FET430UIF
[55571.993503] usb 3-1: Manufacturer: Texas Instruments
...
[55571.999657] cdc_acm 3-1:1.0: ttyACM0: USB ACM device
2. 跟著這個網頁的說明,在 Linux 下燒錄第一個 MSP430 的程式 (這邊使用 Ubuntu, 11.x 版之後應該都可以):
$ git clone http://github.com/wendlers/msp430-harduart.git
$ sudo apt-get install mspdebug gcc-msp430
$ make
3. 編譯完成後,就可以燒錄了。
$ sudo make flash-target

mspdebug rf2500 "prog ./bin/firmware.elf"
MSPDebug version 0.19 - debugging tool for MSP430 MCUs
Copyright (C) 2009-2012 Daniel Beer 
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Trying to open interface 1 on 005
Initializing FET...
FET protocol version is 30394216
Configured for Spy-Bi-Wire
Set Vcc: 3000 mV
Device ID: 0x2553
Device: MSP430G2553
Code memory starts at 0xc000
Number of breakpoints: 2
Erasing...
Programming...
Writing  364 bytes to c000 [section: .text]...
Writing  106 bytes to c16c [section: .rodata]...
Writing   32 bytes to ffe0 [section: .vectors]...
Done, 502 bytes written
4. 到這邊一切順利的話,可以先玩玩看測試用的小程式。
$ sudo screen /dev/ttyACM0 9600
如果你和我一樣,看到恆亮的紅綠燈,screen 沒有顯示任何字,可以改用 minicom:
$ sudo apt-get install minicom
$ minicom -b 9600 -D /dev/ttyACM0
然後按下 reset 鍵,就可以看到 Logo 了:
***************
MSP430 harduart
***************

PRESS any key to start echo example ... 
5. 按任意鍵之後,就可以開始打字。每打一個字母,紅燈就會 toggle 一次。

6. 玩得開心之後,按 Ctrl-A x 離開 Minicom 。如果離不開,直接把 USB 線拔掉即可。 如果你想用 gcc-msp430 來寫程式,那到這個步驟就可以了。下面要開始進入 Forth

7. 去 4E4th 下載 Forth 主程式。 4E4th 主程式大概 6K, 也就是說還有 8K 的空間,可以讓你寫自己的程式。

8. 燒錄:

$ sudo mspdebug rf2500 "prog ./4e4th.a43"
MSPDebug version 0.19 - debugging tool for MSP430 MCUs
Copyright (C) 2009-2012 Daniel Beer 
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Trying to open interface 1 on 007
Initializing FET...
FET protocol version is 30394216
Configured for Spy-Bi-Wire
Set Vcc: 3000 mV
Device ID: 0x2553
Device: MSP430G2553
Code memory starts at 0xc000
Number of breakpoints: 2
Erasing...
Programming...
Writing   34 bytes to 1080...
Writing 4096 bytes to e000...
Writing 3482 bytes to f000...
Writing   32 bytes to ffe0...
Done, 7644 bytes written
9. 4E4th 的 Wiki 上有很詳細的說明,不過他們的 PDF 檔不是標準格式,我用 Chrome 打不開,另存新檔後用 evince 就可以看。

10. 奇怪的是,Forth 的範例我沒辦法使用 minicom, 但是用 screen 跑卻沒問題。可以試試這個:

$ sudo screen /dev/ttyACM0

打指令:
red cclr        -- 紅燈滅
red cset        -- 紅燈亮
green cclr      -- 綠燈滅
green cset      -- 綠燈亮

已經可以使用 Forth 指令了:
11 22 33 ok 
. 33 ok 
. 22 ok 
. 11 ok 

奇怪的是,它的堆疊沒有檢查下限?
. 12771 ok
. -3823 ok
. -7228 ok
11. 定義自己的 word 也沒問題:
: TEST ." Hello Forth!" ; ok 
TEST Hello Forth!ok 
12. 為了 MSP430 特製指令在這裡,需要參考一下: Glossary 完整的符式字的定義在這裡

13. 再多玩一下...

red . . [Enter] 33 1 ok
即 adr = 0x21 (Port1 輸出), m = bit0
green . . [Enter] 33 64 ok 
即 adr = 0x21 (Port1 輸出), m = bit6
14. 這塊板子可以幹什麼呢? 這邊 有些介紹。

15. 如果想研究原始碼,就 clone 一份下來吧!

$ svn co http://www.forth-ev.de/repos/4e4th/