create alias if not exists hdfs_put_ascii as $$
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import java.net.*;
@CODE
Integer hdfs_put_ascii(String uri, String user,
String path, Clob clob)
throws Exception
{
if( uri == null ){
throw new Exception("uri is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}
if( clob == null ){
throw new Exception("clob is not specified.");
}
FileSystem fs = FileSystem.get(
URI.create(uri), new Configuration(), user);
FSDataOutputStream fos = null;
InputStream ais = null;
byte buf[] = new byte[8192];
int rs = 0;
try
{
ais = clob.getAsciiStream();
fos = fs.create(new Path(path));
while( (rs = ais.read(buf)) != -1 ){
fos.write(buf, 0, rs);
}
fos.flush();
}
finally
{
if( ais != null )ais.close();
if( fos != null )fos.close();
}
return new Integer(0);
}
$$
実行例
drop table if exists clobtest;
create table clobtest (c1 numeric(4), c2 clob);
insert into clobtest values (1,
hdfs_get_ascii(
'hdfs://192.168.1.81:9000/',
'hadoop', '/opt/hadoop-data/test.txt')
);
select hdfs_put_ascii(
'hdfs://192.168.1.81:9000/',
'hadoop', '/opt/hadoop-data/test2.txt', c2)
from clobtest
where c1 = 1;
※以下のjarをCLASSPATH環境変数に追加
hadoop-common-0.21.0.jar
hadoop-hdfs-0.21.0.jar
log4j-1.2.15.jar
○動作環境
JDK6 Update22, Hadoop 0.21.0, H2 Database 1.2.147 (2010-11-21)
○関連情報
・CentOS5.5にHadoop0.21.0をインストールする
http://serverarekore.blogspot.com/2010/10/centos55hadoop0210.html
・H2 Databaseに関する他の記事はこちらを参照してください。
0 件のコメント:
コメントを投稿