前提
- lib/kyotocabinet.jar(Kyoto CabinetのJavaバインディング)をクラスパスに含める
- Groovyスクリプト(下記参照)の実行ディレクトリにjkyotocabinet.dll(Kyoto CabinetのDLL)が存在する
手順
下記のGroovyスクリプトを用意する。なお、このスクリプトは、Kyoto CabinetのJavaDocにあるサンプルをGroovyスクリプトにしただけのもの。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kyotocabinet.*; | |
// create the object | |
DB db = new DB(); | |
// open the database | |
if (!db.open("casket.kch", DB.OWRITER | DB.OCREATE)){ | |
System.err.println("open error: " + db.error()); | |
} | |
// store records | |
if (!db.set("foo", "hop") || | |
!db.set("bar", "step") || | |
!db.set("baz", "jump")){ | |
System.err.println("set error: " + db.error()); | |
} | |
// retrieve records | |
String value = db.get("foo"); | |
if (value != null){ | |
System.out.println(value); | |
} else { | |
System.err.println("set error: " + db.error()); | |
} | |
// traverse records | |
Cursor cur = db.cursor(); | |
cur.jump(); | |
String[] rec; | |
while ((rec = cur.get_str(true)) != null) { | |
System.out.println(rec[0] + ":" + rec[1]); | |
} | |
cur.disable(); | |
if(!db.close()){ | |
System.err.println("close error: " + db.error()); | |
} |
Gradleから下記のGroovyスクリプトを実行するには、下記のbuild.gradleを使用する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'groovy' | |
apply plugin: 'eclipse' | |
repositories { | |
flatDir(dirs: file("lib")) | |
} | |
dependencies { | |
groovy localGroovy() | |
compile ":kyotocabinet:@jar" | |
} | |
task execJava (type: JavaExec) { | |
classpath = sourceSets.main.runtimeClasspath | |
main = "KCSample" | |
} |
execJava
タスクを実行すると、上記のGroovyスクリプトが実行される。
0 件のコメント:
コメントを投稿