2011年2月28日月曜日

H2 DatabaseとJRedisでリストの指定した範囲の値を列挙する関数を作成する

H2 DatabaseとJRedisでリストの指定した範囲の値を列挙する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jredis_lrange 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
ResultSet jredis_lrange(String host, Integer intPort,
String key, Long from, Long to)
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.");
}
if( from == null ){
throw new Exception("from is not specified.");
}
if( to == null ){
throw new Exception("to is not specified.");
}
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("ELEMENT", Types.VARCHAR, 4096, 0);

JRedisClient jrc = new JRedisClient(host, port);
for(byte elem[] : jrc.lrange(key, from, to)){
rs.addRow(elem==null?null:DefaultCodec.toStr(elem));
}
return rs;
}
$$

実行例
select * from jredis_lrange('192.168.1.25', 6379, 'list1', 0, -1)

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

H2 DatabaseとJRedisでリストの最後に値を追加する関数を作成する

H2 DatabaseとJRedisでリストの最後に値を追加する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jredis_rpush 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_rpush(String host, Integer intPort,
String key, String value)
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.");
}
if( value == null ){
throw new Exception("value is not specified.");
}

JRedisClient jrc = new JRedisClient(host, port);
jrc.rpush(key, value);
return new Integer(0);
}
$$

実行例
select jredis_rpush('192.168.1.25', 6379, 'list1', 'value1')

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

H2 DatabaseとJRedisでキーを削除する関数を作成する

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

create alias if not exists jredis_del 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_del(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.del(key);
}
$$

実行例
select jredis_set('192.168.1.25', 6379, 'key1', 'value1');
select jredis_del('192.168.1.25', 6379, 'key1');
select jredis_exists('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に関する他の記事はこちらを参照してください。

2011年2月25日金曜日

H2 DatabaseとJRedisで最後にディスクに保存した日付を取得する関数を作成する

H2 DatabaseとJRedisで最後にディスクに保存した日付を取得する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jredis_lastsave 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
java.sql.Date jredis_lastsave(String host, Integer intPort)
throws Exception
{
if( host == null ){
throw new Exception("host is not specified.");
}
int port = 6379;
if( intPort != null ){
port = intPort.intValue();
}

JRedisClient jrc = new JRedisClient(host, port);
return new java.sql.Date(jrc.lastsave());
}
$$

実行例
select jredis_lastsave('192.168.1.25', 6379);

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

H2 DatabaseとJRedisでkeyの数を取得する関数を作成する

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

create alias if not exists jredis_dbsize 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
long jredis_dbsize(String host, Integer intPort)
throws Exception
{
if( host == null ){
throw new Exception("host is not specified.");
}
int port = 6379;
if( intPort != null ){
port = intPort.intValue();
}

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

実行例
select jredis_dbsize('192.168.1.25', 6379);

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

H2 DatabaseとJRedisでkeyの値をデクリメントする関数を作成する

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

create alias if not exists jredis_decrby 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
long jredis_decrby(String host, Integer intPort,
String key, Integer delta)
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.");
}
if( delta == null ){
throw new Exception("delta is not specified.");
}

JRedisClient jrc = new JRedisClient(host, port);
return jrc.decrby(key, delta.intValue());
}
$$

実行例
select jredis_set('192.168.1.25', 6379, 'key1', '10');
select jredis_decrby('192.168.1.25', 6379, 'key1', 2);

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

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

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

create alias if not exists twitter_get_friends_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_friends_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.getFriendsIDs();
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_friends_ids('xxxx',
'xxxx',
'xxxx',
'xxxx');


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