2010年11月4日木曜日

H2 DatabaseとJCIFSでWindows共有上のファイルのサイズを返す関数を作成する

H2 DatabaseとJCIFSでWindows共有上のファイルのサイズを返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jcifs_get_filesize as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import jcifs.smb.*;
@CODE
long jcifs_get_filesize(String domain,
String user, String password, String server,
String path, String filename)
throws Exception
{
if( domain == null ){
throw new Exception("domain is not specified.");
}
user = (user == null)?"":user;
password = (password == null)?"":password;
if( server == null ){
throw new Exception("server is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}
if( filename == null ){
throw new Exception("filename is not specified.");
}

SmbFile file = new SmbFile(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path + "/" + filename
);

return file.length();
}
$$


実行例
select jcifs_get_filesize(
'LIFERAY1',
'test1', 'test1', 'liferay1',
'share', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとJCIFSのjar
(jcifs-1.3.15.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), JCIFS 1.3.15
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年11月3日水曜日

H2 DatabaseとJCIFSでWindows共有上のファイルを削除する関数を作成する

H2 DatabaseとJCIFSでWindows共有上のファイルを削除する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jcifs_delete_file as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import jcifs.smb.*;
@CODE
int jcifs_delete_file(String domain,
String user, String password, String server,
String path, String filename)
throws Exception
{
if( domain == null ){
throw new Exception("domain is not specified.");
}
user = (user == null)?"":user;
password = (password == null)?"":password;
if( server == null ){
throw new Exception("server is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}
if( filename == null ){
throw new Exception("filename is not specified.");
}

SmbFile file = new SmbFile(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path + "/" + filename
);

file.delete();

return 0;
}
$$


実行例
call jcifs_delete_file(
'LIFERAY1',
'test1', 'test1', 'liferay1',
'share', 'SF2.JPG');


※システム環境変数CLASSPATHにtools.jarとJCIFSのjar
(jcifs-1.3.15.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), JCIFS 1.3.15
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年11月2日火曜日

H2 DatabaseとJCIFSでWindows共有上のファイルの作成日時を返す関数を作成する

H2 DatabaseとJCIFSでWindows共有上のファイルの作成日時を返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jcifs_get_create_time as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import jcifs.smb.*;
@CODE
Timestamp jcifs_get_create_time(String domain,
String user, String password, String server,
String path, String filename)
throws Exception
{
if( domain == null ){
throw new Exception("domain is not specified.");
}
user = (user == null)?"":user;
password = (password == null)?"":password;
if( server == null ){
throw new Exception("server is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}
if( filename == null ){
throw new Exception("filename is not specified.");
}

SmbFile file = new SmbFile(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path + "/" + filename
);

return new Timestamp(file.createTime());
}
$$


実行例
select jcifs_get_create_time(
'LIFERAY1',
'test1', 'test1', 'liferay1',
'share', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとJCIFSのjar
(jcifs-1.3.15.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), JCIFS 1.3.15
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年11月1日月曜日

H2 DatabaseとJCIFSでWindows共有上のファイルをコピーする関数を作成する

H2 DatabaseとJCIFSでWindows共有上のファイルをコピーする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jcifs_copy_file as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import jcifs.smb.*;
@CODE
int jcifs_copy_file(String domain,
String user, String password, String server,
String path, String filename,
String path2, String filename2)
throws Exception
{
if( domain == null ){
throw new Exception("domain is not specified.");
}
user = (user == null)?"":user;
password = (password == null)?"":password;
if( server == null ){
throw new Exception("server is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}
if( filename == null ){
throw new Exception("filename is not specified.");
}
if( path2 == null ){
throw new Exception("path2 is not specified.");
}
if( filename2 == null ){
throw new Exception("filename2 is not specified.");
}

SmbFile src = new SmbFile(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path + "/" + filename
);
SmbFile dst = new SmbFile(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path2 + "/" + filename2
);
src.copyTo(dst);

return 0;
}
$$


実行例
call jcifs_copy_file(
'LIFERAY1',
'test1', 'test1', 'liferay1',
'share', 'SF.JPG',
'share', 'SF2.JPG');


※システム環境変数CLASSPATHにtools.jarとJCIFSのjar
(jcifs-1.3.15.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), JCIFS 1.3.15
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月31日日曜日

H2 DatabaseとJCIFSでBLOBをファイルとしてWindows共有上にアップロードする関数を作成する

H2 DatabaseとJCIFSでBLOBをファイルとしてWindows共有上にアップロードする関数を作成するには、以下のスクリプトを実行します。

drop alias jcifs_put_binary;
create alias if not exists jcifs_put_binary as $$
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import jcifs.smb.*;
@CODE
int jcifs_put_binary(String domain,
String user, String password, String server,
String path, String filename, Blob blob)
throws Exception
{
if( domain == null ){
throw new Exception("domain is not specified.");
}
user = (user == null)?"":user;
password = (password == null)?"":password;
if( server == null ){
throw new Exception("server is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}
if( filename == null ){
throw new Exception("filename is not specified.");
}


OutputStream os = null;
InputStream bis = null;
try
{
os = new SmbFileOutputStream(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path + "/" + filename);
bis = blob.getBinaryStream();

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

return 0;
}
$$


実行例
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
jcifs_get_binary('LIFERAY1',
'test1', 'test1', 'liferay1',
'share', 'SF.JPG')
);
select jcifs_put_binary('LIFERAY1',
'test1', 'test1', 'liferay1',
'share/h2', 'renamed_SF.JPG', c2)
from blobtest
where c1 = 1;


※システム環境変数CLASSPATHにtools.jarとJCIFSのjar
(jcifs-1.3.15.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), JCIFS 1.3.15
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2010年10月30日土曜日

CentOS5.5にHadoop0.21.0をインストールする

CentOS5.5にHadoop0.21.0をインストールするには、以下の手順を実行します。

○Sun JDKのdownload&install
以下のサイトからrpm版を落として、インストール
http://java.sun.com/javase/ja/6/download.html

chmod +x jdk-6u22-linux-x64-rpm.bin
./jdk-6u22-linux-x64-rpm.bin

インストールしたJDKをalternativesのリストに追加
alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_22/bin/java 200
alternatives --config javaで、インストールしたJDKを選択

vi /etc/profile
以下を最終行に追加
export JAVA_HOME=/usr/java/jdk1.6.0_22
保存して
source /etc/profile

○hadoop用のユーザ追加
groupadd -g 2000 hadoop
useradd -g hadoop -u 2000 hadoop
passwd hadoop
パスワードを2回入力

○hadoopのインストール
tar xvfz hadoop-0.21.0.tar.gz
mv hadoop-0.21.0 /opt/hadoop-0.21.0
cd /opt
chown -R hadoop:hadoop hadoop-0.21.0

vi /etc/profile
以下を最終行に追加
export HADOOP_HOME=/opt/hadoop-0.21.0
export PATH=$PATH:$HADOOP_HOME/bin
保存して
source /etc/profile

○ssh設定
su - hadoop
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
ssh hadoop0
以下のメッセージが出たらyesを入力
Are you sure you want to continue connecting (yes/no)? yes
exit

○hadoop設定

cd $HADOOP_HOME/conf
vi hadoop-env.sh
以下の行を追加
export JAVA_HOME=/usr/java/jdk1.6.0_22

vi $HADOOP_HOME/conf/core-site.xml
configuration要素を以下のように設定
<configuration>
<property>
<name>fs.default.name</name>
<value>hdfs://hadoop0:9000</value>
</property>
</configuration>

vi $HADOOP_HOME/conf/hdfs-site.xml
configuration要素を以下のように設定
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>

vi $HADOOP_HOME/conf/mapred-site.xml
configuration要素を以下のように設定
<configuration>
<property>
<name>mapred.job.tracker</name>
<value>hadoop0:9001</value>
</property>
</configuration>

vi $HADOOP_HOME/conf/masters
localhostを消してホスト名入力

vi $HADOOP_HOME/conf/slaves
localhostを消してホスト名入力

hdfs namenode -format
start-dfs.sh
start-mapred.sh

動作環境
CentOS5.5, hadoop 0.21.0, JDK6 Update22

H2 DatabaseとJCIFSでCLOBをファイルとしてWindows共有上にアップロードする関数を作成する

H2 DatabaseとJCIFSでCLOBをファイルとしてWindows共有上にアップロードする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jcifs_put_ascii as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import jcifs.smb.*;
@CODE
int jcifs_put_ascii(String domain,
String user, String password, String server,
String path, String filename, Clob clob)
throws Exception
{
if( domain == null ){
throw new Exception("domain is not specified.");
}
user = (user == null)?"":user;
password = (password == null)?"":password;
if( server == null ){
throw new Exception("server is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}
if( filename == null ){
throw new Exception("filename is not specified.");
}


OutputStream os = null;
InputStream ais = null;
try
{
os = new SmbFileOutputStream(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path + "/" + filename);
ais = clob.getAsciiStream();

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

return 0;
}
$$


実行例
drop table if exists clobtest;
create table clobtest (c1 numeric(4), c2 clob);
insert into clobtest values (1,
jcifs_get_ascii('LIFERAY1',
'test1', 'test1', 'liferay1',
'share/h2', 'titles.txt')
);
select jcifs_put_ascii('LIFERAY1',
'test1', 'test1', 'liferay1',
'share/h2', 'renamed_titles.txt', c2)
from clobtest
where c1 = 1;


※システム環境変数CLASSPATHにtools.jarとJCIFSのjar
(jcifs-1.3.15.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), JCIFS 1.3.15
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。