Skip to content

Commit e7f3287

Browse files
Extract vertex label to env variable and remove cleanup code
1 parent ca5d5af commit e7f3287

File tree

4 files changed

+27
-33
lines changed

4 files changed

+27
-33
lines changed

glv-examples/gremlin-python/connections.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def with_remote():
4747
g.V().drop().iterate()
4848

4949
# simple query to verify connection
50-
v = g.add_v().iterate()
51-
count = g.V().count().next()
50+
v = g.add_v("connection").iterate()
51+
count = g.V().has_label("connection").count().next()
5252
print("Vertex count: " + str(count))
5353

5454
# cleanup
@@ -60,8 +60,8 @@ def with_auth():
6060
rc = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g', username='stephen', password='password')
6161
g = traversal().with_remote(rc)
6262

63-
v = g.add_v().iterate()
64-
count = g.V().count().next()
63+
v = g.add_v("connection").iterate()
64+
count = g.V().has_label("connection").count().next()
6565
print("Vertex count: " + str(count))
6666

6767
rc.close()
@@ -72,8 +72,8 @@ def with_kerberos():
7272
rc = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g', kerberized_service='[email protected]')
7373
g = traversal().with_remote(rc)
7474

75-
v = g.add_v().iterate()
76-
count = g.V().count().next()
75+
v = g.add_v("connection").iterate()
76+
count = g.V().has_label("connection").count().next()
7777
print("Vertex count: " + str(count))
7878

7979
rc.close()
@@ -90,8 +90,8 @@ def with_configs():
9090
)
9191
g = traversal().with_remote(rc)
9292

93-
v = g.add_v().iterate()
94-
count = g.V().count().next()
93+
v = g.add_v("connection").iterate()
94+
count = g.V().has_label("connection").count().next()
9595
print("Vertex count: " + str(count))
9696

9797
rc.close()

gremlin-python/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ services:
6565
- VERSION=${VERSION}
6666
- GREMLIN_SOCKET_SERVER_URL=ws://gremlin-socket-server-python:{}/gremlin
6767
- GREMLIN_SOCKET_SERVER_CONFIG_PATH=/python_app/gremlin-socket-server/conf/test-ws-gremlin.yaml
68+
- VERTEX_LABEL=python-example
6869
working_dir: /python_app
6970
command: >
7071
bash -c "apt-get update && apt-get -y install libkrb5-dev krb5-user

gremlin-python/src/main/python/examples/basic_gremlin.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,29 @@
2828
def main():
2929
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
3030
server_url = os.getenv('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin').format(45940)
31+
vertex_label = os.getenv('VERTEX_LABEL', 'person')
3132
rc = DriverRemoteConnection(server_url, 'g')
3233
g = traversal().with_remote(rc)
3334

3435
# basic Gremlin: adding and retrieving data
35-
v1 = g.add_v('person-py-ex').property('name', 'marko').next()
36-
v2 = g.add_v('person-py-ex').property('name', 'stephen').next()
37-
v3 = g.add_v('person-py-ex').property('name', 'vadas').next()
36+
v1 = g.add_v(vertex_label).property('name', 'marko').next()
37+
v2 = g.add_v(vertex_label).property('name', 'stephen').next()
38+
v3 = g.add_v(vertex_label).property('name', 'vadas').next()
3839

3940
# be sure to use a terminating step like next() or iterate() so that the traversal "executes"
4041
# iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
4142
g.V(v1).add_e('knows').to(v2).property('weight', 0.75).iterate()
4243
g.V(v1).add_e('knows').to(v3).property('weight', 0.75).iterate()
4344

4445
# retrieve the data from the "marko" vertex
45-
marko = g.V().has('person-py-ex', 'name', 'marko').values('name').next()
46+
marko = g.V().has(vertex_label, 'name', 'marko').values('name').next()
4647
print("name: " + marko)
4748

4849
# find the "marko" vertex and then traverse to the people he "knows" and return their data
49-
people_marko_knows = g.V().has('person-py-ex', 'name', 'marko').out('knows').values('name').to_list()
50+
people_marko_knows = g.V().has(vertex_label, 'name', 'marko').out('knows').values('name').to_list()
5051
for person in people_marko_knows:
5152
print("marko knows " + person)
5253

53-
# clean added data
54-
g.V().has_label('person-py-ex').drop().iterate()
55-
5654
rc.close()
5755

5856

gremlin-python/src/main/python/examples/connections.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,15 @@ def with_remote():
4646
# variable named "g" as referenced in the following line.
4747
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
4848
server_url = os.getenv('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin').format(45940)
49+
vertex_label = os.getenv('VERTEX_LABEL', 'connection')
4950
rc = DriverRemoteConnection(server_url, 'g')
5051
g = traversal().with_remote(rc)
5152

5253
# simple query to verify connection
53-
v = g.add_v('py-conn-ex').iterate()
54-
count = g.V().has_label('py-conn-ex').count().next()
54+
v = g.add_v(vertex_label).iterate()
55+
count = g.V().has_label(vertex_label).count().next()
5556
print("Vertex count: " + str(count))
5657

57-
# clean added data
58-
g.V().has_label('py-conn-ex').drop().iterate()
5958
# cleanup
6059
rc.close()
6160

@@ -64,6 +63,7 @@ def with_remote():
6463
def with_auth():
6564
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
6665
server_url = os.getenv('GREMLIN_SERVER_BASIC_AUTH_URL', 'ws://localhost:8182/gremlin').format(45941)
66+
vertex_label = os.getenv('VERTEX_LABEL', 'connection')
6767

6868
# disable SSL certificate verification for remote hosts in test environments
6969
if 'localhost' not in server_url:
@@ -77,38 +77,36 @@ def with_auth():
7777

7878
g = traversal().with_remote(rc)
7979

80-
v = g.add_v('py-conn-ex').iterate()
81-
count = g.V().has_label('py-conn-ex').count().next()
80+
v = g.add_v(vertex_label).iterate()
81+
count = g.V().has_label(vertex_label).count().next()
8282
print("Vertex count: " + str(count))
8383

84-
# clean added data
85-
g.V().has_label('py-conn-ex').drop().iterate()
8684
rc.close()
8785

8886

8987
# connecting with Kerberos SASL authentication
9088
def with_kerberos():
9189
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
9290
server_url = os.getenv('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin').format(45942)
91+
vertex_label = os.getenv('VERTEX_LABEL', 'connection')
9392
kerberos_hostname = os.getenv('KRB_HOSTNAME', socket.gethostname())
9493
kerberized_service = f'test-service@{kerberos_hostname}'
9594

9695
rc = DriverRemoteConnection(server_url, 'g', kerberized_service=kerberized_service)
9796
g = traversal().with_remote(rc)
9897

99-
v = g.add_v('py-conn-ex').iterate()
100-
count = g.V().has_label('py-conn-ex').count().next()
98+
v = g.add_v(vertex_label).iterate()
99+
count = g.V().has_label(vertex_label).count().next()
101100
print("Vertex count: " + str(count))
102101

103-
# clean added data
104-
g.V().has_label('py-conn-ex').drop().iterate()
105102
rc.close()
106103

107104

108105
# connecting with customized configurations
109106
def with_configs():
110107
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
111108
server_url = os.getenv('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin').format(45940)
109+
vertex_label = os.getenv('VERTEX_LABEL', 'connection')
112110
rc = DriverRemoteConnection(
113111
server_url, 'g',
114112
username="", password="", kerberized_service='',
@@ -118,13 +116,10 @@ def with_configs():
118116
)
119117
g = traversal().with_remote(rc)
120118

121-
v = g.add_v('py-conn-ex').iterate()
122-
count = g.V().has_label('py-conn-ex').count().next()
119+
v = g.add_v(vertex_label).iterate()
120+
count = g.V().has_label(vertex_label).count().next()
123121
print("Vertex count: " + str(count))
124122

125-
# clean added data
126-
g.V().has_label('py-conn-ex').drop().iterate()
127-
128123
rc.close()
129124

130125

0 commit comments

Comments
 (0)