How to define custom step in Tinkerpop 3? - gremlin

Tinkerpop 2 used to support user-defined step through Gremlin.defineStep(..)
How can I achieve the same using Tinkerpop 3 ?
Is there any Gremlin API to create a custom step and combine a set of traversal steps ?
Any suggestion will be highly appreciated.
Thanks
Kaniska

Patterns for DSL development are not completely established for TinkerPop 3.x, so expect some changes in advice as time progresses. There is surrounding information on this on the gremlin-users mailing list.
If you are using Gremlin Server (or can incorporate Groovy into your project) I don't see a reason to not just do some groovy metaprogramming to get this to work. Using the standard Gremlin Server zip distribution edit the generate-modern.groovy file to include this line at the start:
GraphTraversal.metaClass.knows = { delegate.out('knows') }
then start the server with:
bin/gremlin-server.sh conf/gremlin-server-rest-modern.yaml
Then a simple curl:
$ curl "http://localhost:8182?gremlin=g.V(1).knows().values('name')"
{"requestId":"66c2c929-9cc2-491d-acb0-ae4d7eef6b00","status":{"message":"","code":200,"attributes":{}},"result":{"data":["vadas","josh"],"meta":{}}}
If you have a complex DSL you might not want all that logic trapped in a groovy script. Easy enough - just build a standard groovy project, construct a jar, include some form of static initializer to call your metaprogramming code and place it on Gremlin Server's path. then your init script just needs to call that static initializer and your DSL is loaded.

Related

How to fetch data of a a vertex or an edge from JanusGraph using its Java driver?

I'm connecting to a remote JanusGraph server. There is no problem with the connection but I can't understand the Java API.
I'm running a Java Spring HTTP server. Inside the HTTP response method, I'm creating a graph traversal source like
GraphTraversalSource g = traversal().withRemote("conf/remote-graph.properties");
It seems fine. But When I want to get data about a Vertex I can not with the below code.
g.V(28712).next().keys()
IT GIVES AN EMPTY RESPONSE! WHY?
If I do the same with gremlin shell, I see it. See the below picture
If I do g.V(28712).valueMap(true).unfold().toList(); in Java, I see some results.
I just want to fetch data for a vertex or edge with Java. How should I do that?
thanks
By default, when you connect to a Gremlin Server using one of the Gremlin clients and you ask for a vertex, what you get back is a reference vertex. A reference vertex just contains the ID and the label. To get some properties you need to include them using valueMap, elementMap, project etc. Alternatively you can configure the Gremlin Server to return all properties. The default is set this way to reduce the amount of data that gets sent back to a client.
Please see the documentation for further details.
https://tinkerpop.apache.org/docs/current/reference/#gremlin-applications
https://tinkerpop.apache.org/docs/current/reference/#_properties_of_elements
There is a link in that documentation to a post about why the decisions were made. For convenience, I am including that link here as well.
https://lists.apache.org/thread.html/e959e85d4f8b3d46d281f2742a6e574c7d27c54bfc52f802f7c04af3%40%3Cdev.tinkerpop.apache.org%3E

How to integrate gRPC with karate [duplicate]

We have a requirement where we need to send .avro file as an input request to our API's. Really stuck at this point. If any detail example provided would be more appreciated.
Just use Java interop: https://github.com/intuit/karate#calling-java
You need to write a helper (start with a static method) to convert JSON to Avro and vice versa. I know teams using this for gRPC. Read this thread for tips: https://github.com/intuit/karate/issues/412
Also there is even a "karate-grpc" project: https://github.com/pecker-io/karate-grpc
Also see:
https://twitter.com/KarateDSL/status/1128170638223364097
https://twitter.com/KarateDSL/status/1417023536082812935

Is there a way to get GraphML representation of an in-memory TinkerGraph without writing to file?

I have an in-memory TinkerGraph. I want to write a Spring Boot REST controller to expose a serialized (as GraphML) representation of this Tinkergraph. The serialization APIs (g.io) needs a String filepath to be passed to it. Currently I am having to write to a /tmp file and then read the file to get a String representation of the serialized GraphML.
Is there a way to directly get a String output of the serialized GraphML? Without having to write into a tmp file and read it back in?
g.io("graph.xml").write().iterate()
As of the current latest release 3.4.2, I'm afraid that there is no way to do it with the Gremlin language. The reason it only writes to a file and not to something like an Java OutputStream is that the io() step is meant to be programming language agnostic. Python and other languages off the JVM have no way to construct or specify such an object so writing to file makes it work across the board. I don't know if that will change in the future, unless we came up with a reasonable API that would work intuitively across programming languages.
Since you are using an in-memory TinkerGraph you could bypass Gremlin and got back to the very old way of doing things:
Graph graph = TinkerFactory.createModern();
try (OutputStream os = new FileOutputStream("tinkerpop-modern.xml")) {
graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
}
You would just replace the FileOutputStream with whatever kind of OutputStream you wanted to use. This approach uses the old Graph API which I think is just deprecated in newer versions, so the option should still be available to you. Note that if you are not on the JVM the only way to do return a String would be by submitting a Gremlin script to Gremlin Server.

MarkLogic I don't know how to get all the result

Hello I am trying to read a module with this code:
(: Entry point - must be a read-only query. :)
xdmp:invoke(
'/path/mydocument.xqy',
(xs:QName('var1'), 'test',
xs:QName('var2'), "response"))
I am new in MarkLogic, I am using groovy and the api to connect to it, but also I saw I can invoke the module with this and indeed I did but it returns me
your query returned an empty sequence
I want to know if I can query xs:QName('var1'), 'test', changing test with a wildcard or how can I get all the information from the file called /path/mydocument.xqy?
I tried to use this:
xdmp:document-get("/path/mydocument.xqy)
but it says the file is not found. Although, if I use invoke I can query it, but I don't know what are the values I have to pass. I was wondering if there is something like sql using %% or something to give me all the data.
To answer the first question: "I am trying to read a module "
IF the module is in the database, then you must query the Modules database in which the module resides.
If the module is in the filesystem then you cannot directly access its source as a document but you can by executing xdmp:filesystem-file()
Simplification:
With the Default configuration of the server and REST client, user placed modules are in the "Modules" database and user placed documents are in the "Documents" database. This means, if you do a GET (read a "Document") with no additional parameters, it will return documents from the "Documents" database. Assuming you are using the default configuration for client and server, this would result in the behavior you are seeing. E.g. your Module code is in the Modules database, doing a GET for it by name will search the Documents database and correctly not find it.
You don't mention, and I don't know, the groovy library being used, but the REST API itself and all implementations of general purpose ML REST client libraries I am familiar with have options for overriding the default database with another. If the groovy library supports that, then specify the "Modules" database for your query and it should return the module document. Note: content-type will be application/text not text/xml.
You can simplify things for testing by bypassing the libraries and simply use a browser and try a URL like this http://yourserver.com:8000/v1/documents?uri=/your/module.xqy&database=Modules
Ref: https://docs.marklogic.com/REST/GET/v1/documents
Making the appropriate changes to the path and server for your use.
If you are still confused, then you should start with the basic MarkLogic tutorials and work through them one by one. You will most likely succeed faster by doing this then jumping straight into coding you don't understand yet.
DETAIL:
Note: The default behaviour is to EXECUTE documents when doing a GET call, using the Modules database. Thus doing a GET of http://yourserver:8000/your/module.xqy will EXECUTE it not return its source.
You will notice the REST API has a uri query parameter. This is EXECUTING the REST API code on /v1/documents which in turn will read the document specified by the uri and database parameters and return it.
I guess I can use:
xdmp:invoke(/pview/get-pview-browse-profiles.xqy,
cts:and-query((
cts:element-value-query(
xs:QName("letter"),"*", "wildcarded"),
cts:element-value-query(
xs:QName("collection"),"*", "wildcarded"))))
although it doesn't return anything

Execute R Script on AWS via API

I have an R package that I would like to host through Amazon Web Services that will be accessible via an API. The script should take a couple of input values and return the R output in json format. Also, the API should be able to handle multiple requests simultaneously.
So for example, call http://sampleapi.com/?location=USA?state=Florida. That would then run the R package and return the output data to the calling application.
Has anyone done this before or know of resources you can point me to that would explain how to do so? Thanks!
Thanks for all the suggestions. I decided to use Ruby for the API with the rinruby and rails-api gems and will host that through AWS Elastic Beanstalk. See this question for how I am setting it up - Ruby API - Accept parameters and execute script

Resources