2011年2月20日日曜日

H2 DatabaseとTwitter4Jを使用してフォロワーのユーザ名を取得する関数を作成する

H2 DatabaseとTwitter4Jを使用してフォロワーのユーザ名を取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists twitter_get_follower_ids as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import twitter4j.*;
import twitter4j.http.*;
import twitter4j.conf.*;
@CODE
ResultSet twitter_get_follower_ids(String consumerKey,
String consumerSecret, String accessToken,
String accessTokenSecret)
throws Exception
{
if( consumerKey == null ){
throw new Exception("consumerKey is not specified.");
}
if( consumerSecret == null ){
throw new Exception("consumerSecret is not specified.");
}
if( accessToken == null ){
throw new Exception("accessToken is not specified.");
}
if( accessTokenSecret == null ){
throw new Exception("accessTokenSecret is not specified.");
}

SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("ID", Types.NUMERIC, 20, 0);
rs.addColumn("NAME", Types.VARCHAR, 100, 0);

Properties prop = new Properties();
prop.put("oauth.consumerKey", consumerKey);
prop.put("oauth.consumerSecret", consumerSecret);
prop.put("oauth.accessToken", accessToken);
prop.put("oauth.accessTokenSecret", accessTokenSecret);
PropertyConfiguration pc =
new PropertyConfiguration(prop);

OAuthAuthorization oauth = new OAuthAuthorization(pc);
TwitterFactory tf = new TwitterFactory();
Twitter twitter = tf.getInstance(oauth);
IDs ids = twitter.getFollowersIDs();
int idns[] = ids.getIDs();
for(int idx=0;idx<idns.length;idx++){
User user = twitter.showUser(idns[idx]);
rs.addRow(user.getId(), user.getScreenName());
}
return rs;
}
$$

実行例
select * from twitter_get_follower_ids('xxxx',
'xxxx',
'xxxx',
'xxxx');

※以下のjarをCLASSPATH環境変数に追加
twitter4j-core-2.1.12-SNAPSHOT.jar

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

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

2011年2月19日土曜日

H2 DatabaseとJRedisでkeyをリネームする関数を作成する

H2 DatabaseとJRedisでkeyをリネームする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jredis_rename as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import org.jredis.ri.alphazero.*;
import org.jredis.ri.alphazero.support.*;
@CODE
Integer jredis_rename(String host, Integer intPort,
String oldkey, String newkey)
throws Exception
{
if( host == null ){
throw new Exception("host is not specified.");
}
int port = 6379;
if( intPort != null ){
port = intPort.intValue();
}
if( oldkey == null ){
throw new Exception("old key is not specified.");
}
if( newkey == null ){
throw new Exception("new key is not specified.");
}

JRedisClient jrc = new JRedisClient(host, port);
jrc.rename(oldkey, newkey);
return new Integer(0);
}
$$

実行例
select jredis_rename('192.168.1.25', 6379, 'key1', 'newkey1')

※以下のjarをCLASSPATH環境変数に追加
jredis-core-all-a.0-SNAPSHOT-jar-with-dependencies.jar

○動作環境
JDK6 Update23, H2 Database 1.3.149 (2011-01-07), JRedis2.0.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2011年2月18日金曜日

H2 DatabaseとTwitter4Jを使用して接続ユーザーのタイムラインを取得する関数を作成する

H2 DatabaseとTwitter4Jを使用して接続ユーザーのタイムラインを取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists twitter_get_user_timeline as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import twitter4j.*;
import twitter4j.http.*;
import twitter4j.conf.*;
@CODE
ResultSet twitter_get_user_timeline(String consumerKey,
String consumerSecret, String accessToken,
String accessTokenSecret, Integer page)
throws Exception
{
if( consumerKey == null ){
throw new Exception("consumerKey is not specified.");
}
if( consumerSecret == null ){
throw new Exception("consumerSecret is not specified.");
}
if( accessToken == null ){
throw new Exception("accessToken is not specified.");
}
if( accessTokenSecret == null ){
throw new Exception("accessTokenSecret is not specified.");
}
if( page == null ){
return null;
}

SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("ID", Types.NUMERIC, 20, 0);
rs.addColumn("CREATEDAT", Types.TIMESTAMP, 0, 0);
rs.addColumn("NAME", Types.VARCHAR, 100, 0);
rs.addColumn("TWEET", Types.VARCHAR, 140, 0);

Properties prop = new Properties();
prop.put("oauth.consumerKey", consumerKey);
prop.put("oauth.consumerSecret", consumerSecret);
prop.put("oauth.accessToken", accessToken);
prop.put("oauth.accessTokenSecret", accessTokenSecret);
PropertyConfiguration pc =
new PropertyConfiguration(prop);

OAuthAuthorization oauth = new OAuthAuthorization(pc);
TwitterFactory tf = new TwitterFactory();
Twitter twitter = tf.getInstance(oauth);
ResponseList<Status> rl = twitter.getUserTimeline(new Paging(page));
for(Status status : rl){
rs.addRow(status.getId(),
new Timestamp(status.getCreatedAt().getTime()),
status.getUser().getScreenName(),
status.getText());
}
return rs;
}
$$

実行例
select * from twitter_get_user_timeline('xxxx',
'xxxx',
'xxxx',
'xxxx', 1);

※以下のjarをCLASSPATH環境変数に追加
twitter4j-core-2.1.12-SNAPSHOT.jar

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

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

2011年2月17日木曜日

H2 DatabaseとOrion SSH2を使用してサーバ上でコマンドを実行する関数を作成する

H2 DatabaseとOrion SSH2を使用してサーバ上でコマンドを実行する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_exec as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
String orionssh2_exec(String host, String user,
String password, String cmd)
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( cmd == null ){
throw new Exception("cmd is not specified.");
}

com.trilead.ssh2.Connection conn = null;
Session sess = null;
String result = null;

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

// コマンド実行
sess = conn.openSession();
sess.execCommand(cmd);
// 標準出力
BufferedReader stdout = new BufferedReader(
new InputStreamReader(
new StreamGobbler(sess.getStdout()), "UTF-8"));
StringBuffer sbuf = new StringBuffer();
String line = "";
while((line = stdout.readLine()) != null ){
sbuf.append(line);
sbuf.append("\n");
}
result = sbuf.toString();
}
finally
{
if( sess != null )sess.close();
if( conn != null )conn.close();
}

return result;
}
$$


実行例
select orionssh2_exec(
'192.168.1.25',
'user', 'password',
'ls -alF')


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

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

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

2011年2月16日水曜日

H2 DatabaseとTwitter4Jを使用してフォローしている数を取得する関数を作成する

H2 DatabaseとTwitter4Jを使用してフォローしている数を取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists twitter_get_num_friends as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import twitter4j.*;
import twitter4j.http.*;
import twitter4j.conf.*;
@CODE
Integer twitter_get_num_friends(String consumerKey,
String consumerSecret, String accessToken,
String accessTokenSecret)
throws Exception
{
if( consumerKey == null ){
throw new Exception("consumerKey is not specified.");
}
if( consumerSecret == null ){
throw new Exception("consumerSecret is not specified.");
}
if( accessToken == null ){
throw new Exception("accessToken is not specified.");
}
if( accessTokenSecret == null ){
throw new Exception("accessTokenSecret is not specified.");
}

Properties prop = new Properties();
prop.put("oauth.consumerKey", consumerKey);
prop.put("oauth.consumerSecret", consumerSecret);
prop.put("oauth.accessToken", accessToken);
prop.put("oauth.accessTokenSecret", accessTokenSecret);
PropertyConfiguration pc =
new PropertyConfiguration(prop);

OAuthAuthorization oauth = new OAuthAuthorization(pc);
TwitterFactory tf = new TwitterFactory();
Twitter twitter = tf.getInstance(oauth);
return new Integer(twitter.getAccountTotals().getFriends());
}
$$

実行例
select twitter_get_num_friends('xxxx',
'xxxx',
'xxxx',
'xxxx');

※以下のjarをCLASSPATH環境変数に追加
twitter4j-core-2.1.12-SNAPSHOT.jar

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

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

H2 DatabaseとJRedisで指定したkeyが存在するかどうかを返す関数を作成する

H2 DatabaseとJRedisで指定したkeyが存在するかどうかを返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jredis_exists as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import org.jredis.ri.alphazero.*;
import org.jredis.ri.alphazero.support.*;
@CODE
boolean jredis_exists(String host, Integer intPort,
String key)
throws Exception
{
if( host == null ){
throw new Exception("host is not specified.");
}
int port = 6379;
if( intPort != null ){
port = intPort.intValue();
}
if( key == null ){
throw new Exception("key is not specified.");
}

JRedisClient jrc = new JRedisClient(host, port);
return jrc.exists(key);
}
$$

実行例
select jredis_exists('192.168.1.25', 6379, 'key1');
select jredis_exists('192.168.1.25', 6379, 'not_exist');

※以下のjarをCLASSPATH環境変数に追加
jredis-core-all-a.0-SNAPSHOT-jar-with-dependencies.jar

○動作環境
JDK6 Update23, H2 Database 1.3.149 (2011-01-07), JRedis2.0.0
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

2011年2月15日火曜日

H2 DatabaseとOrion SSH2を使用してBLOBをSFTP上へアップロードする関数を作成する

H2 DatabaseとOrion SSH2を使用してBLOBをSFTP上へアップロードする関数を作成するには、以下のスクリプトを実行します。

create alias if not exists orionssh2_put_binary as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;
@CODE
Integer orionssh2_put_binary(String host, String user,
String password, String path, Blob blob)
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 ){
throw new Exception("path is not specified.");
}

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;

InputStream bis = null;
SFTPv3FileHandle fh = 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");

// ファイルをアップロード
fh = sftpc.createFile(path);
bis = blob.getBinaryStream();
byte buf[] = new byte[8192];
long fo = 0;
int sz = 0;
while((sz = bis.read(buf, 0, 8192)) != -1){
sftpc.write(fh, fo, buf, 0, sz);
fo += sz;
}

}
finally
{
if( bis != null )bis.close();
if( fh != null )sftpc.closeFile(fh);
if( sftpc != null )sftpc.close();
if( conn != null )conn.close();
}

return new Integer(0);
}
$$


実行例
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
orionssh2_get_binary(
'192.168.1.25',
'user', 'password',
'sf.png')
);
select orionssh2_put_binary(
'192.168.1.25',
'user', 'password',
'upload.png', c2)
from blobtest
where c1 = 1;


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

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

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