Skip to content

Instantly share code, notes, and snippets.

@lonniev
Last active September 1, 2022 12:17
Show Gist options
  • Save lonniev/6c9c827fd1797cb7d0d3b32385c698c3 to your computer and use it in GitHub Desktop.
Save lonniev/6c9c827fd1797cb7d0d3b32385c698c3 to your computer and use it in GitHub Desktop.
Perform a Gremlin Query which makes an authenticated REST call during one Step
signInToken = { -> signInPost = new URL( "http://some.host.com:9000/signIn" ).openConnection(); message = '{ "username": "[email protected]", "password": "my-password", "rememberMe": false }'; signInPost.setDoOutput( true ); signInPost.setRequestProperty( "Content-Type", "application/json" ); signInPost.getOutputStream().write( message.getBytes( "UTF-8" ) ); signInPost.getResponseCode(); j = new groovy.json.JsonSlurper().parseText( signInPost.getInputStream().getText() ); j.resources.token }
userName = { u,t -> userGet = new URL( "http://some.host.com:9000/users/${u}" ).openConnection(); userGet.setRequestProperty( "Accept", "application/json" ); userGet.setRequestProperty( "X-Auth-Token", t ); userGet.getResponseCode(); j = new groovy.json.JsonSlurper().parseText( userGet.getInputStream().getText() ); j.resources.name }
g.V().limit(1).map { it -> userName( it.get().value( 'modifiedBy' ), signInToken() ) }.find()
@lonniev
Copy link
Author

lonniev commented Aug 30, 2022

It is always important to remember that Lambda steps are significantly slower than Gremlin-internal steps. Make sure to only "call out" for additional details with small datasets only when necessary.

gremlin> g.V().limit(1).map { it -> userName( it.get().value( 'modifiedBy' ), signInToken() ) }.profile()
==>Traversal Metrics
Step                                                               Count  Traversers       Time (ms)    % Dur
=============================================================================================================
JanusGraphStep(vertex,[])                                              1           1           4.674     3.92
    \_condition=()
    \_isFitted=false
    \_query=[]
    \_limit=1
    \_orders=[]
    \_isOrdered=true
  optimization                                                                                 0.009
  optimization                                                                                 0.003
  scan                                                                                         0.000
    \_condition=VERTEX
    \_query=[]
    \_fullscan=true
LambdaMapStep(lambda)                                                  1           1         114.579    96.08
                                            >TOTAL                     -           -         119.253        -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment