Get value map od node

778 views
Skip to first unread message

Tomasz

unread,
Oct 17, 2017, 9:27:23 AM10/17/17
to Gremlin-users
Enter code here...

Let assume that I use gremlin-python and I have a node - "Person".
I want to get all properties of the node I can do it in at least two ways:

1) dictionary ={'name': g.V(vertex).values('name').next(),
                     
'uid': g.V(vertex).values('uid').next(),
                     
'email': g.V(vertex).values('email').next()}



2) dictionary = {g.V(vertex).valueMap().next()}



I prefer the second option because it is shorter and "always up to date". The first option is error-prone, for example, I added 3 persons with properties: 'name', 'uid', 'email'
, then I added surname property and add another 2 persons. To my query (1) I add next value (surname). When I do the query I get an error because I ask about surname which doesn't exist in the nodes which represent the first three persons.

The second options give me the result of certain properties in the list. Is there exist an elegant way to get values dynamically but not in the list?
 

Stephen Mallette

unread,
Oct 17, 2017, 10:56:47 AM10/17/17
to Gremlin-users
The first approach isn't so good because you make three requests for the same vertex. You would definitely prefer your latter example. Of course, I think you would prefer:

g.V(vertex).valueMap("name", "uid", "email")

as opposed to not specifying the property keys. I tend to make a comparison to SQL here, where doing valueMap() without properties is a lot like SELECT * FROM table. You don't usually do that. You usually specify only the fields that you want to get. So, in SQL you would instead do: SELECT name, uid, email FROM table. If "surname" became a field you were interested in, then you would just add it to the SQL statement when that happened. The same thing applies here for Gremlin.

One other point of note with valueMap() - note that with valueMap() you don't quite get a Map<String,Object>:

gremlin> g.V().valueMap().next()
==>name=[marko]
==>age=[29]

You get a Map<String,List<Object>> because of multi-properties. If you don't use multi-properties that's a bit inconvenient because you have to unwrap the list to pop-off one item for all your properties. You can remove that hierarchy with project():

gremlin> g.V().project('name','age').by('name').by('age').next()
==>name=marko
==>age=29

It's a bit more verbose but now you get just key/value pairs without the list. 




--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/15d7a971-bb79-4eb4-b315-438453f8a38b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Kuppitz

unread,
Oct 17, 2017, 11:54:09 AM10/17/17
to gremli...@googlegroups.com
I didn't quite get the question, but I guess "get values dynamically but not in the list" is referring to "how to get default values for properties that do not exist". So just in case I'm right with this assumption:

gremlin> g.V(1).project("firstname","lastname","age").
           by(values("firstname", "name")).
           by(coalesce(values("lastname"), constant("N/A"))).
           by("age")
==>[firstname:marko,lastname:N/A,age:29]

firstname falls back to name, lastname falls back to a constant value of "N/A".

Cheers,
Daniel


Tomasz

unread,
Oct 17, 2017, 3:03:31 PM10/17/17
to Gremlin-users
Stephen, you are right about "selecting" only values which I really need but I don't understand why Gremlin prefers multi-properties. Do you know why does happen? For me it is quite obvious that if I have name property there will be only one name, if I have email there will be one email etc. It is standard in SQL database that fields are atomic.
I rewrite application which uses relations database with ORM and I find queries in Gremlin quite wired. I don't know if is true or I don't understand it (gremlin).

Tomek

Stephen Mallette

unread,
Oct 17, 2017, 3:20:21 PM10/17/17
to Gremlin-users
 For me it is quite obvious that if I have name property there will be only one name, if I have email there will be one email etc.

It's not really quite obvious actually - it's a modelling choice. Maybe you would define "name" as being only one name, but someone else might use multi-properties to for a name field. How about this use case:

g.addV('person').property(list,'name', 'robert', 'date', '1/1/1977').property(list,'name','bob','date','1/1/2017')

keeping track of someone's name changes over time. Anyway, I guess my point is that while you might not use multi-properties, it is a feature of TinkerPop that people tend to make use of in graphs that support it. You just have to account for it in your traversals.

> I find queries in Gremlin quite wired

I'm not sure what you mean by that unless you meant to type "weird" instead of "wired" - hehe. There are a number of OGM (object graph mapper) tools in the TinkerPop community if you find working with those kinds of tools easier for building applications. Some folks really like that approach. Perhaps you should consider looking at those. Of course, ultimately, I don't think you can completely escape having to write Gremlin (anymore that you can completely escape writing SQL when using an ORM with relational databases). Eventually, you will hit a scenario of sufficient complexity that will simply require it. 



--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.

Tomasz

unread,
Oct 17, 2017, 4:46:38 PM10/17/17
to Gremlin-users


On Tuesday, October 17, 2017 at 9:20:21 PM UTC+2, Stephen Mallette wrote:wrote:Most of the examples from documentation and
 For me it is quite obvious that if I have name property there will be only one name, if I have email there will be one email etc.

It's not really quite obvious actually - it's a modelling choice. Maybe you would define "name" as being only one name, but someone else might use multi-properties to for a name field. How about this use case:

g.addV('person').property(list,'name', 'robert', 'date', '1/1/1977').property(list,'name','bob','date','1/1/2017')

keeping track of someone's name changes over time. Anyway, I guess my point is that while you might not use multi-properties, it is a feature of TinkerPop that people tend to make use of in graphs that support it. You just have to account for it in your traversals.

 
Most of the examples from documentation and Kelvin R. Lawrence's book uses "one property' not 'muli-properties' model.
Keeping track about changes of nodes a great feature. I need it but on this stage, I don't even think about it. Is this common approach to do it as you suggested above?


> I find queries in Gremlin quite wired

I'm not sure what you mean by that unless you meant to type "weird" instead of "wired" - hehe.
Of course, I have in mind weird ;).

There are a number of OGM (object graph mapper) tools in the TinkerPop community if you find working with those kinds of tools easier for building applications. Some folks really like that approach. Perhaps you should consider looking at those. Of course, ultimately, I don't think you can completely escape having to write Gremlin (anymore that you can completely escape writing SQL when using an ORM with relational databases). Eventually, you will hit a scenario of sufficient complexity that will simply require it. 

I definitely want to use Gremlin. Allways when I use new technology I want to know how it works. Using OGM could be easier then writing pure Gremlin but after all, I don't know how it really works.
 



On Tue, Oct 17, 2017 at 3:03 PM, Tomasz <troj...@gmail.com> wrote:
Stephen, you are right about "selecting" only values which I really need but I don't understand why Gremlin prefers multi-properties. Do you know why does happen? For me it is quite obvious that if I have name property there will be only one name, if I have email there will be one email etc. It is standard in SQL database that fields are atomic.
I rewrite application which uses relations database with ORM and I find queries in Gremlin quite wired. I don't know if is true or I don't understand it (gremlin).

Tomek

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Stephen Mallette

unread,
Oct 17, 2017, 4:59:16 PM10/17/17
to Gremlin-users
I'd call "multi-properties" and "meta-properties" an advanced feature. I wouldn't worry about either of them too much as you are just starting out....just be aware that they are an available feature. Using single cardinality for all your properties is fairly normal and most examples that are in TinkerPop and in other documentation sources will demonstrate Gremlin that way.

I'm not sure where you are having the most trouble with Gremlin but hopefully Kelvin's book can help you make the connections your missing. I'd also not overlook this resource:




To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/0e17f620-6bc3-4bbb-8013-a4e918eb2ce4%40googlegroups.com.

Tomasz

unread,
Oct 17, 2017, 4:59:35 PM10/17/17
to Gremlin-users


g.addV('person').property(list,'name', 'robert', 'date', '1/1/1977').property(list,'name','bob','date','1/1/2017')
 
I tried to execute above command and I get:
 org.janusgraph.core.SchemaViolationException: Key is defined for SINGLE cardinality which conflicts with specified: list


Stephen Mallette

unread,
Oct 17, 2017, 5:05:56 PM10/17/17
to Gremlin-users
I ran my code on TinkerGraph which is schemaless. In JanusGraph you have a schema language where you define the cardinality of your properties. That message is just telling you the validation against the schema failed as it is expecting single cardinality for "name" and you tried to pass in list cardinality.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/d9cb55e1-12fb-4589-8035-8b7d21ba96c9%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages