Gremlin's "mean" equivalent in Java (Blueprints 2.6) - java

I'm using OrientDB 2.1.6 and have the following Gremling example query:
g.V("username", "testark").out("IsFriendsWith").height.mean()
This works great on the Gremlin shell, but I need to be able to use that from within Java. Using the Java Graph API I could translate most of it to
final GremlinPipeline<Long, Vertex> pipe = new GremlinPipeline<>(orientGraph);
pipe.V("username", "testark").out("IsFriendsWith").property("height").????????
but not I cannot seem to find the equivalent of the mean function. My current workaround involves computing the mean "manually" based on the return value of property, but I'd obviously prefer to get that value from the database via Gremlin. Can anyone please point out to me what's the equivalent of mean?

The mean function is a method of gremlin-groovy so you won't find it on the Java side:
https://github.com/tinkerpop/gremlin/blob/88de5e5d95bd0b704c5090c43a04a3d42992dfb9/gremlin-groovy/src/main/groovy/com/tinkerpop/gremlin/groovy/loaders/PipeLoader.groovy#L28-L35
You would need to do as you are doing and manually calculate the mean.

Related

JDBI failure case

I am looking at some examples of using the JDBI library for Java database access.
One such example is as follows....
List<String> names = mJdbi.withHandle(handle ->
handle.createQuery("select name from test_table")
.mapTo(String.class)
.list());
I am confused as to what happens when this call fails. For example, what if there is no table called test_table. What should I expect the outcome of this code to be in that case?
Well, what should you expect if any call fails in java? Maybe an Exception?
Be happy that jdbi unburdens you from dealing with SQLException directly. Here is what you will be facing https://jdbi.org/apidocs/org/jdbi/v3/core/JdbiException.html (or ady subclass thereof, probably StatementException in your case)
On a side note: it is <5 minutes of work setting up a project with in-mem db to try it out...

Java - Is there a way to query an Apache Spark schema without iterating?

I'm trying to find out if there's a way to directly query a struct from a Spark schema derived from a dataset of rows. Is there some sort of Java equivalent to the Scala provided dataframe.schema("nameOfStruct")?
I've tried finding such a prebuilt function, but the only thing I could find was a way to iterate through a list of Structs or make an iterator. This seems really redundant when Scala provides a much easier way of doing things, especially if I don't want to check through a loop or find the exact index of my desired Struct.
//adding the metadata to a column
final Metadata metadata = new MetadataBuilder().putLong("metadataExample", 1).build();
final Dataset<Row> dfWithColumnMetadata = df1.withColumn("column_example", df.col("column_example"), metadata);
/*now I want to find the exact Struct and its metadata without having to loop through
an array or create an iterator. However, the array version is the easiest way I could find.
The con here is that I need to know the exact index of the column.*/
System.out.println(dfWithColumnMetadata.schema().fields()[0].metadata().toString());
Is there a way that I could get something like Scala's df.schema("column_example").metadata() ?
I think you can use:
dfWithColumnMetadata.schema().apply("column_example").metadata()

Neo4j get Node by specific properties Java API

Is there a way to get a node by specific attributes using the Java API?
Specifically:
By Name
By Label
By properties - (various properties)
The only functions I found were:
findNode(label)
findNode(label, key, value)
Which only supports one property.
Or do I have to use Cypher to get this?
If your question is about how to find nodes that have specific multiple property values, the Java API has no method that does that task. In general, it is easier to use Cypher for that.

find all nodes with a given label and property using the neo4j java API

I know about the findNodesByLabelAndProperty method of the GraphDatabaseServiceObject, but that method requires a value parameter. In my case, I only want to know if there are any (1 or more) nodes in my database that have a specified label and property, but I don't know anything about the value of the property other than it's type. Any ideas?
Thanks
Howard
If you are using version 2.0 you could use the method getAllNodesWithLabel from class org.neo4j.tooling.GlobalGraphOperations.
GlobalGraphOperations.getAllNodesWithLabel(DynamicLabel.label("<label_name>"))
For more information: http://neo4j.com/api_docs//2.0.0-M06/org/neo4j/tooling/GlobalGraphOperations.html#getAllNodesWithLabel(org.neo4j.graphdb.Label)
Not sure I understand the question correctly, but I would just create another (boolean-valued) property for the purpose of finding the nodes.
You can create another node to store these kinds of counts- a single one with multiple properties such as "nodesWithLabelXAndPropertyY" with the count as the value which you can set at the end of your algorithm run. Or just a boolean exists/not exists value.
Then all you need to do is query this node for summary statistics.

Elasticsearch Java API - fuzzy search with max_expansion

How can I translate the "more complex" fuzzy example from the QueryDSL guide into Java?
What I have so far is this: (Which works fine, but for example I'm unable to find the builder methods for "max_expansion", which would allow me to restrict the query)
QueryBuilders.fuzzyQuery("name", "kimchy")
Any pointers into the right direction are appreciated.
It supposed to be QueryBuilders.fuzzyQuery("name", "kimchy").maxExpansion(5). But, unfortunately, the maxExpansion() method is currently missing. So, until this pull request is merged, the only way to send this query is by expressing it directly in json. You can do it using XContentBuilder.
Construct a Lucene FuzzyQuery directly, then you can pass that option into a constructor arg.

Categories