21.19. Batch operations

Note: You cannot use this resource to execute Cypher queries with USING PERIODIC COMMIT.

Execute multiple operations in batch

This lets you execute multiple API calls through a single HTTP call, significantly improving performance for large insert and update operations.

The batch service expects an array of job descriptions as input, each job description describing an action to be performed via the normal server API.

This service is transactional. If any of the operations performed fails (returns a non-2xx HTTP status code), the transaction will be rolled back and all changes will be undone.

Each job description should contain a to attribute, with a value relative to the data API root (so http://localhost:7474/db/data/node becomes just /node), and a method attribute containing HTTP verb to use.

Optionally you may provide a body attribute, and an id attribute to help you keep track of responses, although responses are guaranteed to be returned in the same order the job descriptions are received.

The following figure outlines the different parts of the job descriptions:

Figure 21.71. Final Graph

Example request

  • POST http://localhost:7474/db/data/batch
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
[ {
  "method" : "PUT",
  "to" : "/node/409/properties",
  "body" : {
    "age" : 1
  },
  "id" : 0
}, {
  "method" : "GET",
  "to" : "/node/409",
  "id" : 1
}, {
  "method" : "POST",
  "to" : "/node",
  "body" : {
    "age" : 1
  },
  "id" : 2
}, {
  "method" : "POST",
  "to" : "/node",
  "body" : {
    "age" : 1
  },
  "id" : 3
} ]

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "id" : 0,
  "from" : "/node/409/properties"
}, {
  "id" : 1,
  "body" : {
    "extensions" : { },
    "labels" : "http://localhost:7474/db/data/node/409/labels",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/409/relationships/out",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/409/relationships/all/{-list|&|types}",
    "traverse" : "http://localhost:7474/db/data/node/409/traverse/{returnType}",
    "self" : "http://localhost:7474/db/data/node/409",
    "property" : "http://localhost:7474/db/data/node/409/properties/{key}",
    "properties" : "http://localhost:7474/db/data/node/409/properties",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/409/relationships/out/{-list|&|types}",
    "incoming_relationships" : "http://localhost:7474/db/data/node/409/relationships/in",
    "create_relationship" : "http://localhost:7474/db/data/node/409/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/409/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/409/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/409/relationships/in/{-list|&|types}",
    "metadata" : {
      "id" : 409,
      "labels" : [ ]
    },
    "data" : {
      "age" : 1
    }
  },
  "from" : "/node/409"
}, {
  "id" : 2,
  "location" : "http://localhost:7474/db/data/node/410",
  "body" : {
    "extensions" : { },
    "labels" : "http://localhost:7474/db/data/node/410/labels",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/410/relationships/out",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/410/relationships/all/{-list|&|types}",
    "traverse" : "http://localhost:7474/db/data/node/410/traverse/{returnType}",
    "self" : "http://localhost:7474/db/data/node/410",
    "property" : "http://localhost:7474/db/data/node/410/properties/{key}",
    "properties" : "http://localhost:7474/db/data/node/410/properties",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/410/relationships/out/{-list|&|types}",
    "incoming_relationships" : "http://localhost:7474/db/data/node/410/relationships/in",
    "create_relationship" : "http://localhost:7474/db/data/node/410/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/410/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/410/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/410/relationships/in/{-list|&|types}",
    "metadata" : {
      "id" : 410,
      "labels" : [ ]
    },
    "data" : {
      "age" : 1
    }
  },
  "from" : "/node"
}, {
  "id" : 3,
  "location" : "http://localhost:7474/db/data/node/411",
  "body" : {
    "extensions" : { },
    "labels" : "http://localhost:7474/db/data/node/411/labels",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/411/relationships/out",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/411/relationships/all/{-list|&|types}",
    "traverse" : "http://localhost:7474/db/data/node/411/traverse/{returnType}",
    "self" : "http://localhost:7474/db/data/node/411",
    "property" : "http://localhost:7474/db/data/node/411/properties/{key}",
    "properties" : "http://localhost:7474/db/data/node/411/properties",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/411/relationships/out/{-list|&|types}",
    "incoming_relationships" : "http://localhost:7474/db/data/node/411/relationships/in",
    "create_relationship" : "http://localhost:7474/db/data/node/411/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/411/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/411/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/411/relationships/in/{-list|&|types}",
    "metadata" : {
      "id" : 411,
      "labels" : [ ]
    },
    "data" : {
      "age" : 1
    }
  },
  "from" : "/node"
} ]

Refer to items created earlier in the same batch job

The batch operation API allows you to refer to the URI returned from a created resource in subsequent job descriptions, within the same batch call.

Use the {[JOB ID]} special syntax to inject URIs from created resources into JSON strings in subsequent job descriptions.

Figure 21.72. Final Graph

Example request

  • POST http://localhost:7474/db/data/batch
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
[ {
  "method" : "POST",
  "to" : "/node",
  "id" : 0,
  "body" : {
    "name" : "bob"
  }
}, {
  "method" : "POST",
  "to" : "/node",
  "id" : 1,
  "body" : {
    "age" : 12
  }
}, {
  "method" : "POST",
  "to" : "{0}/relationships",
  "id" : 3,
  "body" : {
    "to" : "{1}",
    "data" : {
      "since" : "2010"
    },
    "type" : "KNOWS"
  }
}, {
  "method" : "POST",
  "to" : "/index/relationship/my_rels",
  "id" : 4,
  "body" : {
    "key" : "since",
    "value" : "2010",
    "uri" : "{3}"
  }
} ]

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "id" : 0,
  "location" : "http://localhost:7474/db/data/node/401",
  "body" : {
    "extensions" : { },
    "labels" : "http://localhost:7474/db/data/node/401/labels",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/401/relationships/out",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/401/relationships/all/{-list|&|types}",
    "traverse" : "http://localhost:7474/db/data/node/401/traverse/{returnType}",
    "self" : "http://localhost:7474/db/data/node/401",
    "property" : "http://localhost:7474/db/data/node/401/properties/{key}",
    "properties" : "http://localhost:7474/db/data/node/401/properties",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/401/relationships/out/{-list|&|types}",
    "incoming_relationships" : "http://localhost:7474/db/data/node/401/relationships/in",
    "create_relationship" : "http://localhost:7474/db/data/node/401/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/401/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/401/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/401/relationships/in/{-list|&|types}",
    "metadata" : {
      "id" : 401,
      "labels" : [ ]
    },
    "data" : {
      "name" : "bob"
    }
  },
  "from" : "/node"
}, {
  "id" : 1,
  "location" : "http://localhost:7474/db/data/node/402",
  "body" : {
    "extensions" : { },
    "labels" : "http://localhost:7474/db/data/node/402/labels",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/402/relationships/out",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/402/relationships/all/{-list|&|types}",
    "traverse" : "http://localhost:7474/db/data/node/402/traverse/{returnType}",
    "self" : "http://localhost:7474/db/data/node/402",
    "property" : "http://localhost:7474/db/data/node/402/properties/{key}",
    "properties" : "http://localhost:7474/db/data/node/402/properties",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/402/relationships/out/{-list|&|types}",
    "incoming_relationships" : "http://localhost:7474/db/data/node/402/relationships/in",
    "create_relationship" : "http://localhost:7474/db/data/node/402/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/402/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/402/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/402/relationships/in/{-list|&|types}",
    "metadata" : {
      "id" : 402,
      "labels" : [ ]
    },
    "data" : {
      "age" : 12
    }
  },
  "from" : "/node"
}, {
  "id" : 3,
  "location" : "http://localhost:7474/db/data/relationship/239",
  "body" : {
    "extensions" : { },
    "start" : "http://localhost:7474/db/data/node/401",
    "property" : "http://localhost:7474/db/data/relationship/239/properties/{key}",
    "self" : "http://localhost:7474/db/data/relationship/239",
    "properties" : "http://localhost:7474/db/data/relationship/239/properties",
    "type" : "KNOWS",
    "end" : "http://localhost:7474/db/data/node/402",
    "metadata" : {
      "id" : 239,
      "type" : "KNOWS"
    },
    "data" : {
      "since" : "2010"
    }
  },
  "from" : "http://localhost:7474/db/data/node/401/relationships"
}, {
  "id" : 4,
  "location" : "http://localhost:7474/db/data/index/relationship/my_rels/since/2010/239",
  "body" : {
    "extensions" : { },
    "start" : "http://localhost:7474/db/data/node/401",
    "property" : "http://localhost:7474/db/data/relationship/239/properties/{key}",
    "self" : "http://localhost:7474/db/data/relationship/239",
    "properties" : "http://localhost:7474/db/data/relationship/239/properties",
    "type" : "KNOWS",
    "end" : "http://localhost:7474/db/data/node/402",
    "metadata" : {
      "id" : 239,
      "type" : "KNOWS"
    },
    "data" : {
      "since" : "2010"
    },
    "indexed" : "http://localhost:7474/db/data/index/relationship/my_rels/since/2010/239"
  },
  "from" : "/index/relationship/my_rels"
} ]

Execute multiple operations in batch streaming

Figure 21.73. Final Graph

Example request

  • POST http://localhost:7474/db/data/batch
  • Accept: application/json
  • Content-Type: application/json
  • X-Stream: true
[ {
  "method" : "PUT",
  "to" : "/node/115/properties",
  "body" : {
    "age" : 1
  },
  "id" : 0
}, {
  "method" : "GET",
  "to" : "/node/115",
  "id" : 1
}, {
  "method" : "POST",
  "to" : "/node",
  "body" : {
    "age" : 1
  },
  "id" : 2
}, {
  "method" : "POST",
  "to" : "/node",
  "body" : {
    "age" : 1
  },
  "id" : 3
} ]

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "id" : 0,
  "from" : "/node/115/properties",
  "body" : null,
  "status" : 204
}, {
  "id" : 1,
  "from" : "/node/115",
  "body" : {
    "extensions" : { },
    "labels" : "http://localhost:7474/db/data/node/115/labels",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/115/relationships/out",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/115/relationships/all/{-list|&|types}",
    "traverse" : "http://localhost:7474/db/data/node/115/traverse/{returnType}",
    "self" : "http://localhost:7474/db/data/node/115",
    "property" : "http://localhost:7474/db/data/node/115/properties/{key}",
    "properties" : "http://localhost:7474/db/data/node/115/properties",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/115/relationships/out/{-list|&|types}",
    "incoming_relationships" : "http://localhost:7474/db/data/node/115/relationships/in",
    "create_relationship" : "http://localhost:7474/db/data/node/115/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/115/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/115/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/115/relationships/in/{-list|&|types}",
    "metadata" : {
      "id" : 115,
      "labels" : [ ]
    },
    "data" : {
      "age" : 1
    }
  },
  "status" : 200
}, {
  "id" : 2,
  "from" : "/node",
  "body" : {
    "extensions" : { },
    "labels" : "http://localhost:7474/db/data/node/116/labels",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/116/relationships/out",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/116/relationships/all/{-list|&|types}",
    "traverse" : "http://localhost:7474/db/data/node/116/traverse/{returnType}",
    "self" : "http://localhost:7474/db/data/node/116",
    "property" : "http://localhost:7474/db/data/node/116/properties/{key}",
    "properties" : "http://localhost:7474/db/data/node/116/properties",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/116/relationships/out/{-list|&|types}",
    "incoming_relationships" : "http://localhost:7474/db/data/node/116/relationships/in",
    "create_relationship" : "http://localhost:7474/db/data/node/116/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/116/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/116/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/116/relationships/in/{-list|&|types}",
    "metadata" : {
      "id" : 116,
      "labels" : [ ]
    },
    "data" : {
      "age" : 1
    }
  },
  "location" : "http://localhost:7474/db/data/node/116",
  "status" : 201
}, {
  "id" : 3,
  "from" : "/node",
  "body" : {
    "extensions" : { },
    "labels" : "http://localhost:7474/db/data/node/117/labels",
    "outgoing_relationships" : "http://localhost:7474/db/data/node/117/relationships/out",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/117/relationships/all/{-list|&|types}",
    "traverse" : "http://localhost:7474/db/data/node/117/traverse/{returnType}",
    "self" : "http://localhost:7474/db/data/node/117",
    "property" : "http://localhost:7474/db/data/node/117/properties/{key}",
    "properties" : "http://localhost:7474/db/data/node/117/properties",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/117/relationships/out/{-list|&|types}",
    "incoming_relationships" : "http://localhost:7474/db/data/node/117/relationships/in",
    "create_relationship" : "http://localhost:7474/db/data/node/117/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/117/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/117/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/117/relationships/in/{-list|&|types}",
    "metadata" : {
      "id" : 117,
      "labels" : [ ]
    },
    "data" : {
      "age" : 1
    }
  },
  "location" : "http://localhost:7474/db/data/node/117",
  "status" : 201
} ]