Skip to content

Instantly share code, notes, and snippets.

@spmallette
Created September 13, 2024 14:52
Show Gist options
  • Save spmallette/21723ca4ca127c9ed52358680a223da7 to your computer and use it in GitHub Desktop.
Save spmallette/21723ca4ca127c9ed52358680a223da7 to your computer and use it in GitHub Desktop.
Streaming GraphSON
Type: LinkedHashMap Value: {id=1, label=person, name=[marko], age=[29]}
Type: LinkedHashMap Value: {id=2, label=person, name=[vadas], age=[27]}
Type: LinkedHashMap Value: {id=3, label=software, name=[lop], lang=[java]}
Type: LinkedHashMap Value: {id=4, label=person, name=[josh], age=[32]}
Type: LinkedHashMap Value: {id=5, label=software, name=[ripple], lang=[java]}
Type: LinkedHashMap Value: {id=6, label=person, name=[peter], age=[35]}
[
{
"@type": "g:Map",
"@value": [
{
"@type": "g:T",
"@value": "id"
},
{
"@type": "g:Int32",
"@value": 1
},
{
"@type": "g:T",
"@value": "label"
},
"person",
"name",
{
"@type": "g:List",
"@value": [
"marko"
]
},
"age",
{
"@type": "g:List",
"@value": [
{
"@type": "g:Int32",
"@value": 29
}
]
}
]
},
{
"@type": "g:Map",
"@value": [
{
"@type": "g:T",
"@value": "id"
},
{
"@type": "g:Int32",
"@value": 2
},
{
"@type": "g:T",
"@value": "label"
},
"person",
"name",
{
"@type": "g:List",
"@value": [
"vadas"
]
},
"age",
{
"@type": "g:List",
"@value": [
{
"@type": "g:Int32",
"@value": 27
}
]
}
]
},
{
"@type": "g:Map",
"@value": [
{
"@type": "g:T",
"@value": "id"
},
{
"@type": "g:Int32",
"@value": 3
},
{
"@type": "g:T",
"@value": "label"
},
"software",
"name",
{
"@type": "g:List",
"@value": [
"lop"
]
},
"lang",
{
"@type": "g:List",
"@value": [
"java"
]
}
]
},
{
"@type": "g:Map",
"@value": [
{
"@type": "g:T",
"@value": "id"
},
{
"@type": "g:Int32",
"@value": 4
},
{
"@type": "g:T",
"@value": "label"
},
"person",
"name",
{
"@type": "g:List",
"@value": [
"josh"
]
},
"age",
{
"@type": "g:List",
"@value": [
{
"@type": "g:Int32",
"@value": 32
}
]
}
]
},
{
"@type": "g:Map",
"@value": [
{
"@type": "g:T",
"@value": "id"
},
{
"@type": "g:Int32",
"@value": 5
},
{
"@type": "g:T",
"@value": "label"
},
"software",
"name",
{
"@type": "g:List",
"@value": [
"ripple"
]
},
"lang",
{
"@type": "g:List",
"@value": [
"java"
]
}
]
},
{
"@type": "g:Map",
"@value": [
{
"@type": "g:T",
"@value": "id"
},
{
"@type": "g:Int32",
"@value": 6
},
{
"@type": "g:T",
"@value": "label"
},
"person",
"name",
{
"@type": "g:List",
"@value": [
"peter"
]
},
"age",
{
"@type": "g:List",
"@value": [
{
"@type": "g:Int32",
"@value": 35
}
]
}
]
}
]
final GraphTraversalSource g = TinkerFactory.createModern().traversal();
// set up streaming to use the GraphSON3 codec
final JsonFactory jsonFactory = new MappingJsonFactory(GraphSONMapper.build().
addRegistry(TinkerIoRegistryV3.instance()).version(GraphSONVersion.V3_0).create().createMapper());
// used a local file but could use a S3 OutputStream
final JsonGenerator jsonGenerator = jsonFactory.createGenerator(new FileOutputStream("query-output.json"));
jsonGenerator.writeStartArray();
g.V().valueMap(true).sideEffect(m -> {
// write out one result at a time to the stream
try {
jsonGenerator.writeObject(m.get());
} catch (IOException ioe) {
ioe.printStackTrace();
}
}).iterate();
jsonGenerator.writeEndArray();
jsonGenerator.close();
// stream it back in from the local file, but again could be S3 InputStream
final JsonParser jsonParser = jsonFactory.createParser(new FileInputStream("query-output.json"));
// first token is query result list start so skip that one
jsonParser.nextToken();
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
// we wrote maps, so tell the streaming parser to decode maps - see console.txt
final Object o = jsonParser.readValueAs(Map.class);
System.out.println(String.format("Type: %s Value: %s", o.getClass().getSimpleName(), o));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment