Skip to content

Commit 32b1c82

Browse files
author
Zhen Li
authored
Merge pull request #278 from petraselmer/1-1-Code-polish-up
Docs examples clean-ups
2 parents 40da1d0 + f8b5d56 commit 32b1c82

File tree

2 files changed

+129
-71
lines changed

2 files changed

+129
-71
lines changed

examples/src/main/java/org/neo4j/docs/driver/Examples.java

Lines changed: 100 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,47 @@ public static Driver configuration() throws Exception
6161

6262
public static void statement( Session session ) throws Exception
6363
{
64-
// tag::statement[]
65-
StatementResult result =
66-
session.run( "CREATE (person:Person {name: {name}})", Values.parameters( "name", "Arthur" ) );
67-
// end::statement[]
68-
int theOnesCreated = result.consume().counters().nodesCreated();
69-
System.out.println( "There were " + theOnesCreated + " the ones created." );
64+
try ( Transaction transaction = session.beginTransaction() )
65+
{
66+
// tag::statement[]
67+
StatementResult result =
68+
transaction.run( "CREATE (person:Person {name: {name}})", Values.parameters( "name", "Arthur" ) );
69+
transaction.success();
70+
// end::statement[]
71+
int theOnesCreated = result.consume().counters().nodesCreated();
72+
System.out.println( "There were " + theOnesCreated + " the ones created." );
73+
}
7074
}
7175

7276
public static void statementWithoutParameters( Session session ) throws Exception
7377
{
74-
// tag::statement-without-parameters[]
75-
StatementResult result = session.run( "CREATE (p:Person {name: 'Arthur'})" );
76-
// end::statement-without-parameters[]
77-
int theOnesCreated = result.consume().counters().nodesCreated();
78-
System.out.println( "There were " + theOnesCreated + " the ones created." );
78+
try ( Transaction transaction = session.beginTransaction() )
79+
{
80+
// tag::statement-without-parameters[]
81+
StatementResult result = transaction.run( "CREATE (p:Person { name: 'Arthur' })" );
82+
transaction.success();
83+
// end::statement-without-parameters[]
84+
int theOnesCreated = result.consume().counters().nodesCreated();
85+
System.out.println( "There were " + theOnesCreated + " the ones created." );
86+
}
7987
}
8088

8189
public static void resultTraversal( Session session ) throws Exception
8290
{
8391
// tag::result-traversal[]
8492
String searchTerm = "Sword";
85-
StatementResult result =
86-
session.run( "MATCH (weapon:Weapon) WHERE weapon.name CONTAINS {term} RETURN weapon.name",
87-
Values.parameters( "term", searchTerm ) );
88-
89-
System.out.println( "List of weapons called " + searchTerm + ":" );
90-
while ( result.hasNext() )
93+
try ( Transaction tx = session.beginTransaction() )
9194
{
92-
Record record = result.next();
93-
System.out.println( record.get( "weapon.name" ).asString() );
95+
StatementResult result =
96+
tx.run( "MATCH (weapon:Weapon) WHERE weapon.name CONTAINS {term} RETURN weapon.name",
97+
Values.parameters( "term", searchTerm ) );
98+
99+
System.out.println( "List of weapons called " + searchTerm + ":" );
100+
while ( result.hasNext() )
101+
{
102+
Record record = result.next();
103+
System.out.println( record.get( "weapon.name" ).asString() );
104+
}
94105
}
95106
// end::result-traversal[]
96107
}
@@ -99,37 +110,50 @@ public static void accessRecord( Session session ) throws Exception
99110
{
100111
// tag::access-record[]
101112
String searchTerm = "Arthur";
102-
StatementResult result = session.run( "MATCH (weapon:Weapon) WHERE weapon.owner CONTAINS {term} " +
103-
"RETURN weapon.name, weapon.material, weapon.size",
104-
Values.parameters( "term", searchTerm ) );
105113

106-
System.out.println( "List of weapons owned by " + searchTerm + ":" );
107-
while ( result.hasNext() )
114+
try ( Transaction tx = session.beginTransaction() )
108115
{
109-
Record record = result.next();
110-
List<String> sword = new ArrayList<>();
111-
for ( String key : record.keys() )
116+
StatementResult result = tx.run(
117+
"MATCH (weapon:Weapon) WHERE weapon.owner CONTAINS {term} RETURN weapon.name, weapon.material, " +
118+
"weapon.size",
119+
120+
Values.parameters( "term", searchTerm ) );
121+
122+
System.out.println( "List of weapons owned by " + searchTerm + ":" );
123+
while ( result.hasNext() )
112124
{
113-
sword.add( key + ": " + record.get( key ) );
125+
Record record = result.next();
126+
List<String> sword = new ArrayList<>();
127+
for ( String key : record.keys() )
128+
{
129+
sword.add( key + ": " + record.get( key ) );
130+
}
131+
System.out.println( sword );
114132
}
115-
System.out.println( sword );
116133
}
117134
// end::access-record[]
118135
}
119136

120137
public static void retainResultsForNestedQuerying( Session session ) throws Exception
121138
{
122139
// tag::nested-statements[]
123-
StatementResult result = session.run(
124-
"MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN id(knight) AS knight_id",
125-
Values.parameters( "castle", "Camelot" ) );
126-
140+
StatementResult result = null;
141+
try ( Transaction transaction = session.beginTransaction() )
142+
{
143+
result = transaction.run(
144+
"MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN id(knight) AS knight_id",
145+
Values.parameters( "castle", "Camelot" ) );
146+
}
127147
for ( Record record : result.list() )
128148
{
129-
session.run( "MATCH (knight) WHERE id(knight) = {id} " +
130-
"MATCH (king:Person) WHERE king.name = {king} " +
131-
"CREATE (knight)-[:DEFENDS]->(king)",
132-
Values.parameters( "id", record.get( "knight_id" ), "king", "Arthur" ) );
149+
try ( Transaction tx = session.beginTransaction() )
150+
{
151+
tx.run( "MATCH (knight) WHERE id(knight) = {id} " +
152+
"MATCH (king:Person) WHERE king.name = {king} " +
153+
"CREATE (knight)-[:DEFENDS]->(king)",
154+
Values.parameters( "id", record.get( "knight_id" ), "king", "Arthur" ) );
155+
tx.success();
156+
}
133157
}
134158
// end::nested-statements[]
135159
}
@@ -140,11 +164,15 @@ public static void retainResultsForLaterProcessing( Driver driver ) throws Excep
140164
List<Record> records;
141165
try ( Session session = driver.session() )
142166
{
143-
StatementResult result = session.run(
144-
"MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN knight.name AS name",
145-
Values.parameters( "castle", "Camelot" ) );
146167

147-
records = result.list();
168+
try ( Transaction tx = session.beginTransaction() )
169+
{
170+
StatementResult result = tx.run(
171+
"MATCH (knight:Person:Knight) WHERE knight.castle = {castle} RETURN knight.name AS name",
172+
Values.parameters( "castle", "Camelot" ) );
173+
174+
records = result.list();
175+
}
148176
}
149177

150178
for ( Record record : records )
@@ -159,7 +187,10 @@ public static void handleCypherError( Session session ) throws Exception
159187
// tag::handle-cypher-error[]
160188
try
161189
{
162-
session.run( "This will cause a syntax error" ).consume();
190+
try ( Transaction tx = session.beginTransaction() )
191+
{
192+
tx.run( "This will cause a syntax error" ).consume();
193+
}
163194
}
164195
catch ( ClientException e )
165196
{
@@ -195,24 +226,30 @@ public static void transactionRollback( Session session ) throws Exception
195226
public static void resultSummary( Session session ) throws Exception
196227
{
197228
// tag::result-summary-query-profile[]
198-
StatementResult result = session.run( "PROFILE MATCH (p:Person {name: {name}}) RETURN id(p)",
199-
Values.parameters( "name", "Arthur" ) );
229+
try ( Transaction tx = session.beginTransaction() )
230+
{
231+
StatementResult result = tx.run( "PROFILE MATCH (p:Person { name: {name} }) RETURN id(p)",
232+
Values.parameters( "name", "Arthur" ) );
200233

201-
ResultSummary summary = result.consume();
234+
ResultSummary summary = result.consume();
202235

203-
System.out.println( summary.statementType() );
204-
System.out.println( summary.profile() );
236+
System.out.println( summary.statementType() );
237+
System.out.println( summary.profile() );
238+
}
205239
// end::result-summary-query-profile[]
206240
}
207241

208242
public static void notifications( Session session ) throws Exception
209243
{
210244
// tag::result-summary-notifications[]
211-
ResultSummary summary = session.run( "EXPLAIN MATCH (king), (queen) RETURN king, queen" ).consume();
212-
213-
for ( Notification notification : summary.notifications() )
245+
try ( Transaction tx = session.beginTransaction() )
214246
{
215-
System.out.println( notification );
247+
ResultSummary summary = tx.run( "EXPLAIN MATCH (king), (queen) RETURN king, queen" ).consume();
248+
249+
for ( Notification notification : summary.notifications() )
250+
{
251+
System.out.println( notification );
252+
}
216253
}
217254
// end::result-summary-notifications[]
218255
}
@@ -230,10 +267,12 @@ public static Driver requireEncryption() throws Exception
230267
public static Driver trustOnFirstUse() throws Exception
231268
{
232269
// tag::tls-trust-on-first-use[]
233-
Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ), Config.build()
234-
.withEncryptionLevel( Config.EncryptionLevel.REQUIRED )
235-
.withTrustStrategy( Config.TrustStrategy.trustOnFirstUse( new File( "/path/to/neo4j_known_hosts" ) ) )
236-
.toConfig() );
270+
Driver driver =
271+
GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ), Config.build()
272+
.withEncryptionLevel( Config.EncryptionLevel.REQUIRED )
273+
.withTrustStrategy(
274+
Config.TrustStrategy.trustOnFirstUse( new File( "/path/to/neo4j_known_hosts" ) ) )
275+
.toConfig() );
237276
// end::tls-trust-on-first-use[]
238277

239278
return driver;
@@ -242,10 +281,12 @@ public static Driver trustOnFirstUse() throws Exception
242281
public static Driver trustSignedCertificates() throws Exception
243282
{
244283
// tag::tls-signed[]
245-
Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ), Config.build()
246-
.withEncryptionLevel( Config.EncryptionLevel.REQUIRED )
247-
.withTrustStrategy( Config.TrustStrategy.trustCustomCertificateSignedBy( new File( "/path/to/ca-certificate.pem" ) ) )
248-
.toConfig() );
284+
Driver driver =
285+
GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ), Config.build()
286+
.withEncryptionLevel( Config.EncryptionLevel.REQUIRED )
287+
.withTrustStrategy( Config.TrustStrategy
288+
.trustCustomCertificateSignedBy( new File( "/path/to/ca-certificate.pem" ) ) )
289+
.toConfig() );
249290
// end::tls-signed[]
250291

251292
return driver;

examples/src/main/java/org/neo4j/docs/driver/MinimalWorkingExample.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020

2121
// NOTE: Be careful about auto-formatting here: All imports should be between the tags below.
2222
// tag::minimal-example-import[]
23-
import org.neo4j.driver.v1.*;
23+
24+
import org.neo4j.driver.v1.AuthTokens;
25+
import org.neo4j.driver.v1.Driver;
26+
import org.neo4j.driver.v1.GraphDatabase;
27+
import org.neo4j.driver.v1.Record;
28+
import org.neo4j.driver.v1.Session;
29+
import org.neo4j.driver.v1.StatementResult;
30+
import org.neo4j.driver.v1.Transaction;
2431

2532
import static org.neo4j.driver.v1.Values.parameters;
2633
// end::minimal-example-import[]
@@ -31,21 +38,31 @@ public static void minimalWorkingExample() throws Exception
3138
{
3239
// tag::minimal-example[]
3340
Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ) );
34-
Session session = driver.session();
35-
36-
session.run( "CREATE (a:Person {name: {name}, title: {title}})",
37-
parameters( "name", "Arthur", "title", "King" ) );
3841

39-
StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = {name} " +
40-
"RETURN a.name AS name, a.title AS title",
41-
parameters( "name", "Arthur" ) );
42-
while ( result.hasNext() )
42+
try ( Session session = driver.session() )
4343
{
44-
Record record = result.next();
45-
System.out.println( record.get( "title" ).asString() + " " + record.get( "name" ).asString() );
44+
45+
try ( Transaction tx = session.beginTransaction() )
46+
{
47+
tx.run( "CREATE (a:Person {name: {name}, title: {title}})",
48+
parameters( "name", "Arthur", "title", "King" ) );
49+
tx.success();
50+
}
51+
52+
try ( Transaction tx = session.beginTransaction() )
53+
{
54+
StatementResult result = tx.run( "MATCH (a:Person) WHERE a.name = {name} " +
55+
"RETURN a.name AS name, a.title AS title",
56+
parameters( "name", "Arthur" ) );
57+
while ( result.hasNext() )
58+
{
59+
Record record = result.next();
60+
System.out.println( String.format( "%s %s", record.get( "title" ).asString(), record.get( "name" ).asString() ) );
61+
}
62+
}
63+
4664
}
4765

48-
session.close();
4966
driver.close();
5067
// end::minimal-example[]
5168
}

0 commit comments

Comments
 (0)