项目作者: maxdemarzi

项目描述 :
A thin Ruby wrapper to the Neo4j Rest API
高级语言: Ruby
项目地址: git://github.com/maxdemarzi/neography.git
创建时间: 2010-11-12T22:50:45Z
项目社区:https://github.com/maxdemarzi/neography

开源协议:MIT License

下载


Neography

  • Gem Version
  • Build Status
  • Code Climate
  • Coverage Status

Welcome to Neography

Neography is a thin Ruby wrapper to the Neo4j Rest API, for more information:

If you want to utilize the full power of Neo4j, you will want to use JRuby and the excellent Neo4j.rb gem at https://github.com/andreasronge/neo4j by Andreas Ronge

Installation

Gemfile

Add neography to your Gemfile:

  1. gem 'neography'

And run Bundler:

  1. $ bundle

Manually:

Or install neography manually:

  1. $ gem install 'neography'

And require the gem in your Ruby code:

  1. require 'rubygems'
  2. require 'neography'

Read the wiki for information about dependencies.

Rake tasks are available for downloading, installing and running Neo4j.

Usage

Configuration and initialization

Configure Neography as follows:

  1. # these are the default values:
  2. Neography.configure do |config|
  3. config.protocol = "http"
  4. config.server = "localhost"
  5. config.port = 7474
  6. config.directory = "" # prefix this path with '/'
  7. config.cypher_path = "/cypher"
  8. config.gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script"
  9. config.log_file = "neography.log"
  10. config.log_enabled = false
  11. config.slow_log_threshold = 0 # time in ms for query logging
  12. config.max_threads = 20
  13. config.authentication = nil # 'basic' or 'digest'
  14. config.username = nil
  15. config.password = nil
  16. config.parser = MultiJsonParser
  17. config.http_send_timeout = 1200
  18. config.http_receive_timeout = 1200
  19. config.persistent = true
  20. end

Then initialize a Rest instance:

  1. @neo = Neography::Rest.new
  2. @neo = Neography::Rest.new({:authentication => 'basic', :username => "neo4j", :password => "swordfish"})
  3. @neo = Neography::Rest.new("http://neo4j:swordfish@localhost:7474")

For overriding these default and other initialization methods, see the
configuration and initialization page in the Wiki.

REST API

Neography supports the creation and retrieval of nodes and relationships through the Neo4j REST interface.
It supports indexes, Gremlin scripts, Cypher queries and batch operations.

Some of this functionality is shown here, but all of it is explained in the following Wiki pages:

2.0 Only features:

1.8+ features:

Some example usage:

  1. # Node creation:
  2. node1 = @neo.create_node("age" => 31, "name" => "Max")
  3. node2 = @neo.create_node("age" => 33, "name" => "Roel")
  4. # Node properties:
  5. @neo.set_node_properties(node1, {"weight" => 200})
  6. # Relationships between nodes:
  7. @neo.create_relationship("coding_buddies", node1, node2)
  8. # Get node relationships:
  9. @neo.get_node_relationships(node2, "in", "coding_buddies")
  10. # Use indexes:
  11. @neo.add_node_to_index("people", "name", "max", node1)
  12. @neo.get_node_index("people", "name", "max")
  13. # Batches:
  14. @neo.batch [:create_node, {"name" => "Max"}],
  15. [:create_node, {"name" => "Marc"}]
  16. # Cypher queries:
  17. @neo.execute_query("start n=node(0) return n")

You can also use the cypher gem instead of writing cypher as text.

  1. node(1).outgoing(rel(:friends).where{|r| r[:since] == 1994})

would become:

  1. START me=node(1)
  2. MATCH (me)-[friend_rel:`friends`]->(friends)
  3. WHERE (friend_rel.since = 1994)
  4. RETURN friends

This is just a small sample of the full API, see the Wiki documentation for the full API.

Neography raises REST API errors as Ruby errors, see the wiki page about errors.
(Note: older versions of Neography did not raise any errors!)

Phase 2

Trying to mimic the Neo4j.rb API.

Now we are returning full objects. The properties of the node or relationship can be accessed directly (node.name).
The Neo4j ID is available by using node.neo_id.

Some of this functionality is shown here, but all of it is explained in the following Wiki pages:

  1. # create two nodes:
  2. n1 = Neography::Node.create("age" => 31, "name" => "Max")
  3. n2 = Neography::Node.create("age" => 33, "name" => "Roel")
  4. n1.exist? # => true
  5. # get and change some properties:
  6. n1[:age] # => 31
  7. n1.name # => "Max"
  8. n1[:age] = 32 # change property
  9. n1.weight = 190 # new property
  10. n1.age = nil # remove property
  11. # add a relationship between nodes:
  12. new_rel = Neography::Relationship.create(:coding_buddies, n1, n2)
  13. # remove a relationship:
  14. new_rel.del
  15. # add a relationship on nodes:
  16. n1.outgoing(:coding_buddies) << n2
  17. # more advanced relationship traversal:
  18. n1.outgoing(:friends) # Get nodes related by outgoing friends relationship
  19. n1.outgoing(:friends).depth(2).include_start_node # Get n1 and nodes related by friends and friends of friends
  20. n1.rel?(:outgoing, :friends) # Has outgoing friends relationship
  21. n1.rels(:friends,:work).outgoing # Get outgoing friends and work relationships
  22. n1.all_paths_to(n2).incoming(:friends).depth(4) # Gets all paths of a specified type
  23. n1.shortest_path_to(n2).incoming(:friends).depth(4).nodes # Gets just nodes in path

This is just a small sample of the full API, see the Wiki documentation for the full API.

More

Examples

Some example code.

Testing

Some tips about testing.

Complement to Neography are the:

An alternative to Neography is Architect4r by Maximilian Schulz

Neography in the Wild

Getting started with Neography

Contributing

Please create a new issue if you run into any bugs.

Contribute patches via pull requests.

Help

If you are just starting out, or need help send me an e-mail at maxdemarzi@gmail.com.

Check you my blog at http://maxdemarzi.com where I have more Neography examples.

Licenses