Hectorを使ってみる (1)
Cassandra Client LibraryにHectorなんつーのがある模様なので使ってみた
pom.xml
http://mvnrepository.com で検索する限りだと、me.prettyprintとorg.hectorclientなのがあるけど、これ前者が後者に名前変わっただけな模様なので後者を取る(ただし、Cassandra-1.1系が必要な模様)
<?xml version="1.0" ?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>省略</groupId>
<artifactId>省略</artifactId>
<version>1.0</version>
<name>省略</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cassandra</groupId>
<artifactId>cassandra-all</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>org.hectorclient</groupId>
<artifactId>hector-core</artifactId>
<version>1.1-1</version>
</dependency>
</dependencies>
</project>
HectorClient.java
import java.util.Arrays;
import java.util.List;
import me.prettyprint.cassandra.model.BasicColumnDefinition;
import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.template.ColumnFamilyResult;
import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater;
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ColumnIndexType;
import me.prettyprint.hector.api.ddl.ColumnType;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;
public class HectorClient {
public static void main(String[] args) throws Exception {
Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "127.0.0.1");
describeKeyspaces(cluster);
describeColumnFamilies(cluster);
}
private static void describeKeyspaces(Cluster cluster) {
// キースペースに関する情報を列挙
List<KeyspaceDefinition> keyspaces = cluster.describeKeyspaces();
for (KeyspaceDefinition keyspace : keyspaces) {
System.out.println("keyspace: " + keyspace.getName());
}
// sampleのキースペースが無ければ作る
if (cluster.describeKeyspace("sample") == null) {
// sampleキースペースに含めるColumn Familyを定義する
ColumnFamilyDefinition columnFamily = HFactory.createColumnFamilyDefinition(
"sample",
"Test",
ComparatorType.UTF8TYPE
);
columnFamily.setColumnType(ColumnType.SUPER); // Super Column Typeを使う
KeyspaceDefinition keyspace = HFactory.createKeyspaceDefinition(
"sample", // keyspace name
org.apache.cassandra.locator.SimpleStrategy.class.getName(),
1, // replication factor
Arrays.asList(columnFamily) // column family definitions
);
cluster.addKeyspace(keyspace, true);
}
}
private static void describeColumnFamilies(Cluster cluster) {
StringSerializer serializer = StringSerializer.get();
KeyspaceDefinition keyspace = cluster.describeKeyspace("sample");
// キースペース内のColumn Familyを列挙する
List<ColumnFamilyDefinition> columns = keyspace.getCfDefs();
boolean existsSampleColumnFamily = false;
for (ColumnFamilyDefinition columnFamily : columns) {
String name = columnFamily.getName();
System.out.println(
String.format(
"column factory: %s -> %s",
name,
columnFamily.getColumnType()
)
);
System.out.println(columnFamily.getColumnMetadata());
if (name.equals("Sample")) {
existsSampleColumnFamily = true;
}
}
// sampleキースペース内にSampleのColumn Familyが無ければ作る
if (!existsSampleColumnFamily) {
BasicColumnDefinition column = new BasicColumnDefinition();
column.setName(serializer.toByteBuffer("id"));
column.setIndexName("id_idx");
column.setIndexType(ColumnIndexType.KEYS);
column.setValidationClass(ComparatorType.LONGTYPE.getClassName());
BasicColumnFamilyDefinition columnFamily = new BasicColumnFamilyDefinition();
columnFamily.setKeyspaceName("sample");
columnFamily.setName("Sample");
columnFamily.setComparatorType(ComparatorType.UTF8TYPE);
columnFamily.setDefaultValidationClass(ComparatorType.UTF8TYPE.getClassName());
columnFamily.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName());
columnFamily.addColumnDefinition(column);
cluster.addColumnFamily(columnFamily, true);
}
ColumnFamilyTemplate<String, String> template = new ThriftColumnFamilyTemplate<String, String>(
HFactory.createKeyspace("sample", cluster),
"Sample",
StringSerializer.get(),
StringSerializer.get()
);
// test1キーのデータを取得する
ColumnFamilyResult<String, String> res1 = template.queryColumns("test1");
if (res1.hasResults()) {
System.out.println(res1.getString("name"));
System.out.println(res1.getLong("id"));
ColumnFamilyUpdater<String, String> updater = template.createUpdater("test1");
updater.setString("name", "ほげ");
updater.setLong("id", System.currentTimeMillis());
// test1キーなデータを更新する
template.update(updater);
}
// 削除
// template.deleteRow("test1");
}
}
http://hector-client.github.com/hector/build/html/documentation.html