2011年1月21日金曜日

Apache Derbyでドライブの空き容量を返す関数を作成する

Apache Derbyでドライブの空き容量を返す関数を作成するには、以下の手順を実行します。

1.以下のクラスをantでコンパイルします。
FsGetFreeSpaceFunction.java
package com.serverarekore.derby;
import java.io.*;
import java.sql.*;
import java.net.*;

public class FsGetFreeSpaceFunction
{
public static Long fs_get_free_space(String path)
{
if( path == null )return null;
File file = new File(path);
return new Long(file.getFreeSpace());
}
}

build.xmlファイル例
<project name="DerbyFunctions" default="compile" basedir=".">
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile">
<echo message="project: ${ant.project.name}"/>
<mkdir dir="build/" />
<javac srcdir="src/" destdir="build/"
deprecation="on" debug="on">
<classpath>
<path refid="lib.classpath"/>
</classpath>
</javac>
<jar destfile="c:\share\derby_funcs\FsGetFreeSpaceFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


2.derbyにjarをインストール
call sqlj.install_jar('c:\share\derby_funcs\FsGetFreeSpaceFunction.jar', 'APP.func1', 0);


3.クラスパスに通す
call syscs_util.syscs_set_database_property('derby.database.classpath', 
'APP.func1');


4.関数を作成
create function fs_get_free_space(path varchar(1024)) returns bigint
parameter style java no sql language java
external name 'com.serverarekore.derby.FsGetFreeSpaceFunction.fs_get_free_space';


5.実行
select fs_get_free_space('c:\') from sysibm.sysdummy1;


○動作環境
Apache Derby 10.7.1.1, JDK6 Update23
○関連情報
・Apache Derbyに関する他の記事はこちらを参照してください。

2011年1月20日木曜日

H2 DatabaseとOrion SSH2でサーバ上のファイルを列挙する関数を作成する

H2 DatabaseとOrion SSH2でサーバ上のファイルを列挙する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_list_files as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
ResultSet orionssh2_list_files(String host, String user,
String password, String path)
throws Exception
{
if( host == null ){
throw new Exception("server is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( password == null )password = "";
if( path == null )path = ".";

SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("PATH", Types.VARCHAR, 1024, 0);

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;
try
{
// サーバに接続
conn = new com.trilead.ssh2.Connection(host);
conn.connect();
if( !conn.authenticateWithPassword(user, password) ){
throw new Exception("authentication failed.");
}

// SFTP client作成
sftpc = new SFTPv3Client(conn);
sftpc.setCharset("UTF-8");

// ファイル列挙
Vector files = sftpc.ls(path);
for(int fi=0;fi<files.size();fi++){
SFTPv3DirectoryEntry dirent =
(SFTPv3DirectoryEntry)files.get(fi);
rs.addRow(dirent.filename);
}
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return rs;
}
$$


実行例
select * from orionssh2_list_files('192.168.1.25', 'user', 'password', '.')


※以下のjarをCLASSPATH環境変数に追加
orion-ssh2-214.jar

○動作環境
JDK6 Update23, H2 Database 1.2.149 (2011-01-07)

○関連情報
H2 Database上でOrion SSH2を使用するユーザー定義関数のまとめ
・H2 Databaseに関する他の記事はこちらを参照してください。

2011年1月19日水曜日

UbuntuでCurlFtpFSを使用して、FTPサーバ上のディレクトリをマウントする

UbuntuでCurlFtpFSを使用して、FTPサーバ上のディレクトリをマウントするには、以下の手順を実行します。

1.CurlFtpFSのインストール
sudo apt-get install curlftpfs

2.FTPサーバ上のディレクトリのマウント
mkdir ~/ftp
curlftpfs -o user=user1:pass ftp://192.168.1.122/ ~/ftp
ls ~/ftp

3.アンマウント
fusermount -u ~/ftp

動作環境
Ubuntu 10.10

2011年1月18日火曜日

H2 DatabaseとHadoopでBLOBをHDFS上にアップロードする関数を作成する

H2 DatabaseとHadoopでBLOBをHDFS上にアップロードする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists hdfs_put_binary 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_binary(String uri, String user,
String path, Blob blob)
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( blob == null ){
throw new Exception("blob is not specified.");
}

FileSystem fs = FileSystem.get(
URI.create(uri), new Configuration(), user);

FSDataOutputStream fos = null;
InputStream bis = null;
byte buf[] = new byte[8192];
int rs = 0;
try
{
bis = blob.getBinaryStream();
fos = fs.create(new Path(path));
while( (rs = bis.read(buf)) != -1 ){
fos.write(buf, 0, rs);
}
fos.flush();
}
finally
{
if( bis != null )bis.close();
if( fos != null )fos.close();
}

return new Integer(0);
}
$$


実行例
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
hdfs_get_binary(
'hdfs://192.168.1.81:9000/',
'hadoop', '/opt/hadoop-data/SF.JPG')
);
select hdfs_put_binary(
'hdfs://192.168.1.81:9000/',
'hadoop', '/opt/hadoop-data/SF_bak.JPG', c2)
from blobtest
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に関する他の記事はこちらを参照してください。

2011年1月17日月曜日

H2 DatabaseとHadoopでCLOBをHDFS上にアップロードする関数を作成する

H2 DatabaseとHadoopでCLOBをHDFS上にアップロードする関数を作成するには、以下のコードを実行します。

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に関する他の記事はこちらを参照してください。

2011年1月16日日曜日

H2 DatabaseとHadoopでHDFS上のファイルをBLOBとして返す関数を作成する

H2 DatabaseとHadoopでHDFS上のファイルをBLOBとして返す関数を作成するには、以下の手順を実行します。

1.以下のソースファイルをantなどでコンパイルします。
HdfsGetBinaryFunction.java
package com.serverarekore.h2;
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import java.net.*;

public class HdfsGetBinaryFunction
{
static public InputStream hdfs_get_binary(String uri,
String user, String path)
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.");
}

FileSystem fs = FileSystem.get(
URI.create(uri), new Configuration(), user);
InputStream ris = null;

if( fs.exists(new Path(path)) == false ){
return null;
}
InputStream is = fs.open(new Path(path));

final File tf = File.createTempFile("lobtmp", null);
FileOutputStream fos = new FileOutputStream(tf.getAbsolutePath());

byte buf[] = new byte[8192];
int rs = -1;
try
{
while( (rs = is.read(buf)) != -1 ){
fos.write(buf, 0, rs);
}
}
finally
{
if( fos != null )fos.close();
if( is != null )is.close();
}

ris = new InputStream(){
private final InputStream fis = new FileInputStream(tf);
private boolean deleted = false;
private final File fn = tf;
public int autoDelete(int sz) throws IOException
{
if(sz < 0 ){
close();
}
return sz;
}
public void close() throws IOException
{
if( !deleted ){
fis.close();
fn.delete();
deleted = true;
}
}
public int read(byte buf[], int off, int len) throws IOException
{
return deleted?-1:autoDelete(fis.read(buf, off, len));
}

public int read(byte buf[]) throws IOException
{
return deleted?-1:autoDelete(fis.read(buf));
}

public int read() throws IOException
{
return deleted?-1:autoDelete(fis.read());
}
};

return ris;
}
}


build.xmlのサンプル
<project name="H2Functions" default="compile" basedir=".">
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement location="c:/Program Files/H2/bin/h2-1.2.147.jar"/>
</path>
<target name="compile">
<echo message="project: ${ant.project.name}"/>
<mkdir dir="build/" />
<javac srcdir="src/" destdir="build/"
deprecation="on" debug="on">
<classpath>
<path refid="lib.classpath"/>
</classpath>
</javac>
<jar destfile="c:/share/jars/HdfsGetBinary.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


2.システム環境変数CLASSPATHにコンパイルしてできたHdfsGetBinary.jarを追加し、
H2 Databaseのサービスを再起動。

3.H2 Consoleから以下のコマンドを実行して、ファンクション作成。
create alias hdfs_get_binary for 
"com.serverarekore.h2.HdfsGetBinaryFunction.hdfs_get_binary";

4.以下のようなSQLでHDFS上のファイルをBLOBに格納できます。
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
hdfs_get_binary(
'hdfs://192.168.1.81:9000/',
'user', '/opt/hadoop-data/SF.JPG')
);

※以下の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に関する他の記事はこちらを参照してください。

2011年1月15日土曜日

H2 DatabaseとHadoopでHDFS上のファイルをCLOBとして返す関数を作成する

H2 DatabaseとHadoopでHDFS上のファイルをCLOBとして返す関数を作成するには、以下の手順を実行します。

1.以下のソースファイルをantなどでコンパイルします。
HdfsGetAsciiFunction.java
package com.serverarekore.h2;
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import java.net.*;

public class HdfsGetAsciiFunction
{
static public Reader hdfs_get_ascii(String uri,
String user, String path)
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.");
}

FileSystem fs = FileSystem.get(
URI.create(uri), new Configuration(), user);
Reader reader = null;

if( fs.exists(new Path(path)) == false ){
return null;
}
InputStreamReader isr = new InputStreamReader(
fs.open(new Path(path))
);

final File tf = File.createTempFile("lobtmp", null);
FileWriter fw = new FileWriter(tf.getAbsolutePath());

char buf[] = new char[8192];
int rs = -1;
try
{
while( (rs = isr.read(buf)) != -1 ){
fw.write(buf, 0, rs);
}
}
finally
{
fw.close();
isr.close();
}

reader = new Reader(){
private final Reader reader = new FileReader(tf);
private boolean deleted = false;
private final File fn = tf;
public int autoDelete(int sz) throws IOException
{
if(sz < 0 ){
close();
}
return sz;
}
public void close() throws IOException
{
if( !deleted ){
reader.close();
fn.delete();
deleted = true;
}
}
public int read(char buf[], int off, int len) throws IOException
{
return deleted?-1:autoDelete(reader.read(buf, off, len));
}

public int read(char buf[]) throws IOException
{
return deleted?-1:autoDelete(reader.read(buf));
}

public int read() throws IOException
{
return deleted?-1:autoDelete(reader.read());
}
public int read(java.nio.CharBuffer target) throws IOException
{
return deleted?-1:autoDelete(reader.read(target));
}
};

return reader;
}
}


build.xmlのサンプル
<project name="H2Functions" default="compile" basedir=".">
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement location="c:/Program Files/H2/bin/h2-1.2.147.jar"/>
</path>
<target name="compile">
<echo message="project: ${ant.project.name}"/>
<mkdir dir="build/" />
<javac srcdir="src/" destdir="build/"
deprecation="on" debug="on">
<classpath>
<path refid="lib.classpath"/>
</classpath>
</javac>
<jar destfile="c:/share/jars/HdfsGetAscii.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


2.システム環境変数CLASSPATHにコンパイルしてできたHdfsGetAscii.jarを追加し、
H2 Databaseのサービスを再起動。

3.H2 Consoleから以下のSQLを実行して、ファンクション作成。
create alias hdfs_get_ascii for 
"com.serverarekore.h2.HdfsGetAsciiFunction.hdfs_get_ascii";

4.以下のようなSQLでHdfs上のファイルをCLOBに格納できます。
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 * from clobtest;


※以下の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に関する他の記事はこちらを参照してください。