Add real NoSQL sources
[jabaws.git] / webservices / compbio / nosql / cassandra / CassandraCreate.java
diff --git a/webservices/compbio/nosql/cassandra/CassandraCreate.java b/webservices/compbio/nosql/cassandra/CassandraCreate.java
new file mode 100644 (file)
index 0000000..376da4c
--- /dev/null
@@ -0,0 +1,103 @@
+package combio.cassandra;
+
+import java.util.Arrays;
+
+import me.prettyprint.cassandra.serializers.LongSerializer;
+import me.prettyprint.cassandra.serializers.StringSerializer;
+import me.prettyprint.cassandra.service.ThriftKsDef;
+import me.prettyprint.hector.api.Cluster;
+import me.prettyprint.hector.api.Keyspace;
+import me.prettyprint.hector.api.beans.ColumnSlice;
+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.mutation.Mutator;
+import me.prettyprint.hector.api.query.QueryResult;
+import me.prettyprint.hector.api.query.SliceQuery;
+
+public class CassandraCreate {
+       private static Keyspace ksp;
+       private static Cluster cluster;
+       private static Mutator<Long> mutatorLong;
+       private static Mutator<String> mutatorString;
+       private static Mutator<String> mutatorLog;
+       StringSerializer ss = StringSerializer.get();   
+       LongSerializer ls = LongSerializer.get();       
+       
+       
+       // connect to cluster 
+       public void Connection() {
+               cluster = HFactory.getOrCreateCluster("Protein Cluster", "127.0.0.1:9160");
+               KeyspaceDefinition keyspaceDef = cluster.describeKeyspace("ProteinKeyspace");
+               // If keyspace does not exist, the CFs don't exist either. => create them.
+/*             if (keyspaceDef != null) {
+                       cluster.dropColumnFamily("ProteinKeyspace", "ProteinRow", true);
+                       cluster.dropColumnFamily("ProteinKeyspace", "ProteinData", true);
+                       cluster.dropKeyspace("ProteinKeyspace", true);
+                       System.out.println("ProteinKeyspace has been dropped");
+       } else*/ if (keyspaceDef == null) {     // create column family
+                       System.out.println("ProteinKeyspace has been null");
+                       ColumnFamilyDefinition cfProtein = HFactory.createColumnFamilyDefinition("ProteinKeyspace", "ProteinRow", ComparatorType.ASCIITYPE);
+                       ColumnFamilyDefinition cfLog = HFactory.createColumnFamilyDefinition("ProteinKeyspace", "ProteinLog", ComparatorType.ASCIITYPE);
+                       ColumnFamilyDefinition cfData = HFactory.createColumnFamilyDefinition("ProteinKeyspace", "ProteinData", ComparatorType.ASCIITYPE);
+
+                       KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition("ProteinKeyspace",                 
+                                       ThriftKsDef.DEF_STRATEGY_CLASS, 1, Arrays.asList(cfProtein, cfLog, cfData));
+                       //Add the schema to the cluster.
+                       //"true" as the second param means that Hector will block until all nodes see the change.
+                       cluster.addKeyspace(newKeyspace, true);
+                       cluster.addColumnFamily(cfProtein, true);
+                       cluster.addColumnFamily(cfLog, true);
+                       cluster.addColumnFamily(cfData, true);
+               }
+               ksp = HFactory.createKeyspace("ProteinKeyspace", cluster);
+               System.out.println("Cassantra has been connected");             
+       }
+       
+       // parsing data from http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat
+       public void Parsing() {
+               mutatorString = HFactory.createMutator(ksp, ss); // CF ProteinRow store protein and prediction
+               mutatorLog = HFactory.createMutator(ksp, ss); // CF ProteinLog store log informations (ip, id, dates start and dates of end)
+               mutatorLong = HFactory.createMutator(ksp, ls); // CF ProteinData store id and protein per data
+               System.out.println("Parsing......");
+               String in = "http://www.compbio.dundee.ac.uk/www-jpred/results/usage-new/alljobs.dat";
+               DataParsing datParsing = new DataParsing();
+               datParsing.ParsingTest(in);
+               mutatorString.execute();
+               mutatorLong.execute();
+               mutatorLog.execute();
+               System.out.println("Data Inserted");
+       }
+       
+       public void Closing() {
+               cluster.getConnectionManager().shutdown();  
+               System.out.println("Cassantra has been closed");
+       }
+       
+       // check whether this id exists in the cassandra DB
+       public boolean CheckIP(String ip) {
+               SliceQuery<String, String, String> sliceQuery = HFactory.createSliceQuery(ksp, ss, ss, ss);
+               sliceQuery.setColumnFamily("ProteinLog").setKey(ip).setRange("", "", false, 100);
+               QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();
+               if (result.get().getColumns().size() > 0)
+                       return true;
+               else
+                       return false;
+       }       
+       
+       public void InsertData(long dataWork, String dataBegin, String dataEnd, String ip, String id, String statusEx, String statusFinal, String protein, String jnetpred) {
+               mutatorLog.addInsertion(id, "ProteinLog", HFactory.createColumn("ip", ip, ss, ss))
+               .addInsertion(id, "ProteinLog", HFactory.createColumn("DataBegin", dataBegin, ss, ss))
+               .addInsertion(id, "ProteinLog", HFactory.createColumn("DataEnd", dataEnd, ss, ss))
+               .addInsertion(id, "ProteinLog", HFactory.createColumn("Status ex", statusEx, ss, ss))
+               .addInsertion(id, "ProteinLog", HFactory.createColumn("Status final", statusFinal, ss, ss))
+               .addInsertion(id, "ProteinLog", HFactory.createColumn("Protein", protein, ss, ss));                     
+               mutatorString.addInsertion(protein, "ProteinRow", HFactory.createColumn(id, jnetpred, ss, ss));
+               mutatorLong.addInsertion(dataWork, "ProteinData", HFactory.createColumn(id, protein, ss, ss));          
+         }
+       
+       public Keyspace GetKeyspace() {
+               return ksp;
+       }
+}