Templatize Gremlin request parameters
Alibaba Cloud Graph Database (GDB) recommends that you use String-based Gremlin script requests to interact with the GDB server-side. When you send script requests using a software development kit (SDK), you can templatize the parameters in the request to improve query performance.
Parameter templatization uses a Map structure named `bindings`. You can replace variable data parameters in script requests with placeholders. Then, you can add the placeholders and their corresponding data values to the `bindings` map. This method lets you use the same script request with different bindings to perform similar Gremlin access.
The parameter templatization syntax is similar across different language SDKs. The following sections provide key code examples.
FAQ
Internal reserved words
In
property(id, G__id), the unquotedidis the primary key and must satisfy the global uniqueness constraint.When you set parameter bindings, do not use the format
g.addV(label).property('id', id). Both `id` and `label` are internal reserved words. In the statement, the quoted'id'represents a regular property and does not satisfy the uniqueness constraint.
Limit on the number of templatized parameters
GDB supports a maximum of 128 templatized parameters in `bindings`. This limit cannot be changed.
Selecting parameters to templatize
When a script request is used in a fixed scenario, only some data parameters are variable. You can create placeholders only for the variable parts. You can hard-code fixed data directly into the request.
The following example request adds a vertex with the label 'person' and two properties: 'name' and 'age'.
dsl = "g.addV('person').property('name', G___PV1).property('age', G___PV2)" bindings = {"G___PV1":"Jack","G___PV2":29} client.submit(dsl, bindings).all().join()The `dsl` parameter remains unchanged. You can fill the `bindings` map with the retrieved 'name' and 'age' data and submit the request to write the vertex data.
List parameter support
// For list types, such as hasId and within, and for valueMap as shown below, use multiple placeholders. String dsl = "g.V().has(G__key1, within(G__value1)).valueMap(true)"; Map<String, Object> parameters= new HashMap<>(1); List<String> values = new ArrayList<>(Arrays.asList("beijing", "shanghai", "shenzhen")); parameters.put("G__key1", "location"); parameters.put("G__value1", values); List<Result> result = client.submit(dsl, parameters).all().join();Some
Gremlin Stepparameters, such as `ValueMap`, do not support array-type parameters. You can use multiple placeholders to specify each element in the multi-value type.# The valueMap parameter does not support arrays. This causes a parameter error. bindings = {"G___PKs":["name","age"]} dsl = "g.V().hasLabel('person').valueMap(G___PKs)" client.sumbit(dsl, bindings) # Use two placeholders to represent the two parameters. This is correct. bindings = {"G___PK1":"name","G___PK2":"age"} dsl = "g.V().hasLabel('person').valueMap(G___PK1,G___PK2)" client.submit(dsl, bindings).all().join()
Java parameter templatization
String dsl ="g.addV(G___label).property(id,G___id).property('name',G___name)";
Map<String, Object> parameters = new HashMap<>();
parameters.put("G___id","sand131_id_5_99"); // Vertex ID parameter
parameters.put("G___label","person"); // Vertex label parameter
parameters.put("G___name","Jack"); // Value parameter for the 'name' property of the vertex
List<Result> result = client.submit(dsl, parameters).all().join();Python parameter templatization
dsl ="g.addV(G___label).property(id,G___id).property('name',G___name)"
parameters = {}
parameters["G___id"] = "sand131_id_5_99"
parameters["G___label"] = "person"
parameters["G___name"] = "Jack"
results = client.submit(dsl, bindings=parameters).all().result().NET parameter templatization
string dsl ="g.addV(G___label).property(id,G___id).property('name',G___name)";
Dictionary<string, object> parameters = new Dictionary<string, object> {
{"G___id", "sand131_id_5_99"},
{"G___label", "person"},
{"G___name", "Jack"}};
gremlinClient.SubmitAsync<dynamic>(dsl, parameters);Node.js parameter templatization
function addVertex1()
{
return client.submit("g.addV(G___label).property(id,G___id).property('name',G___name)",{
G___id: "sand131_id_5_99",
G___label: "person",
G___name: "Jack"
}).then(data => {
console.log("Add Vertex Result: %s\n", JSON.stringify(data));
});
}Go parameter templatization
dsl := "g.addV(G___label).property(id,G___id).property('name',G___name)"
parameters := make(map[string]interface{})
parameters["G___id"] = "sand131_id_5_99"
parameters["G___label"] = "person"
parameters["G___name"] = "Jack"
results, err := client.SubmitScriptBound(dsl, parameters)