2011年1月14日金曜日

H2 DatabaseとHadoopでHDFS上のファイル・ディレクトリが存在するか調べる関数を作成する

H2 DatabaseとHadoopでHDFS上のファイル・ディレクトリが存在するか調べる関数を作成するには、以下のスクリプトを実行します。

create alias if not exists hdfs_exists 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
Boolean hdfs_exists(String uri, String path)
throws Exception
{
if( uri == null ){
throw new Exception("uri is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}

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

return fs.exists(new Path(path));
}
$$


実行例
select 
hdfs_exists('hdfs://192.168.1.81:9000/',
'/opt/hadoop-data/test.txt')
;


※以下の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月13日木曜日

H2 DatabaseとHadoopでHDFSのデフォルトブロックサイズを返す関数を作成する

H2 DatabaseとHadoopでHDFSのデフォルトブロックサイズを返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists hdfs_get_default_block_size 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
Long hdfs_get_default_block_size(String uri)
throws Exception
{
if( uri == null ){
throw new Exception("uri is not specified.");
}

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

return new Long(fs.getDefaultBlockSize());
}
$$


実行例
select 
hdfs_get_default_block_size('hdfs://192.168.1.81:9000/')
;


※以下の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月12日水曜日

H2 DatabaseとHadoopでHDFS上にディレクトリを作成する関数を作成する

H2 DatabaseとHadoopでHDFS上にディレクトリを作成する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists hdfs_mkdirs 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
Boolean hdfs_mkdirs(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);

return fs.mkdirs(new Path(path));
}
$$


実行例
select 
hdfs_mkdirs('hdfs://192.168.1.81:9000/', 'hadoop',
'/opt/hadoop-data/test1')
;


※以下の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月11日火曜日

H2 DatabaseとHadoopでHDFS上のファイルをリネームする関数を作成する

H2 DatabaseとHadoopでHDFS上のファイルをリネームする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists hdfs_rename 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
Boolean hdfs_rename(String uri, String user,
String srcpath, String destpath)
throws Exception
{
if( uri == null ){
throw new Exception("uri is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( srcpath == null ){
throw new Exception("srcpath is not specified.");
}
if( destpath == null ){
throw new Exception("destpath time is not specified.");
}

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

return fs.rename(new Path(srcpath), new Path(destpath));
}
$$


実行例
select
hdfs_rename('hdfs://192.168.1.81:9000/', 'hadoop',
'/opt/hadoop-data/test.txt',
'/opt/hadoop-data/test_bak.txt')
;

※以下の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月10日月曜日

H2 DatabaseとHadoopでHDFS上のパスの変更日時・アクセス日時を設定する関数を作成する

H2 DatabaseとHadoopでHDFS上のパスの変更日時・アクセス日時を設定する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists hdfs_set_times 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 org.apache.hadoop.fs.permission.*;
import java.net.*;
@CODE
Boolean hdfs_set_times(String uri, String user,
String path, Timestamp mtime,
Timestamp atime)
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( mtime == null ){
throw new Exception("modification time is not specified.");
}
if( atime == null ){
throw new Exception("access time is not specified.");
}

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

fs.setTimes(new Path(path), mtime.getTime(), atime.getTime());
return new Boolean(true);
}
$$


実行例
select 
hdfs_set_times('hdfs://192.168.1.81:9000/', 'hadoop',
'/opt/hadoop-data/test.txt', current_timestamp,
current_timestamp)
;


※以下の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月9日日曜日

H2 DatabaseとHadoopでHDFS上のパスのパーミッションを設定する関数を作成する

H2 DatabaseとHadoopでHDFS上のパスのパーミッションを設定する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists hdfs_set_permission 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 org.apache.hadoop.fs.permission.*;
import java.net.*;
@CODE
Boolean hdfs_set_permission(String uri, String user,
String path, String permission)
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( permission == null ){
throw new Exception("permission is not specified.");
}

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

fs.setPermission(new Path(path), new FsPermission(permission));
return new Boolean(true);
}
$$


実行例
select 
hdfs_set_permission('hdfs://192.168.1.81:9000/', 'hadoop',
'/opt/hadoop-data/test.txt', '766')
;


※以下の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月8日土曜日

H2 DatabaseとHadoopでHDFS上のパスのオーナー・グループを設定する関数を作成する

H2 DatabaseとHadoopでHDFS上のパスのオーナー・グループを設定する関数を作成するには、以下のスクリプトを実行します。

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

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

fs.setOwner(new Path(path), owner, group);
return new Boolean(true);
}
$$


実行例
select 
hdfs_set_owner('hdfs://192.168.1.81:9000/', 'hadoop',
'/opt/hadoop-data/test.txt', 'test', 'testgroup')
;


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