Gremlin: Get all vertices hasLabel 'model' and their children with all properties - java

I have a graph which should have the following json structure
{
"models": [
{
"name": "Model1",
"id": 19,
"diagram": [
{
"name": "Diagram1",
"id": "34"
},
{
"name": "Diagram2",
"id": "36"
}
]
},
{
"name": "Model2",
"id": 14,
"diagram": [
{
"name": "Diagram2",
"id": "32"
}
]
}
]
}
Here is my sample graph:
g.addV('org').property('name', 'Org_seppmed').as('org1').addV('org').property('name', 'Org_siemens').as('org2').addV('user').property('name', 'U_Sebastian').as('user1').addV('user').property('name', 'U_Jaroslaw').as('user2').addV('user').property('name', 'U_Jürgen').as('user3').addV('user').property('name', 'U_Carsten').as('user4').addV('user').property('name', 'U_DevUser').as('user5').addE('administratedBy').from('org1').to('user1').addE('hasEmployee').from('org1').to('user2').addE('hasEmployee').from('org1').to('user3').addE('hasEmployee').from('org2').to('user4').addE('hasEmployee').from('org1').to('user5').addE('hasGuest').from('org1').to('user4').addV('project').property('name', 'P_Bremsen').as('projectmbt1').addV('project').property('name', 'P_Blinker').as('projectmbt2').addE('owns').from('user1').to('projectmbt1').addE('owns').from('user2').to('projectmbt2').addE('writes').from('user2').to('projectmbt1').addE('writes').from('user3').to('projectmbt1').addE('reads').from('user4').to('projectmbt1').addE('writes').from('user1').to('projectmbt2').addE('writes').from('user3').to('projectmbt2').addV('model').property('name', 'M_Bremse').as('modelmbt1').addV('model').property('name', 'M_Blinker').as('modelmbt2').addV('model').property('name', 'M_Bremse2').as('modelmbt3').addE('has').from('projectmbt1').to('modelmbt1').addE('has').from('projectmbt1').to('modelmbt3').addE('has').from('projectmbt2').to('modelmbt2').addV('diagram').property('name', 'D_D1').as('diagramd1').addV('diagram').property('name', 'D_D2').as('diagramd2').addV('diagram').property('name', 'D_D3').as('diagramd3').addV('diagram').property('name', 'D_D1').as('diagramd4').addV('testsuite').property('name', 'T_TestD1').as('testsuited1').addE('has').from('modelmbt1').to('diagramd1').addE('has').from('modelmbt1').to('diagramd2').addE('has').from('modelmbt2').to('diagramd3').addE('has').from('modelmbt3').to('diagramd4').addE('has').from('modelmbt1').to('testsuited1').addV('initial').as('initMBT1D1').addV('precondition').as('preMBT1D1').addV('teststep').property('name', 'N_Blinking left').as('node1MBT1D1').addV('teststep').property('name', 'N_Blinking right').as('node2MBT1D1').addV('teststep').property('name', 'N_Hazard flashers').as('node3MBT1D1').addV('teststep').property('name', 'N_Emergency flashers').as('node4MBT1D1').addV('teststep').property('name', 'N_Blinking off').as('node5MBT1D1').addV('node').as('connodeMBT1D1').addV('postcondition').as('postMBT1D1').addV('final').as('finalMBT1D1').addE('has').from('diagramd1').to('initMBT1D1').addE('has').from('diagramd1').to('preMBT1D1').addE('has').from('diagramd1').to('node1MBT1D1').addE('has').from('diagramd1').to('node2MBT1D1').addE('has').from('diagramd1').to('node3MBT1D1').addE('has').from('diagramd1').to('node4MBT1D1').addE('has').from('diagramd1').to('node5MBT1D1').addE('has').from('diagramd1').to('connodeMBT1D1').addE('has').from('diagramd1').to('postMBT1D1').addE('has').from('diagramd1').to('finalMBT1D1').addE('pointsTo').from('initMBT1D1').to('preMBT1D1').addE('pointsTo').from('preMBT1D1').to('node1MBT1D1').addE('pointsTo').from('preMBT1D1').to('node2MBT1D1').addE('pointsTo').from('preMBT1D1').to('node3MBT1D1').addE('pointsTo').from('preMBT1D1').to('node4MBT1D1').addE('pointsTo').from('preMBT1D1').to('node5MBT1D1').addE('pointsTo').from('node1MBT1D1').to('postMBT1D1').addE('pointsTo').from('postMBT1D1').to('node1MBT1D1').addE('pointsTo').from('node2MBT1D1').to('postMBT1D1').addE('pointsTo').from('node3MBT1D1').to('postMBT1D1').addE('pointsTo').from('node4MBT1D1').to('postMBT1D1').addE('pointsTo').from('node5MBT1D1').to('postMBT1D1').addE('pointsTo').from('postMBT1D1').to('connodeMBT1D1').addE('linksTo').from('connodeMBT1D1').to('diagramd2').addE('pointsTo').from('connodeMBT1D1').to('finalMBT1D1').addV('initial').as('initMBT1D2').addV('teststep').property('name', 'N_Trigger left').as('node1MBT1D2').addV('teststep').property('name', 'N_Trigger right').as('node2MBT1D2').addV('verificationpoint').property('name', 'N_Triggercheck').as('node3MBT1D2').addV('final').as('finalMBT1D2').addE('has').from('diagramd2').to('initMBT1D2').addE('has').from('diagramd2').to('node1MBT1D2').addE('has').from('diagramd2').to('node2MBT1D2').addE('has').from('diagramd2').to('node3MBT1D2').addE('has').from('diagramd2').to('finalMBT1D2').addE('pointsTo').from('initMBT1D2').to('node1MBT1D2').addE('pointsTo').from('initMBT1D2').to('node2MBT1D2').addE('pointsTo').from('node1MBT1D2').to('node3MBT1D2').addE('pointsTo').from('node2MBT1D2').to('node3MBT1D2').addE('pointsTo').from('node3MBT1D2').to('finalMBT1D2')
Now I am trying to find a query to get above structure:
g.V().hasLabel("model").out().hasLabel("diagram").path().by(elementMap())
The result is like:
==>path[{id=12384, label=model, name=Model2}, {id=28904, label=diagram, name=Diagram2}]
==>path[{id=37056, label=model, name=Model1}, {id=16448, label=diagram, name=Diagram1}]
==>path[{id=37056, label=model, name=Model1}, {id=24808, label=diagram, name=Diagram1}]
My expectation was to get a Map<Model, List<Diagram>>. But i get for each diagram of a model a complete new entry, instead of adding all related diagrams to a model.
How can I solve my problem?

If you want to generate a Map you will need to group using the model as the key. Something like shown below. Obviously, you will not really want to use the label but something that identifies the class of the model, if you have such a property, such as name.
g.V().hasLabel("model").
group().
by('name').
by(out().hasLabel("diagram").path().by(elementMap()).fold())
EDITED after discussion in comments:
If you do not want the whole path in the result but just the unique diagram nodes and their properties, the query can be amended to as shown below. This version also adds all of the properties from the label node as the group (map) key.
g.V().hasLabel("model").
group().
by(elementMap()).
by(out().hasLabel("diagram").elementMap().fold())

Related

After REST API call ,i have key/value pair in the below format ,I Need to extract just the specific value corresponding to the key

Option 1 :if i use the TALEND job i get the JSON results stored with backslashes in the JSON file for some reason(which is unparseable)
{"read":[{"Body":"{\"items\":[{\"executionId\":\"a0613a31-d16d-4c4d-9279-4564a86cdd44\",\"startTimestamp\":\"2021-09-15T22:30:26.854Z\",\"triggerTimestamp\":\"2021-09-15T22:30:27.209Z\",\"userId\":\"user1\",\"status\":\"dispatching\",\"runtime{\"type\":\"REMOTE_ENGINE_CLUSTER\"},\"executionStatus\":\"DISPATCHING_FLOW\"},{\"executionId\":\"49a56eb1-f3c7-4f26-9554-8fa88acde38b\",\"startTimestamp\":\"2021-09-15T22:29:15.999Z\",\"triggerTimestamp\":\"2021-09-15T22:29:16.447Z\",\"userId\":\"user1\",\"executionType\":\"MANUAL\",\"status\":\"dispatching\",\"runtime\":{\"type\":\"REMOTE_ENGINE_CLUSTER\"},\"executionStatus\":\"DISPATCHING_FLOW\"}],\"limit\":100,\"offset\":0,\"total\":2}","ERROR_CODE":null}]}
Option 2:Using the WEB URL:
After REST API call,i have the JSON results like:
BODY:context.statusbody
{
"items": [{
"executionId": "4f679c12-d8d7-4dd7-89d5-507a94503988",
"startTimestamp": "2021-09-14T19:05:01.854Z",
"triggerTimestamp": "2021-09-14T19:05:02.385Z",
"userId": "user1",
"taskId": "60b7f6d31c6e394de0163d35",
"status": "dispatching",
"runtime": {
"type": "REMOTE_ENGINE_CLUSTER"
},
"executionStatus": "DISPATCHING_FLOW"
},
{
"executionId": "4f40b04c-1ac1-42ea-9a36-7c25b1b17fe8",
"startTimestamp": "2021-09-14T19:00:24.769Z",
"triggerTimestamp": "2021-09-14T19:00:25.122Z",
"userId": "user1",
"taskId": "60b7f6d31c6e394de0163d35",
"executionType": "SCHEDULED",
"status": "dispatching",
"runtime": {
"type": "REMOTE_ENGINE_CLUSTER"
},
"executionStatus": "DISPATCHING_FLOW"
}
],
"limit": 100,
"offset": 0,
"total": 2
}
From this i just need to extract the executionID and triggerTimestamp for each iteration and store in a global variable
In my grayed out tjava3 i am using global variable as:
context.testexecutionID=((String)globalMap.get("executionID"));
context.triggerTimestamp=((String)globalMap.get("triggerTimestamp"));
context.executionID=context.testexecutionID.substring(5, context.testexecutionID.lastIndexOf("\"")-3);
context.triggerTime=context.triggerTimestamp.substring(5, context.triggerTimestamp.lastIndexOf("\"")-3);
#sarah Based on your valid JSON you added in your edit .
i suggest you to add after tRest to add tExtractJSONFields component paremetred as such :
Job Design should be in first test :
tRest -> tExtractJSONFields -> tLogRow .
If extraction is good . Second test you have to see my previous my first answer to get global Variables .
Okey , Json i Used for your case to do your requirement :
Note That your Json is not Valid
{
"items": [{
"executionId": "6e5fa777-9ede-42b9-b862-03b4b1b12375",
"startTimestamp": "2021-09-15T05:59:40.599Z",
"triggerTimestamp": "2021-09-15T05:59:41.006Z",
"userId": "user"
}],
"limit ": 100,
"offset ": 0,
"total ": 2
}
Without knowing your job design i just got the output that is posted right here .
My job design is as such :
Important !: your tFileInputJson should be like this to get the 2 fields
Same configuration if your want to used tExtractJSONFields .
to Stock your Global Variables :
to get the globalVariables easy way is to use tfixedFlowInput :
Output :

MongoDB Panache Updating nested objects within Document

I have a model which looks like this:
{
"projectName": "MyFirstProject",
"projectId": "1234",
"testCaseList": [
{
"testCaseName": "TestCase1",
"steps": [
{
"Action": "Click on this",
"Result": "pass"
},
{
"Action": "Click on that",
"Result": "pass"
}
]
},
{
"testCaseName": "TestCase2",
"steps": [
{
"Action": "Click on him",
"Result": "pass"
},
{
"Action": "Click on her",
"Result": "pass"
}
]
}
]
}
However, as this is a nested object, I am having difficulties updating it using the method:
default PanacheUpdate update(String update, Object... params)
I am using Repository Pattern and below is my code snippet:
List<TestCase> newTestCaseList = ...;
update("testCaseList", newTestCaseList).where("projectId=?1",projectId);
which actually throws the following error:
org.bson.json.JsonParseException: JSON reader was expecting ':' but found ','.
at org.bson.json.JsonReader.readBsonType(JsonReader.java:149)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:82)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
at org.bson.BsonDocument.parse(BsonDocument.java:63)
at io.quarkus.mongodb.panache.runtime.MongoOperations.executeUpdate(MongoOperations.java:634)
at io.quarkus.mongodb.panache.runtime.MongoOperations.update(MongoOperations.java:629)
My Current Approach
What currently works for me is to use default void update(Entity entity) instead when updating nested objects.
This however presents a few considerations:
Extra code is required to fetch the entire document, parse through, and update the required fields
Since update(Entity entity) works on a document level, it will also update unchanged parts of the document, which isn't ideal.
I guess the encountered error states nothing but a limitation of Panache for mongoDB for the moment through the standard offered PanacheQL.
The issue should be worked-around using native mongoDB Java API that can be accessed through the PanacheMongoEntityBase#mongoCollection:
mongoCollection().updateOne(
eq("projectId", projectId),
new Document("$set", new Document("testCaseList", newTestCaseList))
);

How can I store and update the nested json object into couchbase using java sdk

I am using couchbase Community Edition 5.0.1 and java-client 2.7.4. I want to store the following nested json object into couchbase. If I want to update the same object without affecting the other fields.
Eg:
If I want to add one more player object under players object
array
If I want to add One more group say 'Z Group' under group object array
How can I Achieve this without affecting other fields.
{
"doctype": "config:sample",
"group": [{
"name": "X Group",
"id": 1,
"players": [{
"name": "Roger Federer",
"number": 3286,
"keyword": "tennies"
},
{
"name": "P. V. Sindhu",
"number": 4723,
"keyword": "badminton"
}
]
},
{
"name": "Y Group",
"id": "2",
"players": [{
"name": "Jimmy Connors",
"number": 5623,
"keyword": "tennies"
},
{
"name": "Sachin",
"number": 8756,
"keyword": "Cricket"
}
]
}
]
}
N1QL has a huge variety of functions to operate on arrays:
https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/arrayfun.html
In your case, you could simply use ARRAY_INSERT or ARRAY_PREPEND
Check out update/update-for syntax (last example) https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/update.html
UPDATE default AS d
SET d.group = ARRAY_APPEND(d.group, {......})
WHERE .....;
UPDATE default AS d
SET g.players = ARRAY_APPEND(g.players, {......}) FOR g IN d.group WHEN g.id = 2 END
WHERE .....;
If you know which document IDs you want to update you can use the key-value subdocument API, which will generally be faster than going via N1QL for a single document update.
This will add a new player to the end of X Group's "players" array:
bucket.mutateIn(docId)
.arrayAppend("group[0].players",
JsonObject.create()
.put("name", "John Smith"))
// ... other player JSON
.execute();
And this will add a new Group Z to the "group" array:
bucket.mutateIn(docId)
.arrayAppend("group",
JsonObject.create()
.put("name", "Z Group"))
// ... other group JSON
.execute();

spark dataframes java explode function

I have the below json file
{
"data": [
{
"id": "254094394708288_946242468826807",
"from": {
"name": "Emirates NBD \u0628\u0646\u0643 \u0627\u0644\u0625\u0645\u0627\u0631\u0627\u062a \u062f\u0628\u064a \u0627\u0644\u0648\u0637\u0646\u064a",
"category": "Bank/Financial Institution",
"id": "254094394708288"
}
},
{
"id": "254094394708288_945515772232810",
"from": {
"name": "Emirates NBD \u0628\u0646\u0643 \u0627\u0644\u0625\u0645\u0627\u0631\u0627\u062a \u062f\u0628\u064a \u0627\u0644\u0648\u0637\u0646\u064a",
"category": "Bank/Financial Institution",
"id": "254094394708288"
}
}
]
}
I want to create a hive table based on JSON objects inside "data" JSON array. I want to do this in Java. I dont want to do "select data.id,data.from from temptable". I want to do something similar to to a select * query. It should automatically infer schema from json objects. I came to find explode function in python and scala.
http://stackoverflow.com/questions/31859271/sparksql-and-explode-on-dataframe-in-java
Is there something similar in Java??
In Java there is exactly the same thing:
org.apache.spark.sql.functions.explode(Column e)
which returns Column
You could check my answer here for the details

Mapfish Print Vector layer GeoJSON example?

I'm using MapFish Print to generate PDFs of maps, creating the map images in the pdf when I only use a WMS layer for base tiles is completely fine. Works as expected.
However, when I add a vector layer and a geoJSON object within it, mapfish print throws the following error:
Exception in thread "tilesReader3" java.lang.NullPointerException
at org.mapfish.print.map.renderers.vector.PointRenderer.renderImpl(PointRenderer.java:113)
at org.mapfish.print.map.renderers.vector.PointRenderer.renderImpl(PointRenderer.java:49)
at org.mapfish.print.map.renderers.vector.GeometriesRenderer.render(GeometriesRenderer.java:69)
at org.mapfish.print.map.renderers.vector.FeaturesRenderer$FeatureRenderer.renderImpl(FeaturesRenderer.java:62)
at org.mapfish.print.map.renderers.vector.FeaturesRenderer$FeatureRenderer.renderImpl(FeaturesRenderer.java:58)
at org.mapfish.print.map.renderers.vector.FeaturesRenderer.render(FeaturesRenderer.java:53)
at org.mapfish.print.map.renderers.vector.FeaturesRenderer$FeatureCollectionRenderer.renderImpl(FeaturesRenderer.java:70)
at org.mapfish.print.map.renderers.vector.FeaturesRenderer$FeatureCollectionRenderer.renderImpl(FeaturesRenderer.java:67)
at org.mapfish.print.map.renderers.vector.FeaturesRenderer.render(FeaturesRenderer.java:53)
at org.mapfish.print.map.readers.VectorMapReader$1.renderOnPdf(VectorMapReader.java:85)
at org.mapfish.print.map.ParallelMapTileLoader.handle(ParallelMapTileLoader.java:97)
at org.mapfish.print.map.ParallelMapTileLoader.handle(ParallelMapTileLoader.java:41)
at org.pvalsecc.concurrent.OrderedResultsExecutor.addOutput(OrderedResultsExecutor.java:148)
at org.pvalsecc.concurrent.OrderedResultsExecutor.access$600(OrderedResultsExecutor.java:23)
at org.pvalsecc.concurrent.OrderedResultsExecutor$Runner.run(OrderedResultsExecutor.java:181)
at java.lang.Thread.run(Unknown Source)
My query that I am sending to Mapfish print is a slightly modified version of the default sample (to use a different WMS as the default for the example is down), with the Vector layer added in, as follows:
{
layout: 'A4 portrait',
title: 'A simple example',
srs: 'EPSG:4326',
units: 'dd',
outputFilename: 'mapfish-print',
outputFormat: 'pdf',
layers: [
{
type: 'WMS',
format: 'image/png',
layers: ['OSM-GB:__all__'],
baseURL: 'http://www.osmgb.org.uk/ogc/wms'
},
{
type: 'Vector',
geoJson: {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-0.0996621,
51.5059247
]
},
"properties": {
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-0.099332,
51.5072000
]
},
"properties": {
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-0.0945508,
51.50240484
]
},
"properties": {
}
}
]
}
}
],
pages: [
{
center: [0.09, 51.50],
scale: 4000000,
dpi: 190,
mapTitle: "First map",
comment: "The \"routes\" layer is not shown (the scale is too small)",
data: [
{id:1, name: 'blah', icon: 'icon_pan'},
{id:2, name: 'blip', icon: 'icon_zoomin'}
]
},
{
center: [0.09, 51.50],
scale: 500000,
dpi: 190,
mapTitle: "Second map",
comment: "This is a zoomed in version of the first map. Since the scale is more appropriate, we show the \"routes\" layer.",
data: [
{id:1, name: 'blah', icon: 'icon_pan'},
{id:2, name: 'blip', icon: 'icon_zoomin'}
]
}
]
}
The GeoJSON validates and displays on geojsonlint.com, just fine. Having looked through the source for mapfish print I cannot figure out why exactly this is occurring. Though it seems to be something to do with styles, I have had no luck adding one into the request to fix this issue.
-- Update:
If anybody else ever comes across this issue, the problem was that (undocumented), you have to define within the vector layer, a set of styles, and make sure each feature in the geoJSON uses a style.
The problem was that (undocumented), you have to define within the vector layer, a set of styles and a styleproperty, and make sure each feature in the geoJSON uses a style.
The styles themselves are SVG styles, although many of the tags are renamed, so rather than being fill-color, they will be fillColor.
See here for a bit more detail: http://www.mapfish.org/doc/print/protocol.html#layers-params
The SVG styles which need to be defined are in the form of OpenLayers.Feature.Vector.style

Categories

Resources