1 package compbio.nosql.cassandra;
3 import java.util.Arrays;
5 import me.prettyprint.cassandra.serializers.LongSerializer;
6 import me.prettyprint.cassandra.serializers.StringSerializer;
7 import me.prettyprint.cassandra.service.ThriftKsDef;
8 import me.prettyprint.hector.api.Cluster;
9 import me.prettyprint.hector.api.Keyspace;
10 import me.prettyprint.hector.api.beans.ColumnSlice;
11 import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
12 import me.prettyprint.hector.api.ddl.ComparatorType;
13 import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
14 import me.prettyprint.hector.api.factory.HFactory;
15 import me.prettyprint.hector.api.mutation.Mutator;
16 import me.prettyprint.hector.api.query.QueryResult;
17 import me.prettyprint.hector.api.query.SliceQuery;
19 public class CassandraCreate {
20 private static Keyspace ksp;
21 private static Cluster cluster;
22 private static Mutator<Long> mutatorLong;
23 private static Mutator<String> mutatorString;
24 private static Mutator<String> mutatorLog;
25 StringSerializer ss = StringSerializer.get();
26 LongSerializer ls = LongSerializer.get();
30 public void Connection() {
31 cluster = HFactory.getOrCreateCluster("Protein Cluster", "127.0.0.1:9160");
32 KeyspaceDefinition keyspaceDef = cluster.describeKeyspace("ProteinKeyspace");
33 // If keyspace does not exist, the CFs don't exist either. => create them.
34 /* if (keyspaceDef != null) {
35 cluster.dropColumnFamily("ProteinKeyspace", "ProteinRow", true);
36 cluster.dropColumnFamily("ProteinKeyspace", "ProteinData", true);
37 cluster.dropKeyspace("ProteinKeyspace", true);
38 System.out.println("ProteinKeyspace has been dropped");
39 } else*/ if (keyspaceDef == null) { // create column family
40 System.out.println("ProteinKeyspace has been null");
41 ColumnFamilyDefinition cfProtein = HFactory.createColumnFamilyDefinition("ProteinKeyspace", "ProteinRow", ComparatorType.ASCIITYPE);
42 ColumnFamilyDefinition cfLog = HFactory.createColumnFamilyDefinition("ProteinKeyspace", "ProteinLog", ComparatorType.ASCIITYPE);
43 ColumnFamilyDefinition cfData = HFactory.createColumnFamilyDefinition("ProteinKeyspace", "ProteinData", ComparatorType.ASCIITYPE);
45 KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition("ProteinKeyspace",
46 ThriftKsDef.DEF_STRATEGY_CLASS, 1, Arrays.asList(cfProtein, cfLog, cfData));
47 //Add the schema to the cluster.
48 //"true" as the second param means that Hector will block until all nodes see the change.
49 cluster.addKeyspace(newKeyspace, true);
50 cluster.addColumnFamily(cfProtein, true);
51 cluster.addColumnFamily(cfLog, true);
52 cluster.addColumnFamily(cfData, true);
54 ksp = HFactory.createKeyspace("ProteinKeyspace", cluster);
55 System.out.println("Cassantra has been connected");
58 // parsing data from http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat
59 public void Parsing() {
60 mutatorString = HFactory.createMutator(ksp, ss); // CF ProteinRow store protein and prediction
61 mutatorLog = HFactory.createMutator(ksp, ss); // CF ProteinLog store log informations (ip, id, dates start and dates of end)
62 mutatorLong = HFactory.createMutator(ksp, ls); // CF ProteinData store id and protein per data
63 System.out.println("Parsing......");
64 String in = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat";
65 DataParsing datParsing = new DataParsing();
66 datParsing.ParsingTest(in);
67 mutatorString.execute();
68 mutatorLong.execute();
70 System.out.println("Data Inserted");
73 public void Closing() {
74 cluster.getConnectionManager().shutdown();
75 System.out.println("Cassantra has been closed");
78 // check whether this id exists in the cassandra DB
79 public boolean CheckIP(String ip) {
80 SliceQuery<String, String, String> sliceQuery = HFactory.createSliceQuery(ksp, ss, ss, ss);
81 sliceQuery.setColumnFamily("ProteinLog").setKey(ip).setRange("", "", false, 100);
82 QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();
83 if (result.get().getColumns().size() > 0)
89 public void InsertData(long dataWork, String dataBegin, String dataEnd, String ip, String id, String statusEx, String statusFinal, String protein, String jnetpred) {
90 mutatorLog.addInsertion(id, "ProteinLog", HFactory.createColumn("ip", ip, ss, ss))
91 .addInsertion(id, "ProteinLog", HFactory.createColumn("DataBegin", dataBegin, ss, ss))
92 .addInsertion(id, "ProteinLog", HFactory.createColumn("DataEnd", dataEnd, ss, ss))
93 .addInsertion(id, "ProteinLog", HFactory.createColumn("Status ex", statusEx, ss, ss))
94 .addInsertion(id, "ProteinLog", HFactory.createColumn("Status final", statusFinal, ss, ss))
95 .addInsertion(id, "ProteinLog", HFactory.createColumn("Protein", protein, ss, ss));
96 mutatorString.addInsertion(protein, "ProteinRow", HFactory.createColumn(id, jnetpred, ss, ss));
97 mutatorLong.addInsertion(dataWork, "ProteinData", HFactory.createColumn(id, protein, ss, ss));
100 public Keyspace GetKeyspace() {