Skip to content

Commit cafeca2

Browse files
committed
Changed the way the template code is handled
- have now "runnable" code as templates, so it can be tested directly - replace the "fixed" string fragments with whatever the target repo needs - tried to update / improve the code for Neo4j 4.x - added run instructions for dotnet core - simplified java run instructions - Go code is still overly verbose
1 parent 92df27b commit cafeca2

File tree

7 files changed

+85
-52
lines changed

7 files changed

+85
-52
lines changed

code/csharp/Example.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// dotnet create neo4j-test
2-
// dotnet add Neo4j.Driver
3-
// paste in this code
1+
// install dotnet core on your system
2+
// dotnet new console -o .
3+
// dotnet add package Neo4j.Driver
4+
// paste in this code into Program.cs
5+
// dotnet run
46

57
using System;
68
using System.Collections.Generic;
@@ -11,24 +13,24 @@
1113
namespace dotnet {
1214
class Example {
1315
static async Task Main() {
14-
var driver = GraphDatabase.Driver("<URL>",
15-
AuthTokens.Basic("<USERNAME>", "<PASSWORD>"));
16+
var driver = GraphDatabase.Driver("neo4j+s://demo.neo4jlabs.com:7687",
17+
AuthTokens.Basic("mUser", "s3cr3t"));
1618

1719
var cypherQuery =
18-
@$"
19-
<QUERY>
20+
@"
21+
MATCH (m:Movie {title:$movieTitle})<-[:ACTED_IN]-(a:Person) RETURN a.name as actorName
2022
";
2123

22-
var session = driver.AsyncSession();
24+
var session = driver.AsyncSession(o => o.WithDatabase("movies"));
2325
var result = await session.ReadTransactionAsync(async tx =>
2426
{
2527
var r = await tx.RunAsync(cypherQuery,
26-
new { <PARAM-NAME>=new[] {"<PARAM-VALUE>"}});
28+
new { movieTitle="The Matrix"});
2729
return await r.ToListAsync();
2830
});
2931
await session?.CloseAsync();
3032
foreach (var row in result)
31-
Console.WriteLine(row["<RESULT-COLUMN>"].As<string>());
33+
Console.WriteLine(row["actorName"].As<string>());
3234
}
3335
}
3436
}

code/go/example.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// go mode init main
1+
// go mod init main
22
// go run example.go
33
package main
44
import (
55
"fmt"
66
"github.com/neo4j/neo4j-go-driver/neo4j" //Go 1.8
77
)
88
func main() {
9-
s, err := runQuery("bolt://<HOST>:<BOLTPORT>", "<USERNAME>", "<PASSWORD>")
9+
s, err := runQuery("bolt://demo.neo4jlabs.com:7687", "mUser", "s3cr3t")
1010
if err != nil {
1111
panic(err)
1212
}
@@ -19,23 +19,25 @@ func runQuery(uri, username, password string) ([]string, error) {
1919
return nil, err
2020
}
2121
defer driver.Close()
22-
sessionConfig := neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead}
22+
sessionConfig := neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead, DatabaseName: "movies"}
2323
session, err := driver.NewSession(sessionConfig)
2424
if err != nil {
2525
return nil, err
2626
}
2727
defer session.Close()
2828
results, err := session.ReadTransaction(func(transaction neo4j.Transaction) (interface{}, error) {
2929
result, err := transaction.Run(
30-
"<QUERY>", map[string]interface{}{
31-
"<PARAM-NAME>": "<PARAM-VALUE>",
30+
`
31+
MATCH (m:Movie {title:$movieTitle})<-[:ACTED_IN]-(a:Person) RETURN a.name as actorName
32+
`, map[string]interface{}{
33+
"movieTitle": "The Matrix",
3234
})
3335
if err != nil {
3436
return nil, err
3537
}
3638
arr := make([]string, 0)
3739
for result.Next() {
38-
value, found := result.Record().Get("<RESULT-COLUMN>")
40+
value, found := result.Record().Get("actorName")
3941
if found {
4042
arr = append(arr, value.(string))
4143
}

code/java/Example.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
// todo add minimal maven/gradle file
1+
// Add your the driver dependency to your pom.xml build.gradle etc.
22
// Java Driver Dependency: http://search.maven.org/#artifactdetails|org.neo4j.driver|neo4j-java-driver|4.0.1|jar
33
// Reactive Streams http://search.maven.org/#artifactdetails|org.reactivestreams|reactive-streams|1.0.3|jar
4-
// javac -cp neo4j-java-driver-*.jar Example.java && java -cp neo4j-java-driver-*.jar:reactive-streams-*.jar:. Example
4+
// download jars into current directory
5+
// java -cp "*" Example.java
56

67
import org.neo4j.driver.*;
78
import static org.neo4j.driver.Values.parameters;
89

9-
import static java.util.Arrays.asList;
10-
import static java.util.Collections.singletonMap;
11-
1210
public class Example {
1311

1412
public static void main(String...args) {
15-
Driver driver = GraphDatabase.driver("bolt://<HOST>:<BOLTPORT>",
16-
AuthTokens.basic("<USERNAME>","<PASSWORD>"));
17-
try (Session session = driver.session()) {
13+
Driver driver = GraphDatabase.driver("neo4j+s://demo.neo4jlabs.com:7687",
14+
AuthTokens.basic("mUser","s3cr3t"));
15+
try (Session session = driver.session(SessionConfig.forDatabase("movies"))) {
1816

1917
String cypherQuery =
20-
"""
21-
<QUERY>
22-
""";
18+
"MATCH (m:Movie {title:$movieTitle})<-[:ACTED_IN]-(a:Person) RETURN a.name as actorName";
2319

24-
Result result = session.readTransaction(
20+
var result = session.readTransaction(
2521
tx -> tx.run(cypherQuery,
26-
parameters("<PARAM-NAME>",asList("<PARAM-VALUE>"))));
22+
parameters("movieTitle","The Matrix"))
23+
.list());
2724

28-
while (result.hasNext()) {
29-
System.out.println(result.next().get("<RESULT-COLUMN>").asString());
25+
for (Record record : result) {
26+
System.out.println(record.get("actorName").asString());
3027
}
3128
}
3229
driver.close();

code/javascript/example.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
// npm install --save neo4j-driver
22
// node example.js
33
var neo4j = require('neo4j-driver');
4-
var driver = neo4j.driver('bolt://<HOST>:<BOLTPORT>',
5-
neo4j.auth.basic('<USERNAME>', '<PASSWORD>'),
6-
{encrypted: 'ENCRYPTION_OFF'});
4+
var driver = neo4j.driver('neo4j+s://demo.neo4jlabs.com:7687',
5+
neo4j.auth.basic('mUser', 's3cr3t'),
6+
{/* encrypted: 'ENCRYPTION_OFF' */});
77

88
var query =
9-
`<QUERY>`;
9+
`
10+
MATCH (m:Movie {title:$movieTitle})<-[:ACTED_IN]-(a:Person) RETURN a.name as actorName
11+
`;
1012

11-
var params = {"<PARAM-NAME>": ["<PARAM-VALUE>"]};
13+
var params = {"movieTitle": "The Matrix"};
1214

13-
var session = driver.session();
15+
var session = driver.session({database:"movies"});
1416

1517
session.run(query, params)
1618
.then(function(result) {
1719
result.records.forEach(function(record) {
18-
console.log(record.get('<RESULT-COLUMN>'));
20+
console.log(record.get('actorName'));
1921
})
2022
session.close();
2123
driver.close();

code/python/example.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
# pip install neo4j-driver
2-
# python example.py
1+
# pip3 install neo4j-driver
2+
# python3 example.py
33

44
from neo4j import GraphDatabase, basic_auth
55

66
driver = GraphDatabase.driver(
7-
"bolt://<HOST>:<BOLTPORT>",
8-
auth=basic_auth("<USERNAME>", "<PASSWORD>"))
7+
"neo4j+s://demo.neo4jlabs.com:7687",
8+
auth=basic_auth("mUser", "s3cr3t"))
99

1010
cypher_query = '''
11-
<QUERY>
11+
MATCH (m:Movie {title:$movieTitle})<-[:ACTED_IN]-(a:Person) RETURN a.name as actorName
1212
'''
1313

14-
with driver.session() as session:
14+
with driver.session(database="movies") as session:
1515
results = session.read_transaction(
1616
lambda tx: tx.run(cypher_query,
17-
<PARAM-NAME>=["<PARAM-VALUE>"]))
17+
movieTitle="The Matrix").data())
1818

1919
for record in results:
20-
print(record['<RESULT-COLUMN>'])
20+
print(record['actorName'])
2121

2222
driver.close()

code/render-code.sh

100644100755
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,42 @@ echo "Adding language examples to $TARGET, Hit ctrl-c to abort"
2525
read
2626

2727
mkdir -p $TARGET/code
28+
#cp -r code $TARGET
2829

2930
Q=`/bin/echo -n "$QUERY" | tr '\n' '§'`
3031

32+
echo $QUERY
33+
lines=`echo $QUERY | wc -l`
34+
echo Lines $lines
35+
echo $Q
36+
37+
URL="neo4j+s:\/\/demo.neo4jlabs.com:7687"
38+
BOLTURL="bolt:\/\/<HOST>:<BOLTPORT>"
39+
3140
for file in */?xample.*; do
3241
LANG=${file%%/*}
33-
echo $LANG $file
42+
echo "Updating $LANG $file"
3443
mkdir -p $TARGET/code/$LANG
35-
sed -e "s/<PARAM-NAME>/$PARAMNAME/g" -e "s/<PARAM-VALUE>/$PARAMVALUE/g" -e "s/<QUERY>/$Q/g" -e "s/<RESULT-COLUMN>/$RESULTCOLUMN/g" -e $'s/§/\\\n/g' $file > $TARGET/code/$file
44+
cp $file $TARGET/code/$file
45+
indent=`grep 'MATCH (m:Movie' $TARGET/code/$file | cut -d'M' -f1 | cut -d'"' -f1`
46+
echo "Indent #$indent#"
47+
if [ $LANG == "java" ]; then
48+
Q2=`/bin/echo -n "$QUERY" | sed -e "s/\(.*\)/$indent\"\1\" +/g" | tr '\n' '§' | sed -e 's/\+$/;/g'`
49+
else
50+
Q2=`/bin/echo -n "$QUERY" | sed -e "s/\(.*\)/$indent\1/g" | tr '\n' '§'`
51+
fi
52+
echo "$Q2"
53+
sed -i -e "s/^.*MATCH (m:Movie.*$/$Q2/g" $TARGET/code/$file
54+
mv $TARGET/code/$file $TARGET/code/$file.tmp
55+
tr '§' '\n' < $TARGET/code/$file.tmp > $TARGET/code/$file
56+
rm $TARGET/code/$file.tmp
57+
58+
sed -i -e "s/$URL/$BOLTURL/g" $TARGET/code/$file
59+
sed -i -e "s/movieTitle/$PARAMNAME/g" $TARGET/code/$file
60+
sed -i -e "s/The Matrix/$PARAMVALUE/g" $TARGET/code/$file
61+
sed -i -e "s/actorName/$RESULTCOLUMN/g" $TARGET/code/$file
62+
# database
63+
sed -i -e "s/movies/neo4j/g" $TARGET/code/$file
64+
sed -i -e "s/mUser/<USERNAME>/g" $TARGET/code/$file
65+
sed -i -e "s/s3cr3t/<PASSWORD>/g" $TARGET/code/$file
3666
done

code/test-code.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,22 @@ mkdir -p $CODE
4646
pushd $CODE
4747

4848
npm install --save neo4j-driver
49-
sed -e "s/<BOLTPORT>/$BOLTPORT/g" -e "s/<HOST>/$HOST/g" -e "s/<USERNAME>/$USERNAME/g" -e "s/<PASSWORD>/$PASSWORD/g" $TARGET/code/javascript/example.js > example.js
49+
sed -e "s/<BOLTPORT>/$BOLTPORT/g" -e "s/<HOST>/$HOST/g" -e "s/mUser/$USERNAME/g" -e "s/s3cr3t/$PASSWORD/g" $TARGET/code/javascript/example.js > example.js
5050
node example.js | grep "$EXPECT" || echo "JAVASCRIPT FAIL"
5151

5252
pip install neo4j-driver
53-
sed -e "s/<BOLTPORT>/$BOLTPORT/g" -e "s/<HOST>/$HOST/g" -e "s/<USERNAME>/$USERNAME/g" -e "s/<PASSWORD>/$PASSWORD/g" $TARGET/code/python/example.py > example.py
53+
sed -e "s/<BOLTPORT>/$BOLTPORT/g" -e "s/<HOST>/$HOST/g" -e "s/mUser/$USERNAME/g" -e "s/s3cr3t/$PASSWORD/g" $TARGET/code/python/example.py > example.py
5454
python example.py | grep "$EXPECT" || echo "PYTHON FAIL"
5555

5656
curl -sOL https://repo1.maven.org/maven2/org/reactivestreams/reactive-streams/${RX_VERSION}/reactive-streams-${RX_VERSION}.jar
5757
curl -sOL https://repo1.maven.org/maven2/org/neo4j/driver/neo4j-java-driver/${JAVA_DRIVER_VERSION}/neo4j-java-driver-${JAVA_DRIVER_VERSION}.jar
5858

59-
sed -e "s/<BOLTPORT>/$BOLTPORT/g" -e "s/<HOST>/$HOST/g" -e "s/<USERNAME>/$USERNAME/g" -e "s/<PASSWORD>/$PASSWORD/g" $TARGET/code/java/Example.java > Example.java
59+
sed -e "s/<BOLTPORT>/$BOLTPORT/g" -e "s/<HOST>/$HOST/g" -e "s/mUser/$USERNAME/g" -e "s/s3cr3t/$PASSWORD/g" $TARGET/code/java/Example.java > Example.java
6060

6161
javac -cp neo4j-java-driver-${JAVA_DRIVER_VERSION}.jar Example.java
6262
java -cp neo4j-java-driver-${JAVA_DRIVER_VERSION}.jar:reactive-streams-${RX_VERSION}.jar:. Example | grep "$EXPECT" || echo "JAVA FAIL"
6363

64-
sed -e "s/<BOLTPORT>/$BOLTPORT/g" -e "s/<HOST>/$HOST/g" -e "s/<USERNAME>/$USERNAME/g" -e "s/<PASSWORD>/$PASSWORD/g" $TARGET/code/go/example.go > example.go
64+
sed -e "s/<BOLTPORT>/$BOLTPORT/g" -e "s/<HOST>/$HOST/g" -e "s/mUser/$USERNAME/g" -e "s/s3cr3t/$PASSWORD/g" $TARGET/code/go/example.go > example.go
6565
go mod init main
6666
go run example.go | grep "$EXPECT" || echo "GO FAIL"
6767

0 commit comments

Comments
 (0)