Which Ruby REST API client for neo4j?
The short answer is that there is no any mature ActiveModel-like gems for RESTful neo4j.
The most common scenario is to just use Neography.
Neo4j with Ruby On Rails
JRuby allows Ruby to access Neo4j directly through the Java API. It is the fastest implementation possible. However, Neo4j also provides a REST API that allows any language to access it over HTTP.
I seems like your answer may be here: Which Ruby REST API client for neo4j?
How to save neo4j database?
Create a @neo client:
@neo = Neography::Rest.new
Create a queue:
@queue = []
Make use of the BATCH api for data loading.
def create_person(name, id)
@queue << [:create_node, {"name" => name, "id" => id}]
if @queue.size >= 500
batch_results = neo.batch *@queue
@queue = []
batch_results.each do |result|
id = result["body"]["self"].split('/').last
$persons[id] = result
end
end
end
Run through you csv file:
CSV.foreach('user.csv', :headers => true) do |row|
create_person(row[1], row[0].to_i)
end
Get the leftovers:
batch_results = @neo.batch *@queue
batch_results.each do |result|
id = result["body"]["self"].split('/').last
$persons[id] = result
end
An example of data loading via the rest api can be seen here => https://github.com/maxdemarzi/neo_crunch/blob/master/neo_crunch.rb
An example of using a queue for writes can be seen here => http://maxdemarzi.com/2013/09/05/scaling-writes/
How to setup neo4j with dBpedia ontop of ruby-on-rails application?
The simplest way to load dbpedia into Neo4j is to use the dbpedia4neo library. This is a Java library, but you don't need to know any Java because all you need to do is run the executable.
You could rewrite this in JRuby if you want, but regular Ruby won't work because it relies on Blueprints, a Java library with no Ruby equivalent.
Here are the two key files, which provide the loading procedure.
- https://github.com/oleiade/dbpedia4neo/blob/master/src/main/java/org/acaro/dbpedia4neo/inserter/DBpediaLoader.java
- https://github.com/oleiade/dbpedia4neo/blob/master/src/main/java/org/acaro/dbpedia4neo/inserter/TripleHandler.java
Here is a description of what's involved.
Blueprints is translating the RDF data to a graph representation. To understand what's going on under the hood, see Blueprints Sail Ouplementation:
After you download the dbpedia dump files, you should be able to build the dbpedia4neo Java library and run it without modifying the Java code.
First, clone the oleiade's fork of the GitHub repository and change to the dbpedia4neo
directory:
$ git clone https://github.com/oleiade/dbpedia4neo.git
$ cd dbpedia4neo
(Oleiade's fork includes a minor Blueprints update that does sail.initialize();
See https://groups.google.com/d/msg/gremlin-users/lfpNcOwZ49Y/WI91ae-UzKQJ).
Before you build it, you will need to update the pom.xml
to use more current Blueprints versions and the current Blueprints repository (Sonatype).
To do this, open pom.xml
and at the top of the dependencies
section, change all of the TinkerPop Blueprints versions from 0.6
to 0.9
.
While you are in the file, add the Sonatype repository to the repositories
section at the end of the file:
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/releases</url>
</repository>
Save the file and then build it using maven:
$ mvn clean install
This will download and install all the dependencies for you and create a jar file in the target
directory.
To load dbpedia, use maven to run the executable:
$ mvn exec:java \
-Dexec.mainClass=org.acaro.dbpedia4neo.inserter.DBpediaLoader \
-Dexec.args="/path/to/dbpedia-dump.nt"
The dbpedia dump is large so this will take a while to load.
Now that the data is loaded, you can access the graph in one of two ways:
- Use JRuby and the Blueprints-Neo4j API directly.
- Use regular Ruby and the Rexster REST server, which is similar to Neo4j Server except that it supports multiple graph databases.
For an example of how to create a Rexster client, see Bulbs, a Python framework I wrote that supports both Neo4j Server and Rexster.
- http://bulbflow.com/
- https://github.com/espeed/bulbs
- https://github.com/espeed/bulbs/tree/master/bulbs/rexster
Another approach to all this would be to process the dbpedia RDF dump file in Ruby, write out the nodes and relationships to a CSV file, and use the Neo4j batch importer to load it. But this will require that you manually translate the RDF data into Neo4j relationships.
Retrive DateTime from Neo4j using Lumen and NeoEloquent OGM
I also encountered the issue and have not been able to find a way around this in the way that you want. It is in the neoeloquent library itself as far as I can figure out. However I have come up with an alternative which is not ideal since it would be better to be able to do it with the library but it works.
I have written a RawQueryService
which can take a raw Neo4j
query and retrieve the output as an associative array.
The RawQueryService
is as follows:
<?php
namespace App\Services;
use Illuminate\Support\Facades\DB;
class RawQueryService
{
public static function executeRawQuery(string $query, $noProcessing=false) {
$result = DB::select($query);
$resultArray = array();
foreach($result as $row){
$result = $row['n'];
array_push($resultArray, $result);
}
return $resultArray;
}
}
And then to retrieve the start time:
$start = \App\Services\RawQueryService::executeRawQuery(
"match(n) where n.title=\"".$title."\" return toString(n.start)");
It converts the datetime internally to a string so the Neo4j library does not receive an IllegalArgumentException
.
I hope this helps you.
Related Topics
Breaking a Large Rails App into Smaller Apps
Ruby Metaprogramming, How Does Rspec's 'Should' Work
How to Send Message to All Client Except Sender in Rails/Actioncable
How to Refresh a Page with Turbolinks
Rails 5, "Nil Is Not a Valid Asset Source"
Solid Tutorial for Building a Simple Wiki Application in Ruby on Rails
Initialize Two Variables on Same Line
Instance Variables Inheritance
Single Custom Param Name in Routes for Nested Resources Rails 4.1
How to Bundle Install Gemfile with Specific Version of Bundler
Rails Console Fails with 'Switch to Inspect Mode' in Windows
Validate That a Value Is in a Certain Range, E.G. 1 <= Val <=2
Heroku Rails Rake Task to Sync Production & Local Db