この例では、SCRIPTテーブル演算子を使用してPythonスクリプト ファイルを呼び出します。スクリプトmapper.pyはテキスト入力の行(“Old Macdonald Had A Farm”)を読み込み、その行を個別の語に分割し、各語に対し新しい行を作成します。
Pythonスクリプトの例:
#!/usr/bin/python
import sys
# input comes from STDIN (standard input)
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# split the line into words
words = line.split()
# increase counters
for word in words:
# write the results to STDOUT (standard output);
# what we output here will be the input for the
# Reduce step, i.e. the input for reducer.py
#
# tab-delimited; the trivial word count is 1
print '%s\t%s' % (word, 1)
スクリプトをインストールするには、以下のコマンドを実行します。
CALL SYSUIF.INSTALL_FILE('mapper', 'mapper.py'
'cz!/tmp/mapper.py');
テーブル境界には文がテキスト入力の1行として含まれています。
| Id int | Name varchar(100) |
|---|---|
| 1 | Old Macdonald Had A Farm |
文を個別の語に分割するには、以下のスクリプトを実行します。
SELECT * FROM SCRIPT
( ON ( SELECT name FROM barrier )
SCRIPT_COMMAND('./mydb/mapper.py')
RETURNS ( 'word varchar(10)', 'count_input int' ) ) AS tab;
);
結果:
| Word | Count_input |
|---|---|
| Old | 1 |
| Macdonald | 1 |
| Had | 1 |
| A | 1 |
| Farm | 1 |