ラベル hadoop の投稿を表示しています。 すべての投稿を表示
ラベル hadoop の投稿を表示しています。 すべての投稿を表示

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

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

2011年1月7日金曜日

H2 DatabaseとHadoopでHDFS上のパスのファイル・ディレクトリを削除する関数を作成する

H2 DatabaseとHadoopでHDFS上のパスのファイル・ディレクトリを削除する関数を作成するには、以下のコードを実行します。

create alias if not exists hdfs_delete 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_delete(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 new Boolean(fs.delete(new Path(path), true));
}
$$


実行例
select 
hdfs_delete('hdfs://192.168.1.81:9000/', 'hadoop',
'/opt/hadoop-data/test2.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月6日木曜日

H2 DatabaseとHadoopでHDFS上のファイルのレプリケーションファクターを返す関数を作成する

H2 DatabaseとHadoopでHDFS上のファイルのレプリケーションファクターを返す関数を作成するには、以下のスクリプトを実行します。

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

FileStatus file = fs.getFileStatus(new Path(path));

return new Short(file.getReplication());
}
$$


実行例
select 
hdfs_get_replication('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月5日水曜日

H2 DatabaseとHadoopでHDFS上のパスの変更日時を返す関数を作成する

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

create alias if not exists hdfs_get_modification_time 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
java.sql.Timestamp hdfs_get_modification_time(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());

FileStatus file = fs.getFileStatus(new Path(path));

return new java.sql.Timestamp(file.getModificationTime());
}
$$


実行例
select 
hdfs_get_modification_time('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月4日火曜日

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

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

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

FileStatus file = fs.getFileStatus(new Path(path));

return new Long(file.getLen());
}
$$


実行例
select 
hdfs_get_len('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月3日月曜日

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

H2 DatabaseとHadoopでHDFS上のファイルのブロックサイズを返す関数を作成するには、以下のコードを実行します。

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

FileStatus file = fs.getFileStatus(new Path(path));

return new Long(file.getBlockSize());
}
$$


実行例
select 
hdfs_get_block_size('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月2日日曜日

H2 DatabaseとHadoopでHDFS上のパスのアクセス時間を返す関数を作成する

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

create alias if not exists hdfs_get_access_time 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
java.sql.Timestamp hdfs_get_access_time(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());

FileStatus file = fs.getFileStatus(new Path(path));

return new java.sql.Timestamp(file.getAccessTime());
}
$$


実行例
select 
hdfs_get_access_time('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月1日土曜日

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

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

create alias if not exists hdfs_get_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 java.net.*;
@CODE
String hdfs_get_permission(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());

FileStatus file = fs.getFileStatus(new Path(path));

return file.getPermission().toString();
}
$$


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

2010年12月31日金曜日

H2 DatabaseとHadoopでHDFS上のパスのグループを返す関数を作成する

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

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

FileStatus file = fs.getFileStatus(new Path(path));

return file.getGroup();
}
$$


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

2010年12月30日木曜日

H2 DatabaseとHadoopでHDFS上のパスのオーナーを返す関数を作成する

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

create alias if not exists hdfs_get_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
String hdfs_get_owner(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());

FileStatus file = fs.getFileStatus(new Path(path));

return file.getOwner();
}
$$


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