Prev |  | Next

## 18.12. Batch operations

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

### 18.12.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 `path` 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.37. Final Graph

Example request

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

    [
      {
        "method":"PUT",
        "to":"/node/2/properties",
        "body":{
          "age":1
        },
        "id":0
      },
      {
        "method":"GET",
        "to":"/node/2",
        "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/2/properties"},{"id":1,"body":{
      "outgoing_relationships" : "http://localhost:7474/db/data/node/2/relationships/out",
      "data" : {
        "age" : 1
      },
      "traverse" : "http://localhost:7474/db/data/node/2/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/2/relationships/all/{-list|&|types}",
      "property" : "http://localhost:7474/db/data/node/2/properties/{key}",
      "self" : "http://localhost:7474/db/data/node/2",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/2/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/2/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/2/relationships/in",
      "extensions" : {
      },
      "create_relationship" : "http://localhost:7474/db/data/node/2/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/2/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/2/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/2/relationships/in/{-list|&|types}"
    },"from":"/node/2"},{"id":2,"location":"http://localhost:7474/db/data/node/3","body":{
      "outgoing_relationships" : "http://localhost:7474/db/data/node/3/relationships/out",
      "data" : {
        "age" : 1
      },
      "traverse" : "http://localhost:7474/db/data/node/3/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/3/relationships/all/{-list|&|types}",
      "property" : "http://localhost:7474/db/data/node/3/properties/{key}",
      "self" : "http://localhost:7474/db/data/node/3",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/3/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/3/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/3/relationships/in",
      "extensions" : {
      },
      "create_relationship" : "http://localhost:7474/db/data/node/3/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/3/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/3/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/3/relationships/in/{-list|&|types}"
    },"from":"/node"},{"id":3,"location":"http://localhost:7474/db/data/node/4","body":{
      "outgoing_relationships" : "http://localhost:7474/db/data/node/4/relationships/out",
      "data" : {
        "age" : 1
      },
      "traverse" : "http://localhost:7474/db/data/node/4/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/4/relationships/all/{-list|&|types}",
      "property" : "http://localhost:7474/db/data/node/4/properties/{key}",
      "self" : "http://localhost:7474/db/data/node/4",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/4/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/4/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/4/relationships/in",
      "extensions" : {
      },
      "create_relationship" : "http://localhost:7474/db/data/node/4/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/4/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/4/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/4/relationships/in/{-list|&|types}"
    },"from":"/node"}]

### 18.12.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.38. 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/5","body":{
      "outgoing_relationships" : "http://localhost:7474/db/data/node/5/relationships/out",
      "data" : {
        "name" : "bob"
      },
      "traverse" : "http://localhost:7474/db/data/node/5/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/5/relationships/all/{-list|&|types}",
      "property" : "http://localhost:7474/db/data/node/5/properties/{key}",
      "self" : "http://localhost:7474/db/data/node/5",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/5/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/5/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/5/relationships/in",
      "extensions" : {
      },
      "create_relationship" : "http://localhost:7474/db/data/node/5/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/5/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/5/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/5/relationships/in/{-list|&|types}"
    },"from":"/node"},{"id":1,"location":"http://localhost:7474/db/data/node/6","body":{
      "outgoing_relationships" : "http://localhost:7474/db/data/node/6/relationships/out",
      "data" : {
        "age" : 12
      },
      "traverse" : "http://localhost:7474/db/data/node/6/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/6/relationships/all/{-list|&|types}",
      "property" : "http://localhost:7474/db/data/node/6/properties/{key}",
      "self" : "http://localhost:7474/db/data/node/6",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/6/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/6/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/6/relationships/in",
      "extensions" : {
      },
      "create_relationship" : "http://localhost:7474/db/data/node/6/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/6/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/6/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/6/relationships/in/{-list|&|types}"
    },"from":"/node"},{"id":3,"location":"http://localhost:7474/db/data/relationship/1","body":{
      "start" : "http://localhost:7474/db/data/node/5",
      "data" : {
        "since" : "2010"
      },
      "self" : "http://localhost:7474/db/data/relationship/1",
      "property" : "http://localhost:7474/db/data/relationship/1/properties/{key}",
      "properties" : "http://localhost:7474/db/data/relationship/1/properties",
      "type" : "KNOWS",
      "extensions" : {
      },
      "end" : "http://localhost:7474/db/data/node/6"
    },"from":"http://localhost:7474/db/data/node/5/relationships"},{"id":4,"location":"http://localhost:7474/db/data/index/relationship/my_rels/since/2010/1","body":{
      "indexed" : "http://localhost:7474/db/data/index/relationship/my_rels/since/2010/1",
      "start" : "http://localhost:7474/db/data/node/5",
      "data" : {
        "since" : "2010"
      },
      "self" : "http://localhost:7474/db/data/relationship/1",
      "property" : "http://localhost:7474/db/data/relationship/1/properties/{key}",
      "properties" : "http://localhost:7474/db/data/relationship/1/properties",
      "type" : "KNOWS",
      "extensions" : {
      },
      "end" : "http://localhost:7474/db/data/node/6"
    },"from":"/index/relationship/my_rels"}]

Copyright © 2011 Neo Technology

Prev | Up | Next
| Home |