DataStax Academy FAQ

DataStax Academy migrated to a new learning management system (LMS) in July 2020. We are also moving to a new Cassandra Certification process so there are changes to exam bookings, voucher system and issuing of certificates.

Check out the Academy FAQ pages for answers to your questions:


question

josef.schauer_169242 avatar image
josef.schauer_169242 asked ·

How can I get my Groovy code to work in Studio?

Hello,

I have a question about using groovy code in dse-studio the right way.

schema setup

schema.propertyKey('vId').Uuid().single().ifNotExists().create()
schema.propertyKey('cId').Text().single().ifNotExists().create()
schema.propertyKey('aId').Uuid().single().ifNotExists().create()

schema.propertyKey('property1').Text().multiple().properties('cId','aId').ifNotExists().create()
schema.propertyKey('property2').Text().multiple().properties('cId','aId').ifNotExists().create()
schema.vertexLabel('Vertex').partitionKey('vId').properties('property1','property2').ifNotExists().create()
schema.vertexLabel('Vertex').index('search').search().by('property1').by('property2').ifNotExists().add()

data

g.addV('Vertex').property('vId', '10000000-0000-0000-0000-100000000001')
.property('property1', 'Text1', T.id, UUID.fromString('83755ac4-7d66-42cf-a824-1a6960b5772f'), 'aId', '83755ac4-7d66-42cf-a824-1a6960b5772f', 'cId', '["00000000-0000-0000-0000-100000000001"]')

g.addV('Vertex').property('vId', '10000000-0000-0000-0000-100000000002')
.property('property1', 'Text1', T.id, UUID.fromString('67755ac4-7d66-42cf-a824-1a6960b5772f'), 'aId', '67755ac4-7d66-42cf-a824-1a6960b5772f', 'cId', '["00000000-0000-0000-0000-100000000002"]')

problem

I want to loop over the vertices and add a property. But the property I want to add needs information from the current vertex in the loop. This is for migrating existing data, high performance is no requirement.

I prepared the following code. I tried to use groovy as much as possible, because for me as a java developer it seems to be the easiest way.

vertices = g.V().hasLabel('Vertex').has('property1', regex('.*')).iterator()

while(vertices.hasNext()) {
    vertex = vertices.next();
    UUID vId = UUID.fromString((String)vertex.properties('vId').next().value());
    UUID aId = UUID.randomUUID();
    g.V().hasLabel('Vertex').has('vId', vId).property('property2', 'Text2', T.id, aId, 'aId', aId, 'cId', '["'+vId+'"]');
}

As you can see, I filter for vertices and create the iterator. Then I iterate over the list and prepare some data which I need in the "property"-step.

I need a new UUID which is used twice in the property step. Also, the vertex id is used in the step. Then I filter again based on the current id and want to add the property.

The code runs without any error but doesn't do anything.

How can I get this runnable? I would prefer to use groovy, but if not possible a gremlin command would help, too.

Thanks in advance.

Josef Schauer

studiogroovy
1 comment
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

@josef.schauer_169242 Just acknowledging your ticket. It might have something to do with not having full Groovy support. I'm going to reach out to the DSE Graph team to get you an answer. Cheers!

0 Likes 0 · ·

1 Answer

polandll avatar image
polandll answered ·

A couple of things to say about this issue. First, add:

.iterate()

to the end of the first line in the add vertices codeblock, so that both vertices will be added.

That's the missing piece in the while loop, too. Add that iterate() to the add property statement in the while loop and it works just fine.

1 comment Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

works, thanks.

1 Like 1 · ·