Querys using Cypher and python neo4jrestclient

566 views
Skip to first unread message

MPM

unread,
Dec 17, 2011, 5:12:34 PM12/17/11
to Neo4j
A Short question. Is possible to do cypher querys at Neo4j Server
Database using the python neo4jrestclient library?

Thanks!

Peter Neubauer

unread,
Dec 18, 2011, 11:18:19 AM12/18/11
to ne...@googlegroups.com
Marc,
according to the README, you should be able to post to Neo4j Server
extensions, see https://github.com/versae/neo4j-rest-client
#extensions, which will give you the cypher endpoint, like
http://docs.neo4j.org/chunked/snapshot/cypher-plugin.html . The same
code should work against the now built-in endpoint at
http://docs.neo4j.org/chunked/snapshot/rest-api-cypher.html if you are
running the latest SNAPSHOT.

Cheers,

/peter neubauer

TC CEO of the year - vote for Emil Eifrém!
http://crunchies2011.techcrunch.com/nominate/

Google:neubauer.peter
Skype:peter.neubauer
Phone: +46 704 106975
LinkedIn   http://www.linkedin.com/in/neubauer
Twitter      @peterneubauer

brew install neo4j && neo4j start
heroku addons:add neo4j

Matt Luongo

unread,
Dec 19, 2011, 12:30:44 PM12/19/11
to Neo4j
As Peter mentioned, you definitely can, but unlike the Gremlin
extension the client doesn't give the returned data any special
significance.
For example, if one column is a node per row, the client won't return
a client.Node, it will just return the dict from the JSON.
To help deal with some of this, I wrote a few quick lazy classes...
class LazyBase(object):    """    A mixin to make elements of the REST
client lazy.    """    def __init__(self, url, dic):       
self._dont_update = True        super(LazyBase, self).__init__(url,
create=False)        self._dic = dic.copy()        self._dont_update =
False
    def update(self, *args, **kwargs):        if not
self._dont_update:            super(LazyBase, self).update(*args,
**kwargs)
    @classmethod    def from_dict(cls, dic):        return
cls(dic['self'], dic)
class LazyNode(LazyBase, neo4j.Node):    id_url_template = 'node/%d'
class LazyRelationship(LazyBase, neo4j.Relationship):   
id_url_template = 'relationship/%d'
... and then whenever I get a dict returned from cypher, say for a
node, I just use LazyNode.from_dict(d).This has the benefit of being
built straight from the results, and doesn't fire off another HTTP
request like
client.Node would.
Let me know what you think,
--
Matt Luongo
Co-Founder, Scholr.ly

On Dec 18, 11:18 am, Peter Neubauer <peter.neuba...@neotechnology.com>
wrote:


> Marc,
> according to the README, you should be able to post to Neo4j Server

> extensions, seehttps://github.com/versae/neo4j-rest-client
> #extensions, which will give you the cypher endpoint, likehttp://docs.neo4j.org/chunked/snapshot/cypher-plugin.html. The same
> code should work against the now built-in endpoint athttp://docs.neo4j.org/chunked/snapshot/rest-api-cypher.htmlif you are


> running the latest SNAPSHOT.
>
> Cheers,
>
> /peter neubauer
>

> TC CEO of the year - vote for Emil Eifrém!http://crunchies2011.techcrunch.com/nominate/

Peter Higdon

unread,
Apr 30, 2012, 10:51:28 AM4/30/12
to ne...@googlegroups.com
Hi Matt,

Can you please post these code snippets in the documentation (github or RtD)?
google groups stomped all over the formatting.

Thanks!
PeterH

James Thornton

unread,
Apr 30, 2012, 11:13:41 AM4/30/12
to ne...@googlegroups.com


On Monday, April 30, 2012 9:51:28 AM UTC-5, Peter wrote:

Can you please post these code snippets in the documentation (github or RtD)?
google groups stomped all over the formatting.

Here's how you do Cypher in Bulbs (http://bulbflow.com/)...

>>> from bulbs.neo4jserver import Graph
>>> g = Graph()

# Use the query() method to return initialized nodes
>>> query = "START n=node({id}) RETURN n"
>>> params = dict(id=3)
>>> nodes = g.cypher.query(script, params) 
>>> node = nodes.next()

# Use the table method to return columns and data
>>> query = "START x = node({id}) MATCH x -[r]-> n RETURN type(r), n.name?, n.age?"
>>> params = dict(id=3)
>>> columns, data = g.cypher.table(script, params) 

- James

Javier de la Rosa

unread,
Apr 30, 2012, 11:25:50 AM4/30/12
to ne...@googlegroups.com
On Sat, Dec 17, 2011 at 17:12, MPM <marc...@gmail.com> wrote:
> A Short question. Is possible to do cypher querys at Neo4j Server
> Database using the python neo4jrestclient library?

Yes, you can.

>>> from neo4jrestclient import client, constants

>>> gdb = client.GraphDatabase("http://localhost:7474/db/data")

>>> query = """START n=node(*) RETURN n"""

>>> cypher = gdb.extensions.CypherPlugin.execute_query

>>> nodes = cypher(query=query, returns=constants.NODE)

Using the the "returns" parameter you can /cast/ the results to nodes,
relationships, or paths without doing another request. Or you can also
use constants.RAW for the data as the server produces.

In the next version of the lib, Cypher will be integrated in the core
and not as a plugin anymore.

Best regards.



--
Javier de la Rosa
http://versae.es

Matt Luongo

unread,
Apr 30, 2012, 4:36:04 PM4/30/12
to ne...@googlegroups.com
Peter H,

I posted a gist with the lazy primitives, though I'm not sure this approach is necessary with the newest version of neo4jrestclient. Javier can tell you more about that, though.

HTH,

Matt

Matt Luongo

unread,
Apr 30, 2012, 4:39:01 PM4/30/12
to ne...@googlegroups.com
Oh, and if anyone is interested in some of the cooler tabular stuff with Cypher/Gremlin, make sure you pay attention to Javier's mention of the "raw" option- it lets you break out of the client's primitives if you need to, which is handy.

- Matt
Reply all
Reply to author
Forward
0 new messages