2011年2月12日土曜日

H2 DatabaseとTwitter4Jを使用してお気に入りの数を取得する関数を作成する

H2 DatabaseとTwitter4Jを使用してお気に入りの数を取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists twitter_get_favorites as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import twitter4j.*;
import twitter4j.http.*;
import twitter4j.conf.*;
@CODE
Integer twitter_get_favorites(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().getFavorites());
}
$$

実行例
select twitter_get_favorites('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月11日金曜日

H2 DatabaseとJRedisで指定したkeyのvalueを取得する関数を作成する

H2 DatabaseとJRedisで指定したkeyのvalueを取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jredis_get 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
String jredis_get(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);
byte result[] = jrc.get(key);
return result==null?null:DefaultCodec.toStr(result);
}
$$

実行例
select jredis_get('192.168.1.25', 6379, 'key1')

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

H2 DatabaseとOrion SSH2を使用してSFTPでファイルを移動・リネームする関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでファイルを移動・リネームする関数を作成するには、以下のスクリプトを実行します。

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

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;
String result = 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");

// ファイルを移動・リネーム
sftpc.mv(path1, path2);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return new Integer(0);
}
$$


実行例
select orionssh2_mv('192.168.1.25', 'user', 'password', 'sf.png', './test/sf2.png')


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

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

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

2011年2月10日木曜日

H2 DatabaseとTwitter4Jを使用してツィート回数を取得する関数を作成する

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

create alias if not exists twitter_get_updates as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import twitter4j.*;
import twitter4j.http.*;
import twitter4j.conf.*;
@CODE
Integer twitter_get_updates(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().getUpdates());
}
$$

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

H2 DatabaseとOrion SSH2を使用してSFTPでフルパスを取得する関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでフルパスを取得する関数を作成するには、以下のスクリプトを実行します。

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

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;
String result = 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");

// フルパスを取得
result = sftpc.canonicalPath(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return result;
}
$$


実行例
select orionssh2_get_fullpath('192.168.1.25', 'user', 'password', 'sf.png')


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

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

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

2011年2月8日火曜日

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

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

create alias if not exists twitter_get_screen_name as $$ 
import java.io.*;
import java.util.*;
import java.sql.*;
import twitter4j.*;
import twitter4j.http.*;
import twitter4j.conf.*;
@CODE
String twitter_get_screen_name(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 twitter.getScreenName();
}
$$

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

H2 DatabaseとOrion SSH2を使用してSFTPでシンボリックリンクのターゲットを表示する関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでシンボリックリンクのターゲットを表示する関数を作成するには、以下のスクリプトを実行します。

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

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;
String result = 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");

// シンボリックリンクのターゲットを取得
result = sftpc.readLink(path);
}
finally
{
if( sftpc != null )sftpc.close();
if( sftpc != null )conn.close();
}
return result;
}
$$


実行例
select orionssh2_read_link('192.168.1.25', 'user', 'password', 'symlink_sf.png')


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

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

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