I have the following query:
g
.V("user-11")
.repeat(bothE().subgraph("subGraph").outV())
.times(2)
.cap("subGraph")
.next()
When I run it using gremlin-python, I receive the following response:
{'#type': 'tinker:graph',
'#value': {'vertices': [v[device-3], v[device-1], v[user-11], v[card-1]],
'edges': [e[68bad734-db2b-bffc-3e17-a0813d2670cc][user-11-uses_device->device-1],
e[14bad735-2b70-860f-705f-4c0b769a7849][user-11-uses_device->device-3],
e[f0bb3b6d-d161-ec60-5e6d-068272297f24][user-11-uses_card->card-1]]}}
Which is a Graphson representation of the subgraph obtained by the query.
I want to get the same response using Java and gremlin-driver but I haven't been able to figure how.
My best try was:
ObjectMapper mapper = GraphSONMapper.build().version(GraphSONVersion.V3_0).create().createMapper();
Object a = graphTraversalSource
.V(nodeId)
.repeat(bothE().subgraph("subGraph").outV())
.times(2)
.cap("subGraph")
.next();
return mapper.writeValueAsString(a);
But that gave me the following error:
io.netty.handler.codec.DecoderException: org.apache.tinkerpop.gremlin.driver.ser.SerializationException: org.apache.tinkerpop.shaded.kryo.KryoException: Encountered unregistered class ID: 65536
I am using AWS Neptune, but I doubt that makes a difference given that I receive the answer I want through gremlin-python.
I appreciate any help you can give! Thanks
As mentioned in the comments
When using Java what you get back will be an actual TinkerGraph
Using the GraphBinary or GraphSONV3D0 serializer is recommended.
The Gyro one is older and is likely causing the error you saw if you did not specify one of the others serializers.
Note that even if you use one of the other serializers, to get the graph to deserialize into JSON you will need to use the specific TinkerGraph serializer (see the end of this answer for an example). Otherwise you will just get {} returned.
However, you may not need to produce JSON at all in the case of the Java Gremlin client ....
Given you have an actual TinkerGraph back you can run real Gremlin queries against the in-memory subgraph - just create a new traversal source for it. You can also use the graph.io classes to write the graph to file should you wish to. The TinkerGraph will include properties as well as edges and vertices.
You can also access the TinkerGraph object directly using statements such as
a.vertices and a.edges
By means of a concrete example, if you have a query of the form
TinkerGraph tg = (TinkerGraph)g.V().bothE().subgraph("sg").cap("sg").next();
Then you can do
GraphTraversalSource g2 = tg.traversal();
Long cv = g2.V().count().next();
Long ce = g2.E().count().next();
Or you can just access the TinkerGraph data structure directly using statements of the form:
Vertex v = tg.vertices[<some-id>]
Or
List properties = tg.vertices[<some-id>].properties()
This actually means you have a lot more power available to you in the Java client when working with subgraphs.
If you still feel that you need a JSON version of your subgraph, the IO reference is a handy bookmark to have: https://tinkerpop.apache.org/docs/3.4.9/dev/io/#_io_reference
EDITED: - to save you a lot of reading the docs, this code will print a TinkerGraph as JSON
mapper = GraphSONMapper.build().
addRegistry(TinkerIoRegistryV3d0.instance()).
version(GraphSONVersion.V3_0).create().createMapper();
mapper.writeValueAsString(tg)
Related
I have been trying to import and make use of my trained model (Tensorflow, Python) in Java.
I was able to save the model in Python, but encountered problems when I try to make predictions using the same model in Java.
Here, you can see the python code for initializing, training, saving the model.
Here, you can see the Java code for importing and making predictions for input values.
The error message I get is:
Exception in thread "main" java.lang.IllegalStateException: Attempting to use uninitialized value Variable_7
[[Node: Variable_7/read = Identity[T=DT_FLOAT, _class=["loc:#Variable_7"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_7)]]
at org.tensorflow.Session.run(Native Method)
at org.tensorflow.Session.access$100(Session.java:48)
at org.tensorflow.Session$Runner.runHelper(Session.java:285)
at org.tensorflow.Session$Runner.run(Session.java:235)
at org.tensorflow.examples.Identity_import.main(Identity_import.java:35)
I believe, the problem is somewhere in the python code, but I was not able to find it.
The Java importGraphDef() function is only importing the computational graph (written by tf.train.write_graph in your Python code), it isn't loading the values of trained variables (stored in the checkpoint), which is why you get an error complaining about uninitialized variables.
The TensorFlow SavedModel format on the other hand includes all information about a model (graph, checkpoint state, other metadata) and to use in Java you'd want to use SavedModelBundle.load to create session initialized with the trained variable values.
To export a model in this format from Python, you might want to take a look at a related question Deploy retrained inception SavedModel to google cloud ml engine
In your case, this should amount to something like the following in Python:
def save_model(session, input_tensor, output_tensor):
signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs = {'input': tf.saved_model.utils.build_tensor_info(input_tensor)},
outputs = {'output': tf.saved_model.utils.build_tensor_info(output_tensor)},
)
b = saved_model_builder.SavedModelBuilder('/tmp/model')
b.add_meta_graph_and_variables(session,
[tf.saved_model.tag_constants.SERVING],
signature_def_map={tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature})
b.save()
And invoke that via save_model(session, x, yhat)
And then in Java load the model using:
try (SavedModelBundle b = SavedModelBundle.load("/tmp/mymodel", "serve")) {
// b.session().run(...)
}
Hope that helps.
Fwiw, Deeplearning4j lets you import models trained on TensorFlow with Keras 1.0 (Keras 2.0 support is on the way).
https://deeplearning4j.org/model-import-keras
We also built a library called Jumpy, which is a wrapper around Numpy arrays and Pyjnius that uses pointers instead of copying data, which makes it more efficient than Py4j when dealing with tensors.
https://deeplearning4j.org/jumpy
Your python-model will certainly fail at this:
sess.run(init) #<---this will fail
save_model(sess)
error = tf.reduce_mean(tf.square(prediction - y))
#accuracy = tf.reduce_mean(tf.cast(error, 'float'))
print('Error:', error)
init is not defined in the model - I'm unsure what you want achieve at this place, but that should give you a starting point
I am newbie for cloudant and trying to learn full text search on cloudant through tutorial video. I am successful in searching on the cloudant.com through http request ,Now I want that code to be in java as i am working on GWT framework with java. Till now , I am just able to create connection with cloudant.com by studying the particular github project GITHUBLINK
There its given for search like this
Search search = db.search("views101/animals");
SearchResult<Animal> rslt = search
.limit(10)
.includeDocs(true)
.counts(new String[] {"class","diet"})
.querySearchResult("l*", Animal.class);
My Questions:
1. what exactly is this Animal.class refer to?
2. If this not the way what are the steps for full text search on cloudant.
I have created the view and the search index manually on cloudant.com under a designdoc of a database.
Animal.class refers to the class that the documents found will be deserialised into, if you do not have a class to deserialise the data into, you should be able to a HashMap or similar class to access the data returned.
You can find the example source code for the Animal class in this location: https://github.com/cloudant/java-cloudant/blob/88202a1bd7b9b04d96c4b7b8498a1b8f7f99c9e5/src/test/java/com/cloudant/tests/Animal.java.
Like the previous answer indicated, you could also have the results returned as a generic JsonObject that provides access to the properties and values.
I'm trying to use Olingo to provide a client for interacting with an OData service (also written in Olingo). I'm trying to send a PATCH. However, the standard validation routines are kicking in and if I do not include those elements of the entity that are marked as non-nullable using the standard Olingo tools, I get an error.
in https://olingo.apache.org/doc/odata2/tutorials/OlingoV2BasicClientSample.html it says:
With an HTTP MERGE/PATCH it is also possible to send only the to be updated data as POST Body and omitting the unchanged data. But this is (currently) not shown within this sample.
Unfortunately I'm not sure how to do this, there, doesn't seem to be anywhere to flag to the EntityProvider.writeEntry method that it is a PATCH not a POST/PUT
EntityProviderWriteProperties properties = EntityProviderWriteProperties
.serviceRoot(rootUri).omitJsonWrapper(true).contentOnly(true)
.build();
// serialize data into ODataResponse object
ODataResponse response = EntityProvider.writeEntry(contentType,
entitySet, data, properties);
At this point in my code I get an error if "data" does not contain an entry for my non-nullable fields. The response also returns null values for all the attributes of the entity that aren't in my "data".
I deal with this by manipulating the response to remove all entries not in my "data" after the "standard" generation, but imagine that there must be a better way, even if I can't see it. Any suggestions on how to deal with this?
You have to create an "ExpandSelectTreeNode" which contains only the name of the selected properties to be serialized.
Assuming that your data is a HashMap with the values you can use following code as an example to start from:
// data = new HashMap<>();
ExpandSelectTreeNode node = ExpandSelectTreeNode.entitySet(entitySet)
.selectedProperties(new ArrayList<String>(data.keySet())).build();
EntityProviderWriteProperties properties = EntityProviderWriteProperties
.serviceRoot(rootUri).omitJsonWrapper(true).contentOnly(true)
.expandSelectTree(node)
.build();
// serialize data into ODataResponse object
ODataResponse response = EntityProvider.writeEntry(contentType,
entitySet, data, properties);
Best Regards
Is the contenttype from the client application/json-patch+json ?
I am facing a issue where I am not able to get the document information associated with the record.
The line below is used to get a particular record,
com.ibm.jarm.api.core.Record r = RMFactory.Record.fetchInstance(jarmFPOS, "{AE10E0F1-323F-4445-A529-78F744E8D3E4}", null);
Now that I have a record object I need to fetch the document information such as DocId, DocTitle, DocOwner etc..I know that the required information is stored in a property called as “RecordedDocuments” or we can use RMProperty.DOCUMENT_LINK from which we can get the value. I am using something like below.
Object obj3 = (Object) r.getProperties().get(RMProperty.DOCUMENT_LINK).getObjectValue();
My issue is here. I am not able to get the values from the Object. I tried typecasting it to a document object but no luck. I keep on getting typecasting error. Can anyone please help me how to get the desired output.
Thanks.
The Link object would not be a document directly... I'm not very familiar with RM APIs, more with CE APIs, but it would probably be an implementation of the RMLink interface.
What I usually do in such a case is print out the java class name of the object (obj3.getClass().getName()) and that would give you a good indication on which API object you can use.
May be because it was a list and not a Document that fails the casting? Try what polonoko told you and print the className.
Or try this code :)
List<com.filenet.api.core.Document> doc = (List<com.filenet.api.core.Document>) r.getProperties().get(RMProperty.DOCUMENT_LINK).getObjectValue();
How can one specify a custom object as a parameter for the web-service's method when invoking through SOAP message?
Say I have this code:
SOAPElement operation = body.addChildElement("MyMethod", "", trgNamespace);
SOAPElement value = operation.addChildElement("arg0");
value.addTextNode("i need to send here a custom object not a string")
request.saveChanges();
The addTextNode sends a string whereas I need to send my own object as a parameter for invocation.
You have to serialize your object to transfer it over the line. Serialization is often done using XML or JSON, see the following link for details: http://en.wikipedia.org/wiki/Serialization
That should get you on the right path.
Maybe try higher level and use wsdl-based stubs generator for java? It's Axis wsdl to java
I could think of another approach
You can send that custom object as a binary data (I assume your object is serialize-able). Then encode that data in say Base64 encoding.
There is similar problem asked earlier. Plz check out this link. This seems most relevant to your problem.
Another link mentioned in the above posting gives nice overview of handling these type of problems in general.