Skip to content

Instantly share code, notes, and snippets.

@giladmanor
Created December 18, 2013 11:53
Show Gist options
  • Save giladmanor/8021110 to your computer and use it in GitHub Desktop.
Save giladmanor/8021110 to your computer and use it in GitHub Desktop.
Neo4j <=> RubyOnRails ::: example code for migrating functionality to java || how to pass the live embedded database handle down to java || as used in wikibrains || this example assumes the use of neo4j.rb
#this example uses the javable functionality you may find @ https://gist.github.com/giladmanor/7972127
require 'javable'
#example controller using a service from java to access the neo4j database
class JavaServiceController < ActionController::Base
include Javable
require_jar_folder
def get_some_graph_data
j_service = javable("com.wikibrains.core.SomeService")
#note the passing of the embedded database instance from rhe ruby layer over to java
# namely: Neo4j.started_db translates in java to: GraphDatabaseService
render :json => j_service.loveMeSomeData(Neo4j.started_db,params[:ids]).to_a.map{|i| [i.key, i.value]}
end
end
// Java stateless service that extracts and orders a list of nodes
public class SomeService {
// this would be an example implementation of a service that retrieves a given list of nodes by one property (i.e. name)
// and ordering the list by another property (i.e. value) and returns it with the highest value first
public static List loveMeSomeData(GraphDatabaseService service, String[] nodeNames) {
//grabbing the nodes by the list of names
HashMap<String, Integer> list = new HashMap<>();
for (String name : nodeNames) {
// not that is requires that you have an index called "Clue_exact".
//and you dont, so then either use node id's or use what you have instead
clue = service.index().forNodes("Clue_exact").get("name", name).getSingle();
list.put(clue.getProperty("name").toString(), Integer.valueOf(clue.getProperty("value")));
}
//Sorting by value (No Brainer)
ArrayList<Map.Entry<String, Integer>> l = new ArrayList(list.entrySet());
Collections.sort(l, new Comparator<Map.Entry<?, Integer>>() {
@Override
public int compare(Map.Entry<?, Integer> o1, Map.Entry<?, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
return l;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment