Optimize this query to get lineage diagram in graph using gremlin - azure-cosmosdb

I have my data set up in cosmos DB gremlin API. I am trying to write a query that can starting at a given vertex, gives all the edges with labels and properties in both directions, and the vertices with their properties, with a loop count of lets say 2. I am running the following queries.
Data to be setup
g.addV('item').property('id','1').property('name', 'item1')
g.addV('item').property('id','2').property('name', 'item2')
g.addV('item').property('id','3').property('name', 'item3')
g.addV('item').property('id','4').property('name', 'item4')
g.addV('item').property('id','5').property('name', 'item5')
g.addV('item').property('id','6').property('name', 'item6')
g.addV('item').property('id','7').property('name', 'item7')
g.addV('report').property('id','8').property('name', 'report1')
g.addV('report').property('id','9').property('name', 'report2')
g.addV('folder').property('id','10').property('name', 'folder1')
g.addV('server').property('id','11').property('name', 'server1')
g.V('1').addE('hasParent').to(g.V('2'))
g.V('1').addE('hasParent').to(g.V('3'))
g.V('2').addE('hasParent').to(g.V('4'))
g.V('5').addE('hasParent').to(g.V('6'))
g.V('4').addE('hasParent').to(g.V('5'))
g.V('7').addE('hasParent').to(g.V('5'))
g.V('8').addE('hasParent').to(g.V('9'))
g.V('8').addE('hasParent').to(g.V('9'))
g.V('8').addE('belongsTo').to(g.V('10'))
g.V('10').addE('belongsTo').to(g.V('11'))
query: g.V('id','1').repeat(bothE().dedup().otherV()).times(2).path()
response:
[
{
"labels": [
[],
[],
[],
[],
[]
],
"objects": [
{
"id": "1",
"label": "item",
"type": "vertex",
"properties": {
"name": [
{
"id": "1|name",
"value": "item1"
}
]
}
},
{
"id": "a6b02073-68f8-4d53-8af0-54739eaaeaa0",
"label": "hasParent",
"type": "edge",
"inVLabel": "item",
"outVLabel": "item",
"inV": "2",
"outV": "1"
},
{
"id": "2",
"label": "item",
"type": "vertex",
"properties": {
"name": [
{
"id": "2|name",
"value": "item2"
}
]
}
},
{
"id": "c8722748-3899-4a2c-afe4-6f13b6c1f8d5",
"label": "hasParent",
"type": "edge",
"inVLabel": "item",
"outVLabel": "item",
"inV": "4",
"outV": "2"
},
{
"id": "4",
"label": "item",
"type": "vertex",
"properties": {
"name": [
{
"id": "4|name",
"value": "item4"
}
]
}
}
]
},
{
"labels": [
[],
[],
[],
[],
[]
],
"objects": [
{
"id": "1",
"label": "item",
"type": "vertex",
"properties": {
"name": [
{
"id": "1|name",
"value": "item1"
}
]
}
},
{
"id": "a6b02073-68f8-4d53-8af0-54739eaaeaa0",
"label": "hasParent",
"type": "edge",
"inVLabel": "item",
"outVLabel": "item",
"inV": "2",
"outV": "1"
},
{
"id": "2",
"label": "item",
"type": "vertex",
"properties": {
"name": [
{
"id": "2|name",
"value": "item2"
}
]
}
},
{
"id": "a6b02073-68f8-4d53-8af0-54739eaaeaa0",
"label": "hasParent",
"type": "edge",
"inVLabel": "item",
"outVLabel": "item",
"inV": "2",
"outV": "1"
},
{
"id": "1",
"label": "item",
"type": "vertex",
"properties": {
"name": [
{
"id": "1|name",
"value": "item1"
}
]
}
}
]
},
{
"labels": [
[],
[],
[],
[],
[]
],
"objects": [
{
"id": "1",
"label": "item",
"type": "vertex",
"properties": {
"name": [
{
"id": "1|name",
"value": "item1"
}
]
}
},
{
"id": "2f0c5890-3fa5-4d7d-8ada-910e5419750f",
"label": "hasParent",
"type": "edge",
"inVLabel": "item",
"outVLabel": "item",
"inV": "3",
"outV": "1"
},
{
"id": "3",
"label": "item",
"type": "vertex",
"properties": {
"name": [
{
"id": "3|name",
"value": "item3"
}
]
}
},
{
"id": "2f0c5890-3fa5-4d7d-8ada-910e5419750f",
"label": "hasParent",
"type": "edge",
"inVLabel": "item",
"outVLabel": "item",
"inV": "3",
"outV": "1"
},
{
"id": "1",
"label": "item",
"type": "vertex",
"properties": {
"name": [
{
"id": "1|name",
"value": "item1"
}
]
}
}
]
}
]
This is giving me the desired output, but has a lot of repetitions, i am guessing because it prints the path as it traverses. How can I modify this query to return the smallest possible json output? Just return the vertices and edges once. I am using cosmosDB gremlin APIs if that makes any difference.
Response needed: just a list of vertices and edges needed for painting this graph.

Your example as-is doesn't produce the same output that I see in your question - I get just one path:
gremlin> g.V('id','1').repeat(bothE().dedup().otherV()).times(2).path()
==>[v[1],e[11][1-hasParent->2],v[2],e[13][2-hasParent->4],v[4]]
I'm not sure if that's some mistake in your data script but I think I get the gist of what you are asking from your output. I can recreate a situation of duplication by doing an emit() on the repeat() to include all paths in the output:
gremlin> g.V('id','1').repeat(bothE().dedup().otherV()).emit().times(2).path()
==>[v[1],e[11][1-hasParent->2],v[2]]
==>[v[1],e[12][1-hasParent->3],v[3]]
==>[v[1],e[11][1-hasParent->2],v[2],e[13][2-hasParent->4],v[4]]
Now I have some duplication of vertices/edges in the resulting output. If you just want a unique list of all those objects, just unfold() each Path and dedup():
gremlin> g.V('id','1').repeat(bothE().dedup().otherV()).emit().times(2).path().unfold().dedup()
==>v[1]
==>e[11][1-hasParent->2]
==>v[2]
==>e[12][1-hasParent->3]
==>v[3]
==>e[13][2-hasParent->4]
==>v[4]
This is a bit of an interesting pattern in a sense because it provides an easy way to produce a subgraph for Gremlin Language Variants and graph systems that don't support subgraph() step.

Related

I need help on to configure a Wordpress dApp to mint NFT

This is what how the minted picture shows up I have a dApp i'm working on with wordpress. I used Enefti Theme and i used ModelTheme plugin as my NFT creator and i included their contract on my dApp, it shows that i have successfully minted the NFT, but i can't find it on my dApp even though it's confirmed on bsc scan. also, when i import it on my mobile metamask it doesn't show the photo i minted, just a blank picture.
This is the ABI of the contract (on the configure page, it requires ABI and bytecode put together but i can't paste the bytecode here):
{
"_format": "hh-sol-artifact-1",
"contractName": "NFT",
"sourceName": "contracts/NFT.sol",
"abi": [
{
"inputs": [
{
"internalType": "string",
"name": "_name",
"type": "string"
},
{
"internalType": "string",
"name": "_symbol",
"type": "string"
},
{
"internalType": "string",
"name": "_initBaseURI",
"type": "string"
},
{
"internalType": "string",
"name": "_initNotRevealedUri",
"type": "string"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "approved",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "operator",
"type": "address"
},
{
"indexed": false,
"internalType": "bool",
"name": "approved",
"type": "bool"
}
],
"name": "ApprovalForAll",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "previousOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnershipTransferred",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "approve",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "baseExtension",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "cost",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "getApproved",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "operator",
"type": "address"
}
],
"name": "isApprovedForAll",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "maxMintAmount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "maxSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_mintAmount",
"type": "uint256"
}
],
"name": "mint",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "notRevealedUri",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "ownerOf",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bool",
"name": "_state",
"type": "bool"
}
],
"name": "pause",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "paused",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "renounceOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "reveal",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "revealed",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "from",
"type": "address"
},
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "safeTransferFrom",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "from",
"type": "address"
},
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "_data",
"type": "bytes"
}
],
"name": "safeTransferFrom",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "operator",
"type": "address"
},
{
"internalType": "bool",
"name": "approved",
"type": "bool"
}
],
"name": "setApprovalForAll",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_newBaseExtension",
"type": "string"
}
],
"name": "setBaseExtension",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_newBaseURI",
"type": "string"
}
],
"name": "setBaseURI",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_newCost",
"type": "uint256"
}
],
"name": "setCost",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_notRevealedURI",
"type": "string"
}
],
"name": "setNotRevealedURI",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_newmaxMintAmount",
"type": "uint256"
}
],
"name": "setmaxMintAmount",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes4",
"name": "interfaceId",
"type": "bytes4"
}
],
"name": "supportsInterface",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "index",
"type": "uint256"
}
],
"name": "tokenByIndex",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "uint256",
"name": "index",
"type": "uint256"
}
],
"name": "tokenOfOwnerByIndex",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "tokenURI",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "from",
"type": "address"
},
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_owner",
"type": "address"
}
],
"name": "walletOfOwner",
"outputs": [
{
"internalType": "uint256[]",
"name": "",
"type": "uint256[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "withdraw",
"outputs": [],
"stateMutability": "payable",
"type": "function"
}
]
}

Spatial Search in Here Data Hub not working

I have created a space in Here DataHub and multiple features in it. The format of feature is -
{
"type": "Feature",
"id": "5mmUm99MMRp7TSfO",
"geometry": {
"type": "Point",
"bbox": null,
"coordinates": [
-2.960847,
53.430828,
0.0
]
},
"properties": {
"#ns:com:here:xyz": {
"space": "4XICUXLX",
"createdAt": 1648724559819,
"updatedAt": 1648724559819,
"uuid": null,
"puuid": null,
"muuid": null,
"tags": [
"car",
"sedan"
]
},
"featureName": "4b44eab9-d030-44b5-9330-66132766666b",
"lon": 53.430828,
"geometry": {
"type": "Point",
"coordinates": {
"lat": -2.960847,
"lon": 53.430828
}
},
"lat": -2.960847
}
}
There are multiple features with these coordinates of lat and lon.
I want to search for features in the spatial in a radius of x m.
After executing spatial search APi result is empty, it contains no features -
curl -X GET "https://xyz.api.here.com/hub/spaces/4XICUXLX/spatial?lat=-2.960847&lon=53.430828&radius=0&limit=100&p.lat=-2.960847&p.lon=53.430828" -H "accept: application/geo+json"
Result - { "type": "FeatureCollection", "etag": "\"b4ce422e09aaae1bb21bd51e8e3029bf\"", "features": [] }
When I search for features in general by property by calling search API -
https://xyz.api.here.com/hub/spaces/4XICUXLX/search?p.lat=-2.960847&p.lon=53.430828
Response is list of features having that coordinate -
{
"type": "FeatureCollection",
"etag": "\"ce76cc9fbba2305d0f6c182237a6d316\"",
"features": [
{
"id": "5mmUm99MMRp7TSfO",
"type": "Feature",
"properties": {
"lat": -2.960847,
"lon": 53.430828,
"geometry": {
"type": "Point",
"coordinates": {
"lat": -2.960847,
"lon": 53.430828
}
},
"featureName": "4b44eab9-d030-44b5-9330-66132766666b",
"#ns:com:here:xyz": {
"tags": [
"car",
"sedan"
],
"space": "4XICUXLX",
"createdAt": 1648724559819,
"updatedAt": 1648724559819
}
},
"geometry": {
"type": "Point",
"coordinates": [
-2.960847,
53.430828,
0
]
}
},
{
"id": "asojLXk2T8h1A0uR",
"type": "Feature",
"properties": {
"lat": -2.960847,
"lon": 53.430828,
"geometry": {
"type": "Point",
"coordinates": {
"lat": -2.960847,
"lon": 53.430828
}
},
"featureName": "e251e750-fcd0-484d-9c75-eda54601784d",
"#ns:com:here:xyz": {
"tags": [
"car",
"sedan"
],
"space": "4XICUXLX",
"createdAt": 1648722389306,
"updatedAt": 1648722389306
}
},
"geometry": {
"type": "Point",
"coordinates": [
-2.960847,
53.430828,
0
]
}
}
]
}
Can anyone please help me with what is wrong in the spatial API call/query? Why results are not coming in it?

Retrieve consecutive array element from an array using JSONPath without using index, only by their value

I have a json body as below -
{
"type": "SourceUnit",
"children": [
{
"type": "PragmaDirective",
"name": "solidity",
"value": ">=0.4.22 <0.9.0"
},
{
"type": "ContractDefinition",
"name": "txorigin",
"baseContracts": [],
"subNodes": [],
"kind": "contract"
}
],
"tokens": [
{
"type": "Keyword",
"value": "pragma"
},
{
"type": "Identifier",
"value": "solidity"
},
{
"type": "Punctuator",
"value": ">="
},
{
"type": "Version",
"value": "0.4.22"
},
{
"type": "Punctuator",
"value": "<"
},
{
"type": "Version",
"value": "0.9.0"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Keyword",
"value": "contract"
},
{
"type": "Identifier",
"value": "txorigin"
},
{
"type": "Punctuator",
"value": "{"
},
{
"type": "type",
"value": "address"
},
{
"type": "Identifier",
"value": "owner"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Keyword",
"value": "constructor"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Keyword",
"value": "public"
},
{
"type": "Punctuator",
"value": "{"
},
{
"type": "Identifier",
"value": "owner"
},
{
"type": "Punctuator",
"value": "="
},
{
"type": "Identifier",
"value": "msg"
},
{
"type": "Punctuator",
"value": "."
},
{
"type": "Identifier",
"value": "sender"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Punctuator",
"value": "}"
},
{
"type": "Keyword",
"value": "function"
},
{
"type": "Identifier",
"value": "sendTo"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "type",
"value": "address"
},
{
"type": "Identifier",
"value": "receiver"
},
{
"type": "Punctuator",
"value": ","
},
{
"type": "type",
"value": "uint"
},
{
"type": "Identifier",
"value": "amount"
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Keyword",
"value": "public"
},
{
"type": "Punctuator",
"value": "{"
},
{
"type": "Identifier",
"value": "require"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Identifier",
"value": "tx"
},
{
"type": "Punctuator",
"value": "."
},
{
"type": "Identifier",
"value": "origin"
},
{
"type": "Punctuator",
"value": "=="
},
{
"type": "Identifier",
"value": "owner"
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "type",
"value": "bool"
},
{
"type": "Identifier",
"value": "success"
},
{
"type": "Punctuator",
"value": ","
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": "="
},
{
"type": "Identifier",
"value": "receiver"
},
{
"type": "Punctuator",
"value": "."
},
{
"type": "Identifier",
"value": "call"
},
{
"type": "Punctuator",
"value": "."
},
{
"type": "Identifier",
"value": "value"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Identifier",
"value": "amount"
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Keyword",
"value": ""
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Identifier",
"value": "require"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Identifier",
"value": "success"
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Punctuator",
"value": "}"
},
{
"type": "Punctuator",
"value": "}"
},
{
"type": "Keyword",
"value": "<EOF>"
}
]
}
whenever any code executes, I want to check that the above "tx", ".", "origin" values are available in my generated json or not. and, it should be consecutive like (tx.origin). if it is available it will return something. I am available to get any one value - .tokens[?(#.value == "tx")].value
I need to check all 3 in a consecutive manner.
Could you guys help me?
Thanks in advance.
Another option is to just get the values
$.tokens[*].value
This will get you all the values. They should be in the right sequence, but there's no guarantee.

Why is averageRating always 0?

I am using the 'Explore Popular Places by Category' API and it always returns averageRating as 0 for all items in the result.
I made this call with various categories such as restaurants and sights-museums, but it always returns 0 for averageRating. Does HERE not provide this detail, and if so, why is it returned in the response?
Some details:
I am making a call to this url:
https://places.ls.hereapi.com/places/v1/discover/explore?at=52.5159%2C13.3777&cat=sights-museums&apiKey={api_key}
and it returns something like this:
{
"results": {
"next": "https://places.ls.hereapi.com/places/v1/discover/explore;context=Y2F0PXNpZ2h0cy1tdXNldW1zJmZsb3ctaWQ9Mjk0ZjU1NTgtMmY0Mi01Y2FiLWJlYWUtOGEyM2ViY2EzMzgzXzE1ODMyNjMyNjEwMzZfMF84NTcyJm9mZnNldD0yMCZzaXplPTIw?at=52.5159%2C13.3777&app_id=LKO34glU2MBEVbcOD5mQ&app_code=A2ta_nQ8HRYwenju5HFG5Q",
"items": [
{
"position": [
52.51629,
13.37817
],
"distance": 54,
"title": "Brandenburg Gate",
"averageRating": 0,
"category": {
"id": "landmark-attraction",
"title": "Landmark/Attraction",
"href": "https://places.ls.hereapi.com/places/v1/categories/places/landmark-attraction?app_id=LKO34glU2MBEVbcOD5mQ&app_code=A2ta_nQ8HRYwenju5HFG5Q",
"type": "urn:nlp-types:category",
"system": "places"
},
"icon": "https://download.vcdn.data.here.com/p/d/places2/icons/categories/38.icon",
"vicinity": "Pariser Platz<br/>Mitte, 10117 Berlin",
"having": [],
"type": "urn:nlp-types:place",
"href": "https://places.ls.hereapi.com/places/v1/places/276u33db-8ee2e0de906e459cbade0593986debe9;context=Zmxvdy1pZD0yOTRmNTU1OC0yZjQyLTVjYWItYmVhZS04YTIzZWJjYTMzODNfMTU4MzI2MzI2MTAzNl8wXzg1NzImcmFuaz0w?app_id=LKO34glU2MBEVbcOD5mQ&app_code=A2ta_nQ8HRYwenju5HFG5Q",
"id": "276u33db-8ee2e0de906e459cbade0593986debe9",
"alternativeNames": [
{
"name": "Brandenburger Tor",
"language": "de"
}
]
},
{
"position": [
52.51373,
13.37976
],
"distance": 279,
"title": "Holocaust Memorial",
"averageRating": 0,
"category": {
"id": "museum",
"title": "Museum",
"href": "https://places.ls.hereapi.com/places/v1/categories/places/museum?app_id=LKO34glU2MBEVbcOD5mQ&app_code=A2ta_nQ8HRYwenju5HFG5Q",
"type": "urn:nlp-types:category",
"system": "places"
},
"icon": "https://download.vcdn.data.here.com/p/d/places2/icons/categories/10.icon",
"vicinity": "Cora-Berliner-Straße 1<br/>Mitte, 10117 Berlin",
"having": [],
"type": "urn:nlp-types:place",
"href": "https://places.ls.hereapi.com/places/v1/places/276u33de-df7d57fd38494a93b2018fe549a0fd75;context=Zmxvdy1pZD0yOTRmNTU1OC0yZjQyLTVjYWItYmVhZS04YTIzZWJjYTMzODNfMTU4MzI2MzI2MTAzNl8wXzg1NzImcmFuaz0x?app_id=LKO34glU2MBEVbcOD5mQ&app_code=A2ta_nQ8HRYwenju5HFG5Q",
"id": "276u33de-df7d57fd38494a93b2018fe549a0fd75",
"openingHours": {
"text": "Tue-Sun: 10:00 - 20:00",
"label": "Opening hours",
"isOpen": false,
"structured": [
{
"start": "T100000",
"duration": "PT10H00M",
"recurrence": "FREQ:DAILY;BYDAY:TU,WE,TH,FR,SA,SU"
}
]
},
"alternativeNames": [
{
"name": "Memorial to the Murdered European Jews",
"language": "en"
},
{
"name": "Memorial to the Murdered Jews of Europe",
"language": "en"
},
{
"name": "Denkmal für die ermordeten Juden Europas",
"language": "de"
}
]
},
{
"position": [
52.51666,
13.40784
],
"distance": 2041,
"title": "Nicholas Quarter",
"averageRating": 0,
"category": {
"id": "museum",
"title": "Museum",
"href": "https://places.ls.hereapi.com/places/v1/categories/places/museum?app_id=LKO34glU2MBEVbcOD5mQ&app_code=A2ta_nQ8HRYwenju5HFG5Q",
"type": "urn:nlp-types:category",
"system": "places"
},
"icon": "https://download.vcdn.data.here.com/p/d/places2/icons/categories/10.icon",
"vicinity": "Nikolaikirchplatz<br/>Mitte, 10178 Berlin",
"having": [],
"type": "urn:nlp-types:place",
"href": "https://places.ls.hereapi.com/places/v1/places/276u33dc-049683d3c6be4bdba823808678a1b164;context=Zmxvdy1pZD0yOTRmNTU1OC0yZjQyLTVjYWItYmVhZS04YTIzZWJjYTMzODNfMTU4MzI2MzI2MTAzNl8wXzg1NzImcmFuaz0xOQ?app_id=LKO34glU2MBEVbcOD5mQ&app_code=A2ta_nQ8HRYwenju5HFG5Q",
"id": "276u33dc-049683d3c6be4bdba823808678a1b164",
"alternativeNames": [
{
"name": "Nikolaiviertel",
"language": "en"
},
{
"name": "Nikolaiviertel",
"language": "de"
}
]
}
]
},
"search": {
"context": {
"location": {
"position": [
52.5159,
13.3777
],
"address": {
"text": "Ebertstraße 22<br/>Mitte, 10117 Berlin<br/>Germany",
"house": "22",
"street": "Ebertstraße",
"postalCode": "10117",
"district": "Mitte",
"city": "Berlin",
"county": "Berlin",
"stateCode": "Berlin",
"country": "Germany",
"countryCode": "DEU"
}
},
"type": "urn:nlp-types:place",
"href": "https://places.ls.hereapi.com/places/v1/places/loc-dmVyc2lvbj0xO3RpdGxlPUViZXJ0c3RyYSVDMyU5RmUrMjI7bGF0PTUyLjUxNTk7bG9uPTEzLjM3Nzc7c3RyZWV0PUViZXJ0c3RyYSVDMyU5RmU7aG91c2U9MjI7Y2l0eT1CZXJsaW47cG9zdGFsQ29kZT0xMDExNztjb3VudHJ5PURFVTtkaXN0cmljdD1NaXR0ZTtzdGF0ZUNvZGU9QmVybGluO2NvdW50eT1CZXJsaW47Y2F0ZWdvcnlJZD1idWlsZGluZztzb3VyY2VTeXN0ZW09aW50ZXJuYWw;context=c2VhcmNoQ29udGV4dD0x?app_id=LKO34glU2MBEVbcOD5mQ&app_code=A2ta_nQ8HRYwenju5HFG5Q"
}
}
}
Does HERE not provide the averageRatings of restaurants or sights-museums?
From the docs it appears if the place hasn't received a rating it'll be 0
Note: If the place has no ratings (yet), both the average and the count values are zero. But if the place cannot be rated (i.e. a street), the whole rating object is not present.
https://developer.here.com/documentation/places/dev_guide/topics/object-rating.html

Get the complete graph information (properties,id etc ) in hierarchical way under a particular node in gremlin

I am able to get all the child nodes and its related information under a parent node but in a array of objects format , for the query
g.V(4128).repeat(out()).emit()
[
{
"id": 4152.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "1l3-37c-1l1",
"value": "XYZ"
}
],
"description": [
{
"id": "1zb-37c-7wl",
"value": "XYZ World"
}
],
"shortName": [
{
"id": "16v-37c-745",
"value": "XYZ"
}
]
}
},
{
"id": 4176.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "1l6-380-1l1",
"value": "XYZ-XYZW"
}
],
"description": [
{
"id": "1ze-380-7wl",
"value": "XYZ West Campus"
}
],
"shortName": [
{
"id": "16y-380-745",
"value": "XYZW"
}
]
}
},
{
"id": 8344.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "3kj-6fs-1l1",
"value": "XYZ-XYZE"
}
],
"description": [
{
"id": "3yr-6fs-7wl",
"value": "XYZ East Campus"
}
],
"shortName": [
{
"id": "36b-6fs-745",
"value": "XYZE"
}
]
}
},
{
"id": 4104.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "1kx-360-1l1",
"value": "XYZ-XYZW-P1"
}
],
"description": [
{
"id": "1z5-360-7wl",
"value": "XYZ West Campus-Phase-1"
}
],
"shortName": [
{
"id": "16p-360-745",
"value": "P1"
}
]
}
},
{
"id": 4296.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "1ll-3bc-1l1",
"value": "XYZ-XYZW-P3"
}
],
"description": [
{
"id": "1zt-3bc-7wl",
"value": "XYZ West Campus-Phase-3"
}
],
"shortName": [
{
"id": "17d-3bc-745",
"value": "P3"
}
]
}
},
{
"id": 8200.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "3k1-6bs-1l1",
"value": "XYZ-XYZW-P2"
}
],
"description": [
{
"id": "3y9-6bs-7wl",
"value": "XYZ West Campus-Phase-2"
}
],
"shortName": [
{
"id": "35t-6bs-745",
"value": "P2"
}
]
}
},
{
"id": 8224.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "3yc-6cg-1l1",
"value": "XYZ-XYZE-P1"
}
],
"description": [
{
"id": "4ck-6cg-7wl",
"value": "XYZ East Campus-Phase-1"
}
],
"shortName": [
{
"id": "3k4-6cg-745",
"value": "P1"
}
]
}
},
{
"id": 8392.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "3kp-6h4-1l1",
"value": "XYZ-XYZE-P2"
}
],
"description": [
{
"id": "3yx-6h4-7wl",
"value": "XYZ East Campus-Phase-2"
}
],
"shortName": [
{
"id": "36h-6h4-745",
"value": "P2"
}
]
}
}
]
I need to get the same information in hierarchial way .
I tried the following query
g.V(4128).repeat(out()).emit().tree().by(__.valueMap(true))
but I don't get the valid json.
I need to get the above data but in hierarchical way
Updated
I tried the following query
g.V(4128).repeat(out()).emit().tree().by(__.valueMap(true))
And the response I received was
[
{
"{label = org, name = [XYZ], orgId = [00000000-0000-0000-0000-000000000001], desc = [XYZ Organization], id = 4128}": {
"{label = location, displayName = [hh], description = [hfds], shortName = [kk], id = 8272}": {
},
"{label = location, displayName = [XYZ], description = [XYZ World], shortName = [XYZ], id = 4152}": {
"{label = location, displayName = [XYZ-XYZW], description = [XYZ West Campus], shortName = [XYZW], id = 4176}": {
"{label = location, displayName = [XYZ-XYZW-P3], description = [XYZ West Campus-Phase-3], shortName = [P3], id = 4296}": {
},
"{label = location, displayName = [XYZ-XYZW-P2], description = [XYZ West Campus-Phase-2], shortName = [P2], id = 8200}": {
},
"{label = location, displayName = [XYZ-XYZW-P1], description = [XYZ West Campus-Phase-1], shortName = [P1], id = 4104}": {
}
},
"{label = location, displayName = [XYZ-XYZE], description = [XYZ East Campus], shortName = [XYZE], id = 8344}": {
"{label = location, displayName = [XYZ-XYZE-P1], description = [XYZ East Campus-Phase-1], shortName = [P1], id = 8224}": {
},
"{label = location, displayName = [XYZ-XYZE-P2], description = [XYZ East Campus-Phase-2], shortName = [P2], id = 8392}": {
}
}
},
"{label = location, displayName = [hh], description = [hfds], shortName = [kk], id = 8248}": {
},
"{label = location, displayName = [hh], description = [hfds], shortName = [kk], id = 12488}": {
}
}
}
]
Now as you can see the json so received is not appropriate because the key contains fields that are '=' separated .
Now as for the answer I also tried the folowing query
g.V(4128).repeat(out()).emit().tree().next()
but the response returned only ids and didnt resolve the vertex properties
[
{
"4128": {
"8272": {},
"4152": {
"4176": {
"4104": {},
"4296": {},
"8200": {}
},
"8344": {
"8224": {},
"8392": {}
}
},
"8248": {},
"12488": {}
}
}
]
For the context I am using the following configurations in my gremlin-server.yaml file
authentication: {className: org.apache.tinkerpop.gremlin.server.auth.AllowAllAuthenticator,
config: null}
channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer
graphs: {graph: 'D:/graph-data-access/src/test/resources/titan-inmemory.properties'}
gremlinPool: 8
host: localhost
maxAccumulationBufferComponents: 1024
maxChunkSize: 8192
maxContentLength: 65536
maxHeaderSize: 8192
maxInitialLineLength: 4096
metrics:
consoleReporter: null
csvReporter: null
gangliaReporter: null
graphiteReporter: null
jmxReporter: null
slf4jReporter: {enabled: true, interval: 180000, loggerName: org.apache.tinkerpop.gremlin.server.Settings$Slf4jReporterMetrics}
plugins: [aurelius.titan, tinkerpop.gephi]
port: 8182
processors: []
resultIterationBatchSize: 64
scriptEngines:
gremlin-groovy:
config: null
imports: [java.lang.Math]
scripts: ['D:/graph-data-access/src/test/resources/generate-locations.groovy']
staticImports: [java.lang.Math.PI]
scriptEvaluationTimeout: 30000
serializedResponseTimeout: 30000
serializers:
- className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0
config: {useMapperFromGraph: graph}
- className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0
config: {serializeResultToString: true}
- className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0
config: {useMapperFromGraph: graph}
- className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0
config: {useMapperFromGraph: graph}
ssl: {enabled: false, keyCertChainFile: null, keyFile: null, keyPassword: null, trustCertChainFile: null}
threadPoolBoss: 1
threadPoolWorker: 1
writeBufferHighWaterMark: 65536
writeBufferLowWaterMark: 32768
I am using Tinkerpop version 3.0.1 incubating as I am trying to use Titan 1.0.0 with DynamoDb as storage backend for the remote connection.
Not sure what you mean by "but i don't get valid json". You should be able to serialized a Tree in GraphSON 1.0 if you don't include by(valueMap(true)) and just serialize the full vertex. There is a failure if you try to do it as just maps as it makes some assumptions about there being a graph Element as the key (which is probably bad).
gremlin> mapper = graph.io(graphson()).mapper().version(GraphSONVersion.V1_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper#6b2e0f78
gremlin> mapper.writeValueAsString(g.V(1).repeat(out()).emit().tree().next())
==>{"1":{"key":{"id":1,"label":"vertex","type":"vertex","properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":2,"value":29}]}},"value":{"2":{"key":{"id":2,"label":"vertex","type":"vertex","properties":{"name":[{"id":3,"value":"vadas"}],"age":[{"id":4,"value":27}]}},"value":{}},"3":{"key":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"value":{}},"4":{"key":{"id":4,"label":"vertex","type":"vertex","properties":{"name":[{"id":7,"value":"josh"}],"age":[{"id":8,"value":32}]}},"value":{"3":{"key":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"value":{}},"5":{"key":{"id":5,"label":"vertex","type":"vertex","properties":{"name":[{"id":9,"value":"ripple"}],"lang":[{"id":10,"value":"java"}]}},"value":{}}}}}}}
That formats to:
{
"1": {
"key": {
"id": 1,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 0,
"value": "marko"
}],
"age": [{
"id": 2,
"value": 29
}]
}
},
"value": {
"2": {
"key": {
"id": 2,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 3,
"value": "vadas"
}],
"age": [{
"id": 4,
"value": 27
}]
}
},
"value": {}
},
"3": {
"key": {
"id": 3,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 5,
"value": "lop"
}],
"lang": [{
"id": 6,
"value": "java"
}]
}
},
"value": {}
},
"4": {
"key": {
"id": 4,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 7,
"value": "josh"
}],
"age": [{
"id": 8,
"value": 32
}]
}
},
"value": {
"3": {
"key": {
"id": 3,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 5,
"value": "lop"
}],
"lang": [{
"id": 6,
"value": "java"
}]
}
},
"value": {}
},
"5": {
"key": {
"id": 5,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 9,
"value": "ripple"
}],
"lang": [{
"id": 10,
"value": "java"
}]
}
},
"value": {}
}
}
}
}
}
}
In GraphSON 2.0 you get this:
gremlin> mapper = graph.io(graphson()).mapper().version(GraphSONVersion.V2_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper#3ffb3598
gremlin> mapper.writeValueAsString(g.V(1).repeat(out()).emit().tree().next())
==>{"#type":"g:Tree","#value":[{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":1},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":0},"value":"marko","label":"name"}}],"age":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":2},"value":{"#type":"g:Int32","#value":29},"label":"age"}}]}}},"value":{"#type":"g:Tree","#value":[{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":2},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":3},"value":"vadas","label":"name"}}],"age":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":4},"value":{"#type":"g:Int32","#value":27},"label":"age"}}]}}},"value":{"#type":"g:Tree","#value":[]}},{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":3},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":5},"value":"lop","label":"name"}}],"lang":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":6},"value":"java","label":"lang"}}]}}},"value":{"#type":"g:Tree","#value":[]}},{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":4},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":7},"value":"josh","label":"name"}}],"age":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":8},"value":{"#type":"g:Int32","#value":32},"label":"age"}}]}}},"value":{"#type":"g:Tree","#value":[{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":3},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":5},"value":"lop","label":"name"}}],"lang":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":6},"value":"java","label":"lang"}}]}}},"value":{"#type":"g:Tree","#value":[]}},{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":5},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":9},"value":"ripple","label":"name"}}],"lang":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":10},"value":"java","label":"lang"}}]}}},"value":{"#type":"g:Tree","#value":[]}}]}}]}}]}
which formats as:
{
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["marko"],
"id": {
"#type": "g:Int32",
"#value": 1
},
"age": [{
"#type": "g:Int32",
"#value": 29
}]
},
"value": {
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["lop"],
"id": {
"#type": "g:Int32",
"#value": 3
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["vadas"],
"id": {
"#type": "g:Int32",
"#value": 2
},
"age": [{
"#type": "g:Int32",
"#value": 27
}]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["josh"],
"id": {
"#type": "g:Int32",
"#value": 4
},
"age": [{
"#type": "g:Int32",
"#value": 32
}]
},
"value": {
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["ripple"],
"id": {
"#type": "g:Int32",
"#value": 5
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["lop"],
"id": {
"#type": "g:Int32",
"#value": 3
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}]
}
}]
}
}]
}
Note that you can also use by(valueMap(true)) in the case of GraphSON 2.0:
gremlin> mapper.writeValueAsString(g.V(1).repeat(out()).emit().tree().by(valueMap(true)).next())
==>{"#type":"g:Tree","#value":[{"key":{"label":"vertex","name":["marko"],"id":{"#type":"g:Int32","#value":1},"age":[{"#type":"g:Int32","#value":29}]},"value":{"#type":"g:Tree","#value":[{"key":{"label":"vertex","name":["lop"],"id":{"#type":"g:Int32","#value":3},"lang":["java"]},"value":{"#type":"g:Tree","#value":[]}},{"key":{"label":"vertex","name":["vadas"],"id":{"#type":"g:Int32","#value":2},"age":[{"#type":"g:Int32","#value":27}]},"value":{"#type":"g:Tree","#value":[]}},{"key":{"label":"vertex","name":["josh"],"id":{"#type":"g:Int32","#value":4},"age":[{"#type":"g:Int32","#value":32}]},"value":{"#type":"g:Tree","#value":[{"key":{"label":"vertex","name":["ripple"],"id":{"#type":"g:Int32","#value":5},"lang":["java"]},"value":{"#type":"g:Tree","#value":[]}},{"key":{"label":"vertex","name":["lop"],"id":{"#type":"g:Int32","#value":3},"lang":["java"]},"value":{"#type":"g:Tree","#value":[]}}]}}]}}]}
which formats to:
{
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["marko"],
"id": {
"#type": "g:Int32",
"#value": 1
},
"age": [{
"#type": "g:Int32",
"#value": 29
}]
},
"value": {
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["lop"],
"id": {
"#type": "g:Int32",
"#value": 3
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["vadas"],
"id": {
"#type": "g:Int32",
"#value": 2
},
"age": [{
"#type": "g:Int32",
"#value": 27
}]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["josh"],
"id": {
"#type": "g:Int32",
"#value": 4
},
"age": [{
"#type": "g:Int32",
"#value": 32
}]
},
"value": {
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["ripple"],
"id": {
"#type": "g:Int32",
"#value": 5
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["lop"],
"id": {
"#type": "g:Int32",
"#value": 3
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}]
}
}]
}
}]
}
If you are on older version of TinkerPop going back to 3.0.x, then there are some limitations with tree that aren't easily worked around. The only approach is to take a raw tree and post-process it to get it to a form that is friendly to JSON. Here is one way to do that:
gremlin> convert = { it.collectEntries{ k,v -> [(k.id()): [k, v.isEmpty() ? v : convert(v)]] }}
==>groovysh_evaluate$_run_closure1#255e5e2e
gremlin> t = g.V(1).repeat(out()).emit().tree().next()
==>v[1]={v[2]={}, v[3]={}, v[4]={v[3]={}, v[5]={}}}
gremlin> convert(t)
==>1=[v[1], {2=[v[2], {}], 3=[v[3], {}], 4=[v[4], {3=[v[3], {}], 5=[v[5], {}]}]}]
gremlin> mapper.writeValueAsString(convert(t))
==>{"1":[{"id":1,"label":"vertex","type":"vertex","properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":2,"value":29}]}},{"2":[{"id":2,"label":"vertex","type":"vertex","properties":{"name":[{"id":3,"value":"vadas"}],"age":[{"id":4,"value":27}]}},{}],"3":[{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},{}],"4":[{"id":4,"label":"vertex","type":"vertex","properties":{"name":[{"id":7,"value":"josh"}],"age":[{"id":8,"value":32}]}},{"3":[{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},{}],"5":[{"id":5,"label":"vertex","type":"vertex","properties":{"name":[{"id":9,"value":"ripple"}],"lang":[{"id":10,"value":"java"}]}},{}]}]}]}
This formats in GraphSON 1.0 (which is what is used in 3.0.x) to:
{
"1": [{
"id": 1,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 0,
"value": "marko"
}],
"age": [{
"id": 2,
"value": 29
}]
}
}, {
"2": [{
"id": 2,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 3,
"value": "vadas"
}],
"age": [{
"id": 4,
"value": 27
}]
}
}, {}],
"3": [{
"id": 3,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 5,
"value": "lop"
}],
"lang": [{
"id": 6,
"value": "java"
}]
}
}, {}],
"4": [{
"id": 4,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 7,
"value": "josh"
}],
"age": [{
"id": 8,
"value": 32
}]
}
}, {
"3": [{
"id": 3,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 5,
"value": "lop"
}],
"lang": [{
"id": 6,
"value": "java"
}]
}
}, {}],
"5": [{
"id": 5,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 9,
"value": "ripple"
}],
"lang": [{
"id": 10,
"value": "java"
}]
}
}, {}]
}]
}]
}
The convert() function i created just recursively iterates the tree and turns every [vertex: children] into [vertexId: [vertex,children]. You can convert to whatever format you like, so long as you build valid JSON where there is a usable String key field. I could have just as easily made the convert() function return [vertexId: [node: vertex, leaves: children]:
gremlin> convert = { it.collectEntries{ k,v -> [(k.id()): [node: k, leaves:v.isEmpty() ? v : convert(v)]] }}
==>groovysh_evaluate$_run_closure1#2ab5afc7
gremlin> mapper.writeValueAsString(convert(t))
==>{"1":{"node":{"id":1,"label":"vertex","type":"vertex","properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":2,"value":29}]}},"leaves":{"2":{"node":{"id":2,"label":"vertex","type":"vertex","properties":{"name":[{"id":3,"value":"vadas"}],"age":[{"id":4,"value":27}]}},"leaves":{}},"3":{"node":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"leaves":{}},"4":{"node":{"id":4,"label":"vertex","type":"vertex","properties":{"name":[{"id":7,"value":"josh"}],"age":[{"id":8,"value":32}]}},"leaves":{"3":{"node":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"leaves":{}},"5":{"node":{"id":5,"label":"vertex","type":"vertex","properties":{"name":[{"id":9,"value":"ripple"}],"lang":[{"id":10,"value":"java"}]}},"leaves":{}}}}}}}

Resources