Prev |  | Next

## 18.15. Batch operations

### 18.15.1. 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 18.77. Final Graph

Example request

- `POST` `http://localhost:7474/db/data/batch`
- `Accept:` `application/json`
- `Content-Type:` `application/json`

    [ {
      "method" : "PUT",
      "to" : "/node/216/properties",
      "body" : {
        "age" : 1
      },
      "id" : 0
    }, {
      "method" : "GET",
      "to" : "/node/216",
      "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`

    [ {
      "id" : 0,
      "from" : "/node/216/properties"
    }, {
      "id" : 1,
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/216/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/216/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/216/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/216/relationships/all/{-list|&|types}",
        "property" : "http://localhost:7474/db/data/node/216/properties/{key}",
        "all_relationships" : "http://localhost:7474/db/data/node/216/relationships/all",
        "self" : "http://localhost:7474/db/data/node/216",
        "properties" : "http://localhost:7474/db/data/node/216/properties",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/216/relationships/out/{-list|&|types}",
        "incoming_relationships" : "http://localhost:7474/db/data/node/216/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/216/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/216/relationships",
        "data" : {
          "age" : 1
        }
      },
      "from" : "/node/216"
    }, {
      "id" : 2,
      "location" : "http://localhost:7474/db/data/node/217",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/217/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/217/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/217/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/217/relationships/all/{-list|&|types}",
        "property" : "http://localhost:7474/db/data/node/217/properties/{key}",
        "all_relationships" : "http://localhost:7474/db/data/node/217/relationships/all",
        "self" : "http://localhost:7474/db/data/node/217",
        "properties" : "http://localhost:7474/db/data/node/217/properties",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/217/relationships/out/{-list|&|types}",
        "incoming_relationships" : "http://localhost:7474/db/data/node/217/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/217/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/217/relationships",
        "data" : {
          "age" : 1
        }
      },
      "from" : "/node"
    }, {
      "id" : 3,
      "location" : "http://localhost:7474/db/data/node/218",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/218/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/218/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/218/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/218/relationships/all/{-list|&|types}",
        "property" : "http://localhost:7474/db/data/node/218/properties/{key}",
        "all_relationships" : "http://localhost:7474/db/data/node/218/relationships/all",
        "self" : "http://localhost:7474/db/data/node/218",
        "properties" : "http://localhost:7474/db/data/node/218/properties",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/218/relationships/out/{-list|&|types}",
        "incoming_relationships" : "http://localhost:7474/db/data/node/218/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/218/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/218/relationships",
        "data" : {
          "age" : 1
        }
      },
      "from" : "/node"
    } ]

### 18.15.2. 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 18.78. Final Graph

Example request

- `POST` `http://localhost:7474/db/data/batch`
- `Accept:` `application/json`
- `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`

    [ {
      "id" : 0,
      "location" : "http://localhost:7474/db/data/node/219",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/219/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/219/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/219/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/219/relationships/all/{-list|&|types}",
        "property" : "http://localhost:7474/db/data/node/219/properties/{key}",
        "all_relationships" : "http://localhost:7474/db/data/node/219/relationships/all",
        "self" : "http://localhost:7474/db/data/node/219",
        "properties" : "http://localhost:7474/db/data/node/219/properties",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/219/relationships/out/{-list|&|types}",
        "incoming_relationships" : "http://localhost:7474/db/data/node/219/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/219/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/219/relationships",
        "data" : {
          "name" : "bob"
        }
      },
      "from" : "/node"
    }, {
      "id" : 1,
      "location" : "http://localhost:7474/db/data/node/220",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/220/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/220/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/220/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/220/relationships/all/{-list|&|types}",
        "property" : "http://localhost:7474/db/data/node/220/properties/{key}",
        "all_relationships" : "http://localhost:7474/db/data/node/220/relationships/all",
        "self" : "http://localhost:7474/db/data/node/220",
        "properties" : "http://localhost:7474/db/data/node/220/properties",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/220/relationships/out/{-list|&|types}",
        "incoming_relationships" : "http://localhost:7474/db/data/node/220/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/220/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/220/relationships",
        "data" : {
          "age" : 12
        }
      },
      "from" : "/node"
    }, {
      "id" : 3,
      "location" : "http://localhost:7474/db/data/relationship/130",
      "body" : {
        "extensions" : {
        },
        "start" : "http://localhost:7474/db/data/node/219",
        "property" : "http://localhost:7474/db/data/relationship/130/properties/{key}",
        "self" : "http://localhost:7474/db/data/relationship/130",
        "properties" : "http://localhost:7474/db/data/relationship/130/properties",
        "type" : "KNOWS",
        "end" : "http://localhost:7474/db/data/node/220",
        "data" : {
          "since" : "2010"
        }
      },
      "from" : "http://localhost:7474/db/data/node/219/relationships"
    }, {
      "id" : 4,
      "location" : "http://localhost:7474/db/data/index/relationship/my_rels/since/2010/130",
      "body" : {
        "extensions" : {
        },
        "start" : "http://localhost:7474/db/data/node/219",
        "property" : "http://localhost:7474/db/data/relationship/130/properties/{key}",
        "self" : "http://localhost:7474/db/data/relationship/130",
        "properties" : "http://localhost:7474/db/data/relationship/130/properties",
        "type" : "KNOWS",
        "end" : "http://localhost:7474/db/data/node/220",
        "data" : {
          "since" : "2010"
        },
        "indexed" : "http://localhost:7474/db/data/index/relationship/my_rels/since/2010/130"
      },
      "from" : "/index/relationship/my_rels"
    } ]

### 18.15.3. Execute multiple operations in batch streaming

Figure 18.79. 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/46/properties",
      "body" : {
        "age" : 1
      },
      "id" : 0
    }, {
      "method" : "GET",
      "to" : "/node/46",
      "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`

    [ {
      "id" : 0,
      "from" : "/node/46/properties",
      "body" : null,
      "status" : 204
    }, {
      "id" : 1,
      "from" : "/node/46",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/46/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/46/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/46/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/46/relationships/all/{-list|&|types}",
        "property" : "http://localhost:7474/db/data/node/46/properties/{key}",
        "all_relationships" : "http://localhost:7474/db/data/node/46/relationships/all",
        "self" : "http://localhost:7474/db/data/node/46",
        "properties" : "http://localhost:7474/db/data/node/46/properties",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/46/relationships/out/{-list|&|types}",
        "incoming_relationships" : "http://localhost:7474/db/data/node/46/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/46/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/46/relationships",
        "data" : {
          "age" : 1
        }
      },
      "status" : 200
    }, {
      "id" : 2,
      "from" : "/node",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/47/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/47/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/47/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/47/relationships/all/{-list|&|types}",
        "property" : "http://localhost:7474/db/data/node/47/properties/{key}",
        "all_relationships" : "http://localhost:7474/db/data/node/47/relationships/all",
        "self" : "http://localhost:7474/db/data/node/47",
        "properties" : "http://localhost:7474/db/data/node/47/properties",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/47/relationships/out/{-list|&|types}",
        "incoming_relationships" : "http://localhost:7474/db/data/node/47/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/47/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/47/relationships",
        "data" : {
          "age" : 1
        }
      },
      "location" : "http://localhost:7474/db/data/node/47",
      "status" : 201
    }, {
      "id" : 3,
      "from" : "/node",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/48/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/48/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/48/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/48/relationships/all/{-list|&|types}",
        "property" : "http://localhost:7474/db/data/node/48/properties/{key}",
        "all_relationships" : "http://localhost:7474/db/data/node/48/relationships/all",
        "self" : "http://localhost:7474/db/data/node/48",
        "properties" : "http://localhost:7474/db/data/node/48/properties",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/48/relationships/out/{-list|&|types}",
        "incoming_relationships" : "http://localhost:7474/db/data/node/48/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/48/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/48/relationships",
        "data" : {
          "age" : 1
        }
      },
      "location" : "http://localhost:7474/db/data/node/48",
      "status" : 201
    } ]

Copyright © 2013 Neo Technology

Prev | Up | Next
| Home |