|
3 | 3 |
|
4 | 4 | [abstract]
|
5 | 5 | --
|
6 |
| -This section describes procedures that can be used to remove nodes from the database once a time limit has been reached. |
| 6 | +This section describes procedures that can be used to remove nodes from the database once a time or time limit has been reached. |
7 | 7 | --
|
8 | 8 |
|
9 | 9 | Some nodes are not meant to live forever.
|
10 | 10 | That's why with APOC you can specify a time by when they are removed from the database, by utilizing a schema index and an additional label.
|
11 |
| -A few convenience procedures help with that. |
| 11 | +A few procedures help with that. |
12 | 12 |
|
13 |
| -ifdef::backend-html5[] |
14 |
| -++++ |
15 |
| -<iframe width="560" height="315" src="https://www.youtube.com/embed/e9aoQ9xOmoU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> |
16 |
| -++++ |
17 |
| -endif::[] |
| 13 | +This section includes: |
18 | 14 |
|
19 |
| -Enable TTL with setting in `neo4j.conf` : `apoc.ttl.enabled=true` |
| 15 | +* <<ttl-available-procedures, Available Procedures>> |
| 16 | +* <<ttl-config-parameters, Configuration and Parameters>> |
| 17 | +//* <<ttl-handson-video, Time-To-Live Hands-On>> |
| 18 | +* <<ttl-examples, Examples>> |
| 19 | + ** <<ttl-expireAt, Expire at Specified Time>> |
| 20 | + ** <<ttl-expireIn, Expire after Amount of Time>> |
| 21 | +* <<ttl-process, Manual Process: How TTL Works>> |
20 | 22 |
|
21 |
| -There are some convenience procedures to expire nodes. |
| 23 | +[[ttl-available-procedures]] |
| 24 | +== Available Procedures |
22 | 25 |
|
23 |
| -You can also do it yourself by running |
| 26 | +The table below describes the available procedures: |
| 27 | + |
| 28 | +[separator=¦,opts=header,cols="1,1m,1m,5"] |
| 29 | +|=== |
| 30 | +include::../../../build/generated-documentation/apoc.ttl.csv[] |
| 31 | +|=== |
| 32 | + |
| 33 | +[[ttl-config-parameters]] |
| 34 | +== Configuration and Parameters |
| 35 | + |
| 36 | +For configuration, you will need to enable time-to-live functionality with the following settings in `neo4j.conf`: |
| 37 | + |
| 38 | +.neo4j.conf |
| 39 | +[source,properties] |
| 40 | +---- |
| 41 | +apoc.ttl.enabled=true |
| 42 | +
|
| 43 | +# Optional: controls the repeat frequency |
| 44 | +# apoc.ttl.schedule=5 |
| 45 | +---- |
| 46 | + |
| 47 | +In the available procedures listed above, there are several parameters with specific values. |
| 48 | +The table below outlines values and formats for the valid parameters. |
| 49 | + |
| 50 | +[options="header"] |
| 51 | +|=== |
| 52 | +| Parameter | Description | Possible Values | Examples |
| 53 | +| `node` | The entity or entities to add the label and property of time-to-live (previous selection statement needed) | Any node or group of nodes fitting desired criteria | `n`, `person`, `group` |
| 54 | +| `epochTime` | The datetime value of when the node(s) should expire | Any value in epoch seconds or millisecond format | `1540944000`, `1582209630000` |
| 55 | +| `time-unit` | Measurement of units for input value | `ms, s, m, h, d` (long forms: `millis, milliseconds, seconds, minutes, hours, days`) | `milliseconds`, `h` |
| 56 | +|=== |
| 57 | + |
| 58 | +//[[ttl-handson-video]] |
| 59 | +//== Time-To-Live Hands-On |
| 60 | +// |
| 61 | +//ifdef::backend-html5[] |
| 62 | +//++++ |
| 63 | +//<iframe width="560" height="315" src="https://www.youtube.com/embed/e9aoQ9xOmoU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> |
| 64 | +//++++ |
| 65 | +//endif::[] |
| 66 | +
|
| 67 | +[[ttl-examples]] |
| 68 | +== Examples: Time-To-Live |
| 69 | +
|
| 70 | +This section includes examples showing how to use the time-to-live procedures. |
| 71 | +These examples are based on a movies dataset, which can be imported by running the following Cypher query: |
| 72 | +
|
| 73 | +include::../export/createExportGraph.adoc[] |
| 74 | +
|
| 75 | +The Neo4j Browser visualization below shows the imported graph: |
| 76 | +
|
| 77 | +image::play-movies.png[title="Movies Graph Visualization"] |
| 78 | +
|
| 79 | +[[ttl-expireAt]] |
| 80 | +=== Expire node(s) at specified time |
| 81 | +
|
| 82 | +The `apoc.ttl.expireAtInstant` procedure deletes a node or group of nodes after the datetime specified. |
| 83 | +
|
| 84 | +To remove a single node or set of nodes, we can use a selection query prior to calling the procedure that defines which nodes we want to apply the time-to-live label and property. |
| 85 | +We then call the procedure and pass in the selected node(s), the future datetime at which we want the nodes to be removed, and the specificity of the datetime (seconds, milliseconds, etc). |
24 | 86 |
|
25 | 87 | [source,cypher]
|
26 | 88 | ----
|
27 |
| -SET n:TTL |
28 |
| -SET n.ttl = timestamp() + 3600 |
| 89 | +MATCH (movie:Movie)<-[produced:PRODUCED]-(person:Person) |
| 90 | +CALL apoc.ttl.expireAtInstant(person,1585176720,'s') |
| 91 | +RETURN movie, produced, person |
29 | 92 | ----
|
30 | 93 |
|
31 |
| -[cols="1m,5"] |
| 94 | +.Results |
| 95 | +[opts="header"] |
32 | 96 | |===
|
33 |
| -| CALL apoc.date.expire.in(node,time,'time-unit') | expire node in given time-delta by setting :TTL label and `ttl` property |
34 |
| -| CALL apoc.date.expire(node,time,'time-unit') | expire node at given time by setting :TTL label and `ttl` property |
| 97 | +| "movie" | "produced" | "person" |
| 98 | +| {"title":"The Matrix","tagline":"Welcome to the Real World","released":1999} | {} | {"name":"Joel Silver","ttl":1585176720000,"born":1952} |
35 | 99 | |===
|
36 | 100 |
|
37 |
| -Optionally set `apoc.ttl.schedule=5` as repeat frequency. |
| 101 | +After the point in time specified (in this case, after `2020-03-25 17:52:00`), the node(s) will be expired and deleted from the graph. |
| 102 | +Running the statement below will return no results for our example graph. |
| 103 | +
|
| 104 | +[source,cypher] |
| 105 | +---- |
| 106 | +MATCH (movie:Movie)<-[produced:PRODUCED]-(person:Person) |
| 107 | +RETURN movie, produced, person |
| 108 | +---- |
38 | 109 |
|
39 |
| -== Process |
| 110 | +[[ttl-expireIn]] |
| 111 | +=== Expire node(s) after specified time period |
40 | 112 |
|
41 |
| -30s after startup an index is created: |
| 113 | +The `apoc.ttl.expireAfterTimeLength` procedure deletes a node or group of nodes after the length of time specified. |
| 114 | +//LEFT OFF HERE! |
| 115 | +Just as with the similar procedure above, we can use a selection query prior to calling the procedure that defines which nodes we want to apply the time-to-live label and property. |
| 116 | +We then call the procedure and pass in the selected node(s), the time delta from current time at which we want the nodes to be removed, and the specificity of the time amount (seconds, milliseconds, etc). |
42 | 117 |
|
43 | 118 | [source,cypher]
|
44 | 119 | ----
|
45 |
| -CREATE INDEX ON :TTL(ttl) |
| 120 | +MATCH (movie:Movie)<-[produced:PRODUCED]-(person:Person) |
| 121 | +CALL apoc.ttl.expireAfterTimeLength(person,1585176720,'s') |
| 122 | +RETURN movie, produced, person |
46 | 123 | ----
|
47 | 124 |
|
48 |
| -At startup a statement is scheduled to run every 60s (or configure in `neo4j.conf` - `apoc.ttl.schedule=120`) |
| 125 | +.Results |
| 126 | +[opts="header"] |
| 127 | +|=== |
| 128 | +| "movie" | "produced" | "person" |
| 129 | +| {"title":"The Matrix","tagline":"Welcome to the Real World","released":1999} | {} | {"name":"Joel Silver","ttl":120000,"born":1952} |
| 130 | +|=== |
| 131 | +
|
| 132 | +After the length of time specified has passed (in this case, after `120 seconds`), the node(s) will be expired and deleted from the graph. |
| 133 | +Running the statement below will return no results for our example graph. |
49 | 134 |
|
50 | 135 | [source,cypher]
|
51 | 136 | ----
|
52 |
| -MATCH (t:TTL) where t.ttl < timestamp() WITH t LIMIT 1000 DETACH DELETE t |
| 137 | +MATCH (movie:Movie)<-[produced:PRODUCED]-(person:Person) |
| 138 | +RETURN movie, produced, person |
53 | 139 | ----
|
54 | 140 |
|
55 |
| -The `ttl` property holds the *time when the node is expired in milliseconds since epoch*. |
| 141 | +[[ttl-process]] |
| 142 | +== Manual Process: How TTL Works |
56 | 143 |
|
57 |
| -You can expire your nodes by setting the :TTL label and the ttl property: |
| 144 | +You can also do the time-to-live process manually by running the following steps: |
58 | 145 |
|
| 146 | +* Set the `:TTL` label and `ttl` property on the node(s) you want to expire. |
59 | 147 |
|
60 | 148 | [source,cypher]
|
61 | 149 | ----
|
62 |
| -MATCH (n:Foo) WHERE n.bar SET n:TTL, n.ttl = timestamp() + 10000; |
| 150 | +SET n:TTL |
| 151 | +SET n.ttl = timestamp() + 3600 |
63 | 152 | ----
|
64 | 153 |
|
65 |
| -There is also a procedure that does the same: |
| 154 | +The `ttl` property holds the *time when the node is expired in milliseconds since epoch*. |
| 155 | +
|
| 156 | +* Create an index on the time-to-live label and property. |
66 | 157 |
|
67 | 158 | [source,cypher]
|
68 | 159 | ----
|
69 |
| -CALL apoc.date.expire(node,time,'time-unit'); |
70 |
| -CALL apoc.date.expire(n,100,'s'); |
| 160 | +CREATE INDEX ON :TTL(ttl) |
| 161 | +---- |
| 162 | +
|
| 163 | +When using the procedure, the index is created 30 seconds after startup. |
| 164 | +
|
| 165 | +* Remove node(s) that have passed the expiration time or length of time |
| 166 | +
|
| 167 | +[source,cypher] |
| 168 | +---- |
| 169 | +MATCH (t:TTL) where t.ttl < timestamp() WITH t LIMIT 1000 DETACH DELETE t |
| 170 | +---- |
| 171 | +
|
| 172 | +When using the procedure, the deletion statement to remove nodes past expiration will run every 60 seconds. |
| 173 | +You can also configure the schedule by adding the following setting in `neo4j.conf`: |
| 174 | +
|
| 175 | +
|
| 176 | +.neo4j.conf |
| 177 | +[source,properties] |
71 | 178 | ----
|
| 179 | +# Optional: controls the repeat frequency |
| 180 | +apoc.ttl.schedule=120 |
| 181 | +---- |
0 commit comments