2011年3月19日土曜日

H2 DatabaseとJRedisで複数のセットから積集合を作成する関数を作成する

H2 DatabaseとJRedisで複数のセットから積集合を作成する関数を作成するには、以下のスクリプトを実行します。

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

JRedisClient jrc = new JRedisClient(host, port);
jrc.sinterstore(dest, set1, set2);
return new Integer(0);
}
$$

実行例
select jredis_del('192.168.1.25', 6379, 'set1');
select jredis_del('192.168.1.25', 6379, 'set2');
select jredis_sadd('192.168.1.25', 6379, 'set1', 'value1');
select jredis_sadd('192.168.1.25', 6379, 'set1', 'value2');
select jredis_sadd('192.168.1.25', 6379, 'set1', 'value3');
select jredis_sadd('192.168.1.25', 6379, 'set2', 'value2');
select jredis_sadd('192.168.1.25', 6379, 'set2', 'value5');
select jredis_sinterstore('192.168.1.25', 6379, 'rs', 'set1', 'set2');
select * from jredis_smembers('192.168.1.25', 6379, 'rs');

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

0 件のコメント:

コメントを投稿