Prev |  | Next

## 18.15. Batch operations

| Caution
Batch support is currently experimental. Expect this part of the API to change.

### 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/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"
    }, {
      "id" : 1,
      "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}",
        "all_relationships" : "http://localhost:7474/db/data/node/46/relationships/all",
        "property" : "http://localhost:7474/db/data/node/46/properties/{key}",
        "self" : "http://localhost:7474/db/data/node/46",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/46/relationships/out/{-list|&|types}",
        "properties" : "http://localhost:7474/db/data/node/46/properties",
        "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
        }
      },
      "from" : "/node/46"
    }, {
      "id" : 2,
      "location" : "http://localhost:7474/db/data/node/47",
      "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}",
        "all_relationships" : "http://localhost:7474/db/data/node/47/relationships/all",
        "property" : "http://localhost:7474/db/data/node/47/properties/{key}",
        "self" : "http://localhost:7474/db/data/node/47",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/47/relationships/out/{-list|&|types}",
        "properties" : "http://localhost:7474/db/data/node/47/properties",
        "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
        }
      },
      "from" : "/node"
    }, {
      "id" : 3,
      "location" : "http://localhost:7474/db/data/node/48",
      "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}",
        "all_relationships" : "http://localhost:7474/db/data/node/48/relationships/all",
        "property" : "http://localhost:7474/db/data/node/48/properties/{key}",
        "self" : "http://localhost:7474/db/data/node/48",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/48/relationships/out/{-list|&|types}",
        "properties" : "http://localhost:7474/db/data/node/48/properties",
        "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
        }
      },
      "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/49",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/49/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/49/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/49/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/49/relationships/all/{-list|&|types}",
        "all_relationships" : "http://localhost:7474/db/data/node/49/relationships/all",
        "property" : "http://localhost:7474/db/data/node/49/properties/{key}",
        "self" : "http://localhost:7474/db/data/node/49",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/49/relationships/out/{-list|&|types}",
        "properties" : "http://localhost:7474/db/data/node/49/properties",
        "incoming_relationships" : "http://localhost:7474/db/data/node/49/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/49/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/49/relationships",
        "data" : {
          "name" : "bob"
        }
      },
      "from" : "/node"
    }, {
      "id" : 1,
      "location" : "http://localhost:7474/db/data/node/50",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/50/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/50/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/50/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/50/relationships/all/{-list|&|types}",
        "all_relationships" : "http://localhost:7474/db/data/node/50/relationships/all",
        "property" : "http://localhost:7474/db/data/node/50/properties/{key}",
        "self" : "http://localhost:7474/db/data/node/50",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/50/relationships/out/{-list|&|types}",
        "properties" : "http://localhost:7474/db/data/node/50/properties",
        "incoming_relationships" : "http://localhost:7474/db/data/node/50/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/50/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/50/relationships",
        "data" : {
          "age" : 12
        }
      },
      "from" : "/node"
    }, {
      "id" : 3,
      "location" : "http://localhost:7474/db/data/relationship/23",
      "body" : {
        "extensions" : {
        },
        "start" : "http://localhost:7474/db/data/node/49",
        "property" : "http://localhost:7474/db/data/relationship/23/properties/{key}",
        "self" : "http://localhost:7474/db/data/relationship/23",
        "properties" : "http://localhost:7474/db/data/relationship/23/properties",
        "type" : "KNOWS",
        "end" : "http://localhost:7474/db/data/node/50",
        "data" : {
          "since" : "2010"
        }
      },
      "from" : "http://localhost:7474/db/data/node/49/relationships"
    }, {
      "id" : 4,
      "location" : "http://localhost:7474/db/data/index/relationship/my_rels/since/2010/23",
      "body" : {
        "extensions" : {
        },
        "start" : "http://localhost:7474/db/data/node/49",
        "property" : "http://localhost:7474/db/data/relationship/23/properties/{key}",
        "self" : "http://localhost:7474/db/data/relationship/23",
        "properties" : "http://localhost:7474/db/data/relationship/23/properties",
        "type" : "KNOWS",
        "end" : "http://localhost:7474/db/data/node/50",
        "data" : {
          "since" : "2010"
        },
        "indexed" : "http://localhost:7474/db/data/index/relationship/my_rels/since/2010/23"
      },
      "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/319/properties",
      "body" : {
        "age" : 1
      },
      "id" : 0
    }, {
      "method" : "GET",
      "to" : "/node/319",
      "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/319/properties",
      "body" : null,
      "status" : 204
    }, {
      "id" : 1,
      "from" : "/node/319",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/319/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/319/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/319/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/319/relationships/all/{-list|&|types}",
        "all_relationships" : "http://localhost:7474/db/data/node/319/relationships/all",
        "property" : "http://localhost:7474/db/data/node/319/properties/{key}",
        "self" : "http://localhost:7474/db/data/node/319",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/319/relationships/out/{-list|&|types}",
        "properties" : "http://localhost:7474/db/data/node/319/properties",
        "incoming_relationships" : "http://localhost:7474/db/data/node/319/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/319/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/319/relationships",
        "data" : {
          "age" : 1
        }
      },
      "status" : 200
    }, {
      "id" : 2,
      "from" : "/node",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/320/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/320/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/320/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/320/relationships/all/{-list|&|types}",
        "all_relationships" : "http://localhost:7474/db/data/node/320/relationships/all",
        "property" : "http://localhost:7474/db/data/node/320/properties/{key}",
        "self" : "http://localhost:7474/db/data/node/320",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/320/relationships/out/{-list|&|types}",
        "properties" : "http://localhost:7474/db/data/node/320/properties",
        "incoming_relationships" : "http://localhost:7474/db/data/node/320/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/320/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/320/relationships",
        "data" : {
          "age" : 1
        }
      },
      "location" : "http://localhost:7474/db/data/node/320",
      "status" : 201
    }, {
      "id" : 3,
      "from" : "/node",
      "body" : {
        "extensions" : {
        },
        "paged_traverse" : "http://localhost:7474/db/data/node/321/paged/traverse/{returnType}{?pageSize,leaseTime}",
        "outgoing_relationships" : "http://localhost:7474/db/data/node/321/relationships/out",
        "traverse" : "http://localhost:7474/db/data/node/321/traverse/{returnType}",
        "all_typed_relationships" : "http://localhost:7474/db/data/node/321/relationships/all/{-list|&|types}",
        "all_relationships" : "http://localhost:7474/db/data/node/321/relationships/all",
        "property" : "http://localhost:7474/db/data/node/321/properties/{key}",
        "self" : "http://localhost:7474/db/data/node/321",
        "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/321/relationships/out/{-list|&|types}",
        "properties" : "http://localhost:7474/db/data/node/321/properties",
        "incoming_relationships" : "http://localhost:7474/db/data/node/321/relationships/in",
        "incoming_typed_relationships" : "http://localhost:7474/db/data/node/321/relationships/in/{-list|&|types}",
        "create_relationship" : "http://localhost:7474/db/data/node/321/relationships",
        "data" : {
          "age" : 1
        }
      },
      "location" : "http://localhost:7474/db/data/node/321",
      "status" : 201
    } ]

Copyright © 2012 Neo Technology

Prev | Up | Next
| Home |