1月23日、「ビッグデータ管理入門」の後半を、NIIで聞いてきた
その内容(=表題のことをする)をメモメモのつづき
今回は、Cassandraアクセス。
hectorを使ってアクセスしている。
今回は、いただいたソースコードを貼ってみる。
(説明は次回)
とりあえず、今日はここまで。
あしたから、説明する。
その内容(=表題のことをする)をメモメモのつづき
今回は、Cassandraアクセス。
hectorを使ってアクセスしている。
今回は、いただいたソースコードを貼ってみる。
(説明は次回)
import java.util.Iterator; import me.prettyprint.cassandra.serializers.IntegerSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.service.ColumnSliceIterator; import me.prettyprint.cassandra.service.KeyIterator; import me.prettyprint.cassandra.service.ThriftKsDef; 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.Keyspace; import me.prettyprint.hector.api.beans.HColumn; import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition; import me.prettyprint.hector.api.ddl.ComparatorType; import me.prettyprint.hector.api.ddl.KeyspaceDefinition; import me.prettyprint.hector.api.factory.HFactory; import me.prettyprint.hector.api.query.SliceQuery; /** * Hello world! * */ public class Sample { public static String CLUSTER_NAME = "Test Cluster"; public static String DB_SERVER = "127.0.0.1"; public static String KEYSPACE_NAME = "MyKeyspace"; public static String COLUMN_FAMILY_NAME = "AccessLog"; public static String LOG_FILE = "access.log"; private Cluster cluster; private KeyspaceDefinition keyspaceDef; private Keyspace keyspace; private ColumnFamilyTemplate<Integer, String> template; public static void main(String[] args) { Sample app = new Sample(); // app.dropKeyspace(); app.prepare(); app.insertSample(); app.findAllSample(); app.updateSample(); app.deleteSample(); app.findAllKeys(); } public Sample() { cluster = HFactory.getOrCreateCluster(CLUSTER_NAME, DB_SERVER + ":9160"); } private void prepare() { keyspaceDef = cluster.describeKeyspace(KEYSPACE_NAME); if (keyspaceDef == null) { KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition(KEYSPACE_NAME, ThriftKsDef.DEF_STRATEGY_CLASS, 1, null); cluster.addKeyspace(newKeyspace, true); ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(KEYSPACE_NAME, COLUMN_FAMILY_NAME, ComparatorType.BYTESTYPE); cluster.addColumnFamily(cfDef); } keyspace = HFactory.createKeyspace(KEYSPACE_NAME, cluster); template = new ThriftColumnFamilyTemplate<Integer, String>(keyspace, COLUMN_FAMILY_NAME, IntegerSerializer.get(), StringSerializer.get()); } private void dropKeyspace() { cluster.dropKeyspace(KEYSPACE_NAME); } private void insertSample() { System.out.println("----- insertSample -----"); for (int i = 0; i < 10; ++i) { ColumnFamilyUpdater<Integer, String> updater = template.createUpdater(i); updater.setString("c1", "Hello" + i); updater.setString("c2", "World" + i * 2); updater.setString("c3", "!!" + i * i); template.update(updater); System.out.println("Inserted : " + i); } } private void updateSample() { System.out.println("----- updateSample -----"); for (int i = 0; i < 5; ++i) { int id = (int) (Math.random() * 10); ColumnFamilyUpdater<Integer, String> updater = template.createUpdater(id); updater.setString("c3", "!!!" + id * id * id); template.update(updater); System.out.println("Updated : " + id); } } private void deleteSample() { System.out.println("----- deleteSample -----"); // ランダムに3つc3消す for (int i = 0; i < 3; ++i) { int id = (int) (Math.random() * 10); template.deleteColumn(id, "c3"); System.out.println("Deleted : " + id + ".c3"); } // ランダムに1つ消す int id = (int) (Math.random() * 10); template.deleteRow(id); System.out.println("Deleted : " + id); } private void findAllSample() { System.out.println("----- findAllSample1 -----"); for (int i = 0; i < 10; ++i) { int id = i; ColumnFamilyResult<Integer, String> res = template.queryColumns(id); String value = "" + res.getKey() + " : " + "c1=" + res.getString("c1") + " " + "c3=" + res.getString("c3"); System.out.println(value); } } private void findAllKeys() { System.out.println("----- findAllKeys -----"); @SuppressWarnings("deprecation") KeyIterator<Integer> keyIterator = new KeyIterator<Integer>(keyspace, COLUMN_FAMILY_NAME, IntegerSerializer.get()); Iterator<Integer> itr = keyIterator.iterator(); while (itr.hasNext()) { Integer key = itr.next(); System.out.println(key); } } } |
とりあえず、今日はここまで。
あしたから、説明する。