Skip to content

Commit d65edf6

Browse files
added trying-my-hands-on-neo4j-with-some-iot-data
1 parent c64240d commit d65edf6

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
---
2+
canonical_url: https://dev.to/networkandcode/trying-my-hands-on-neo4j-with-some-iot-data-28g7
3+
categories: cypher, graphdb, iot, neo4j
4+
date: 2022-06-30
5+
tags: cypher, graphdb, iot, neo4j
6+
title: Trying my hands on Neo4j with some IoT data
7+
---
8+
9+
* This post first appeared on [dev.to](https://dev.to/networkandcode/trying-my-hands-on-neo4j-with-some-iot-data-28g7) *
10+
11+
## Introduction
12+
13+
Hello :wave:, time for some GraphDB...
14+
15+
Neo4j is a Graph DB written in Java, the j in 4j at the end stands for Java :). I like the variety of self placed [courses](https://graphacademy.neo4j.com/courses/) and the certifications offered by them, all for free, wow.
16+
17+
[Cypher](https://neo4j.com/docs/cypher-manual/current/) is the declarative query language we would be using to interact with the Neo4j database. We can run Cypher queries directly on the Neo4j [browser](https://neo4j.com/docs/browser-manual/current/) and can use one of the [drivers](https://neo4j.com/developer/language-guides/) to make calls to the database from our application.
18+
19+
In this post we shall focus on running certain Cypher queries from the browser, and see some key GraphDB concepts along the way. Let's get started...
20+
21+
## Sandbox
22+
Head over to this [link](https://neo4j.com/sandbox/), signup/signin, and create a sandbox > create a project, choose Blank Project.
23+
24+
We have choosen blank project cause we can also load our own data instead of working with some pre-built data.
25+
26+
Click on the Open button to open the project with the Neo4j browser.
27+
28+
## Delete all
29+
I have already added some data to the blank project before, so I'm going to delete all the nodes and their relationships first, you may not need this yet, however you may come back to this step later on when needed.
30+
```
31+
MATCH (n) DETACH DELETE n
32+
33+
Deleted 42 nodes, deleted 1 relationship, completed after 9 ms.
34+
```
35+
In the command above, the parentheses pair represents a node, and n is a variable that represents a node, so we are matching all nodes (as there are no conditions) and subsequently deleting those including their relationships using [DETACH DELETE](https://neo4j.com/docs/cypher-manual/current/clauses/delete/).
36+
37+
## Node
38+
A node is a discrete entity, for our IoT use case, it could be an Edge device, power source etc. Refer to this github [repo](https://github.com/muntasirjoarder/NeoThings) for a sample IoT data modeling.
39+
40+
In Cypher a Node is enclosed in parantheses, for ex. `(e)` refers to a node represented by a variable e.
41+
42+
### Labels
43+
And then comes label(s) which are denoted by colon `:`, that we could use to tag a node, for instance if it's an edge device we could label it `:EdgeDevice`. So now our node becomes `(e:EdgeDevice)`. Note that labels are usually written in PascalCase.
44+
45+
We could add multiple labels to a node, so let's also label our edge device as a thing, making our node look like `(e:EdgeDevice:Thing)`,
46+
47+
### Properties
48+
Properties are key value pairs defining the actual object. Let's say we want to give some names to our edge devices, like Edge Device 1, 2 etc. and another property to identify the floor in which they are installed, like GF, 1F, 2F etc. The properties for our first device would then be like:
49+
```
50+
{
51+
name: 'Edge Device 1',
52+
floor: 'GF'
53+
}
54+
```
55+
56+
### Create nodes
57+
Our node should finally look like `(e:EdgeDevice:Thing { name: 'Edge Device 1', floor: 'GF' } )`. Note that the variable name is only optional and is required only if we reuse the variable in our query. Let's create the node on the browser.
58+
```
59+
CREATE (e:EdgeDevice:Thing { name: 'Edge Device 1', floor: 'GF' } ) RETURN e
60+
```
61+
You should now see a graph output like ![Edge Device 1 graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38qkhytq3slruitbo7le.png)
62+
63+
And the respective table should be:
64+
```
65+
{
66+
"identity": 41,
67+
"labels": [
68+
"EdgeDevice",
69+
"Thing"
70+
],
71+
"properties": {
72+
"name": "Edge Device 1",
73+
"floor": "GF"
74+
}
75+
}
76+
```
77+
Note that the identity was auto generated.
78+
79+
### Constraints
80+
What if we create another node with the same properties.
81+
```
82+
CREATE (e:EdgeDevice:Thing { name: 'Edge Device 1', floor: 'GF' } ) RETURN e
83+
```
84+
85+
It would get created, and we would now have two similar nodes.
86+
```
87+
MATCH (e:EdgeDevice:Thing { name: 'Edge Device 1'} ) RETURN COUNT(e)
88+
89+
2
90+
```
91+
92+
let's say we want the name of the device to be unique, in such case, we could create a [constraint](https://neo4j.com/docs/cypher-manual/current/constraints/examples/) to set the name as unique.
93+
94+
But before doing so, we should get rid of the duplicates, as otherwise it would throw an error. Let's first get the IDs of the nodes.
95+
```
96+
MATCH (e:EdgeDevice:Thing) RETURN(ID(e))
97+
98+
0
99+
41
100+
```
101+
So there are two nodes with IDs 0 and 41, we can delete one, let's go with 41. Note the numbers may vary in your case.
102+
```
103+
MATCH (e) WHERE ID(e) = 41 DELETE e
104+
105+
Deleted 1 node, completed after 8 ms.
106+
```
107+
108+
Let's now create the constraint.
109+
```
110+
CREATE CONSTRAINT unqiue_edge_device_name FOR (e:EdgeDevice) REQUIRE e.name IS UNIQUE
111+
112+
Added 1 constraint, completed after 70 ms.
113+
```
114+
115+
Now, if we try to create another device with the same name, it should fail.
116+
```
117+
CREATE (e:EdgeDevice:Thing { name: 'Edge Device 1', floor: 'GF' } ) RETURN e
118+
119+
Neo.ClientError.Schema.ConstraintValidationFailed
120+
Node(0) already exists with label `EdgeDevice` and property `name` = 'Edge Device 1'
121+
```
122+
The above [error](https://neo4j.com/docs/status-codes/current/) denotes ClientError as the error classification, Schema as the error category and ConstraintValidationFailed as the error title.
123+
124+
### Import nodes
125+
So far, we have added only one edge device, we could add other devices as required in a similar manner. However it's common to import many such nodes from a CSV as it would be faster, than executing CREATE statements individually.
126+
127+
I am just going to add some data in Google sheets as follows.
128+
![Google sheet for edge devices](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/864ldautyu73t9yp0xdi.png) There are 74 devices in the table, from device 2 to 75.
129+
130+
I then published it with the following settings.![Google sheets publish](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4oft6ekutuzcyhkajt51.png). Let's now create nodes.
131+
```
132+
LOAD CSV WITH HEADERS FROM <google-spreadsheet-link> AS row
133+
CREATE (:EdgeDevice:Thing { name: row.name, floor: row.floor })
134+
135+
Added 148 labels, created 74 nodes, set 148 properties, completed after 586 ms.
136+
```
137+
Awesome, so we have successfully created the edge device nodes.
138+
139+
### Manufacturer nodes
140+
Let's do a similar exercise to add nodes for the manufacturers. We would add only one property which is name.
141+
142+
Note that I got the companies list from [here](https://builtin.com/internet-things/iot-internet-of-things-companies)
143+
144+
Let's create those. But this time let's go with a loop as we would only add 10 companies.
145+
```
146+
WITH ['Cooler Screens',
147+
'Farmer’s Fridge',
148+
'Simplisafe',
149+
'Inspire',
150+
'Enovo',
151+
'Tive',
152+
'Xage Security',
153+
'Samsara',
154+
'Arm',
155+
'Clearblade'] AS manufacturers
156+
FOREACH ( manufacturer IN manufacturers | CREATE (m:Manufacturer{ name: manufacturer}) )
157+
158+
Added 10 labels, created 10 nodes, set 10 properties, completed after 8 ms.
159+
```
160+
161+
Let's also add a constraint for the name.
162+
```
163+
CREATE CONSTRAINT unqiue_manufacturer_name FOR (m:Manufacturer) REQUIRE m.name IS UNIQUE
164+
```
165+
166+
## Sensor types
167+
Let's add few other nodes for the sensor type. I obtained the info form this [link](https://behrtech.com/blog/top-10-iot-sensor-types/)
168+
```
169+
WITH [
170+
'Temperature',
171+
'Humidity',
172+
'Pressure',
173+
'Proximity',
174+
'Level',
175+
'Accelerometers',
176+
'Gyroscope',
177+
'Gas',
178+
'Infrared',
179+
'Optical'
180+
] AS sensorTypes
181+
FOREACH ( sensorType IN sensorTypes | CREATE (:SensorType{ name: sensorType }) )
182+
183+
Added 10 labels, created 10 nodes, set 10 properties, completed after 7 ms.
184+
```
185+
186+
## Relatioships
187+
So far we have been adding nodes, for now these nodes are isolated data entities with out any relationships to other nodes.
188+
189+
Let's now add a relationship between the nodes Edge Device 1 and Cooler Screens.
190+
191+
Relationships are represented by arrows and square brackets. They are unidirectional `-[]->` or `<-[]-`. They also have a label like node, but it's only one label for a relationship unlike a node, and the relationship label is also called a relationship type.
192+
193+
Like a node, we can also add properties to a relationship. So if we use `r` as the variable and `IS_MANUFACTURED_BY` as the label it should now become `-[r:IS_MANUFACTURED_BY]->`. Just like nodes, using a variable is optional here too, and is useful when there is a need to reuse it.
194+
195+
Ok, so now let's put the source and target nodes in the relation, so the final Cypher would be `(:Edge_Device:Thing { name: 'Edge Device 1' } ) -[]-> (:Manufacturer { name: 'Cooler Screens'})`
196+
197+
We have formed the cypher, we just need to put CREATE before it to create the relation.
198+
```
199+
Match ( e:EdgeDevice:Thing {name: 'Edge Device 1'}), (m:Manufacturer { name: 'Cooler Screens' } )
200+
CREATE (e) -[r:IS_MANUFACTURED_BY]-> (m)
201+
RETURN e, r, m
202+
```
203+
204+
Let's use MATCH to get the graph.
205+
```
206+
MATCH (e:Edge_Device:Thing { name: 'Edge Device 1' } ) -[r:IS_MANUFACTURED_BY]-> (m:Manufacturer { name: 'Cooler Screens'}) RETURN e, r, m
207+
```
208+
209+
The graph should look like ![Relationship graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mwep5gzq8jf1arem1lj.png)
210+
211+
Note that we can drag the graph objects for desired visibility.
212+
213+
### Sensor
214+
To say Edge device device 1 supports Temperature sensor.
215+
```
216+
Match ( e:EdgeDevice:Thing {name: 'Edge Device 1'}), (s:SensorType { name: 'Temperature' })
217+
CREATE (e) -[r:HAS_SENSOR_TYPE]-> (s)
218+
RETURN e, r, s
219+
```
220+
221+
The returned graph should be
222+
![Sensor relationship](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0ka1w2y6adee1b2gi64.png)
223+
224+
Let's now see the final graph of Edge device 1 with both relationships.
225+
```
226+
MATCH (e:EdgeDevice:Thing) -[r]-> (n) RETURN e, r, n
227+
```
228+
![Graph with all relationships](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2pyb54v2y9ljtld2dqy.png)
229+
230+
Thus, we have put some minimal data relevant to IoT and explored some fundamental concepts/functionalities of GraphDB, Cypher, and the Neo4j browser. Thank you for reading !!!
231+
232+

_posts/2022-07-11-setup-iot-core-on-google-cloud-with-terraform.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ tags: cloud, googlecloud, iot, terraform
66
title: Setup IoT core on Google cloud with Terraform
77
---
88

9+
*This post first appeared on [dev.to](https://dev.to/networkandcode/setup-iot-core-on-google-cloud-with-terraform-4h6a)*
10+
911
Hello :wave:, we shall see how to provision a minimal IoT infrastructure on Google cloud with Terraform.
1012

1113
I shall be doing this straight on the Google cloud shell...

0 commit comments

Comments
 (0)