Skip to content

Commit c20c387

Browse files
committed
rolled in reivew comments
1 parent 0e6e9dd commit c20c387

27 files changed

+1079
-693
lines changed

python/example_code/neptune/analytics/create_neptune_graph_example.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import boto3
55
from botocore.exceptions import ClientError, BotoCoreError
6-
6+
from botocore.config import Config
77
# snippet-start:[neptune.python.graph.create.main]
88
"""
99
Running this example.
@@ -26,31 +26,21 @@ def main():
2626
"""
2727
Main entry point: create NeptuneGraph client and call graph creation.
2828
"""
29-
# Hypothetical client - boto3 currently doesn't have NeptuneGraph client, so replace with actual client if available
30-
neptune_graph_client = boto3.client("neptune")
31-
32-
execute_create_graph(neptune_graph_client, GRAPH_NAME)
29+
config = Config(retries={"total_max_attempts": 1, "mode": "standard"}, read_timeout=None)
30+
client = boto3.client("neptune-graph", config=config)
31+
execute_create_graph(client, GRAPH_NAME)
3332

3433

3534
def execute_create_graph(client, graph_name):
36-
"""
37-
Creates a new Neptune graph.
38-
39-
:param client: Boto3 Neptune graph client (hypothetical)
40-
:param graph_name: Name of the graph to create
41-
"""
4235
try:
4336
print("Creating Neptune graph...")
44-
45-
# Hypothetical method for create_graph, adjust accordingly if you use HTTP API or SDK extensions
4637
response = client.create_graph(
47-
GraphName=graph_name,
48-
ProvisionedMemory=16 # Example parameter, adjust if API differs
38+
GraphName=graph_name
4939
)
5040

51-
created_graph_name = response.get("Name")
52-
graph_arn = response.get("Arn")
53-
graph_endpoint = response.get("Endpoint")
41+
created_graph_name = response.get("GraphName")
42+
graph_arn = response.get("GraphArn")
43+
graph_endpoint = response.get("GraphEndpoint")
5444

5545
print("Graph created successfully!")
5646
print(f"Graph Name: {created_graph_name}")
@@ -65,6 +55,7 @@ def execute_create_graph(client, graph_name):
6555
print(f"Unexpected error: {str(e)}")
6656

6757

58+
6859
if __name__ == "__main__":
6960
main()
7061
# snippet-end:[neptune.python.graph.create.main]

python/example_code/neptune/analytics/neptune_analytics_query_example.py

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import boto3
55
from botocore.exceptions import ClientError
6-
6+
from botocore.config import Config
77
# snippet-start:[neptune.python.graph.execute.main]
88
"""
99
Running this example.
@@ -20,49 +20,85 @@
2020
2121
"""
2222

23-
NEPTUNE_ANALYTICS_ENDPOINT = "https://<your-neptune-analytics-endpoint>:8182"
2423
GRAPH_ID = "<your-graph-id>"
2524

2625
def main():
27-
# Build the boto3 client for neptune-graph with endpoint override
28-
client = boto3.client(
29-
"neptune-graph",
30-
endpoint_url=NEPTUNE_ANALYTICS_ENDPOINT
31-
)
26+
config = Config(retries={"total_max_attempts": 1, "mode": "standard"}, read_timeout=None)
27+
client = boto3.client("neptune-graph", config=config)
3228

3329
try:
34-
execute_gremlin_profile_query(client, GRAPH_ID)
30+
print("\n--- Running OpenCypher query without parameters ---")
31+
run_open_cypher_query(client, GRAPH_ID)
32+
33+
print("\n--- Running OpenCypher query with parameters ---")
34+
run_open_cypher_query_with_params(client, GRAPH_ID)
35+
36+
print("\n--- Running OpenCypher explain query ---")
37+
run_open_cypher_explain_query(client, GRAPH_ID)
38+
3539
except Exception as e:
3640
print(f"Unexpected error in main: {e}")
3741

38-
def execute_gremlin_profile_query(client, graph_id):
42+
def run_open_cypher_query(client, graph_id):
3943
"""
40-
Executes a Gremlin or OpenCypher query on Neptune Analytics graph.
41-
42-
Args:
43-
client (boto3.client): The NeptuneGraph client.
44-
graph_id (str): The graph identifier.
44+
Run an OpenCypher query without parameters.
4545
"""
46-
print("Running openCypher query on Neptune Analytics...")
47-
4846
try:
49-
response = client.execute_query(
47+
resp = client.execute_query(
5048
GraphIdentifier=graph_id,
5149
QueryString="MATCH (n {code: 'ANC'}) RETURN n",
5250
Language="OPEN_CYPHER"
5351
)
52+
if 'Payload' in resp:
53+
result = resp['Payload'].read().decode('utf-8')
54+
print(result)
55+
else:
56+
print("No query result returned.")
57+
except ClientError as e:
58+
print(f"NeptuneGraph ClientError: {e.response['Error']['Message']}")
59+
except Exception as e:
60+
print(f"Unexpected error: {e}")
5461

55-
# The response 'Payload' may contain the query results as a streaming bytes object
56-
# Convert to string and print
57-
if 'Payload' in response:
58-
result = response['Payload'].read().decode('utf-8')
59-
print("Query Result:")
62+
def run_open_cypher_query_with_params(client, graph_id):
63+
"""
64+
Run an OpenCypher query with parameters.
65+
"""
66+
try:
67+
parameters = {'code': 'ANC'}
68+
resp = client.execute_query(
69+
GraphIdentifier=graph_id,
70+
QueryString="MATCH (n {code: $code}) RETURN n",
71+
Language="OPEN_CYPHER",
72+
Parameters=parameters
73+
)
74+
if 'Payload' in resp:
75+
result = resp['Payload'].read().decode('utf-8')
6076
print(result)
6177
else:
6278
print("No query result returned.")
79+
except ClientError as e:
80+
print(f"NeptuneGraph ClientError: {e.response['Error']['Message']}")
81+
except Exception as e:
82+
print(f"Unexpected error: {e}")
6383

84+
def run_open_cypher_explain_query(client, graph_id):
85+
"""
86+
Run an OpenCypher explain query (explainMode = "debug").
87+
"""
88+
try:
89+
resp = client.execute_query(
90+
GraphIdentifier=graph_id,
91+
QueryString="MATCH (n {code: 'ANC'}) RETURN n",
92+
Language="OPEN_CYPHER",
93+
ExplainMode="debug"
94+
)
95+
if 'Payload' in resp:
96+
result = resp['Payload'].read().decode('utf-8')
97+
print(result)
98+
else:
99+
print("No query result returned.")
64100
except ClientError as e:
65-
print(f"NeptuneGraph error: {e.response['Error']['Message']}")
101+
print(f"NeptuneGraph ClientError: {e.response['Error']['Message']}")
66102
except Exception as e:
67103
print(f"Unexpected error: {e}")
68104

python/example_code/neptune/database/neptune_gremlin_query_example.py renamed to python/example_code/neptune/database/neptune_execute_gremlin_explain_query.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,17 @@ def main():
4040
def execute_gremlin_query(neptune_client):
4141
"""
4242
Executes a Gremlin query against an Amazon Neptune database.
43-
44-
:param neptune_client: Boto3 Neptunedata client
4543
"""
4644
try:
4745
print("Querying Neptune...")
4846

49-
response = neptune_client.execute_gremlin_query(
47+
response = neptune_client.execute_gremlin_explain_query(
5048
gremlinQuery="g.V().has('code', 'ANC')"
5149
)
5250

5351
print("Full Response:")
54-
print(response)
52+
print(response['output'].read().decode('UTF-8'))
5553

56-
result = response.get("result")
57-
if result:
58-
print("Query Result:")
59-
print(result)
60-
else:
61-
print("No result returned from the query.")
6254
except ClientError as e:
6355
print(f"Error calling Neptune: {e.response['Error']['Message']}")
6456
except BotoCoreError as e:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def run_profile_query(neptune_client):
7474
gremlinQuery="g.V().has('code', 'ANC')"
7575
)
7676
print("Profile Query Result:")
77-
print(response.get("output", "No profile output returned."))
77+
print(response['output'].read().decode('UTF-8'))
7878
except Exception as e:
7979
print(f"Failed to execute PROFILE query: {str(e)}")
8080

python/example_code/neptune/database/gremlin_profile_query_example.py renamed to python/example_code/neptune/database/neptune_execute_gremlin_query.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,22 @@
2121
- A connected environment such as a **VPN**, **AWS Direct Connect**, or a **peered VPC**
2222
2323
"""
24-
25-
2624
# Customize this with your Neptune endpoint
2725
NEPTUNE_ENDPOINT = "https://<your-neptune-endpoint>:8182"
2826

2927
def execute_gremlin_profile_query(client):
3028
"""
31-
Executes a Gremlin PROFILE query using the provided Neptune client.
29+
Executes a Gremlin query using the provided Neptune Data client.
3230
"""
3331
print("Executing Gremlin PROFILE query...")
3432

3533
try:
36-
response = client.execute_gremlin_profile_query(
37-
gremlinQuery="g.V().has('code', 'ANC')"
34+
response = client.execute_gremlin_query(
35+
gremlinQueyr="g.V().has('code', 'ANC')"
3836
)
39-
output = response.get("output")
4037

41-
if output:
42-
print("Query Profile Output:")
43-
print(json.dumps(output, indent=2))
44-
else:
45-
print("No output returned from the profile query.")
38+
print("Response is:")
39+
print(response['result'])
4640

4741
except ClientError as e:
4842
print(f"Neptune error: {e.response['Error']['Message']}")

python/example_code/neptune/database/open_cypher_explain_example.py renamed to python/example_code/neptune/database/neptune_execute_open_cypher_query.py

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import boto3
5+
import json
56
from botocore.config import Config
67
from botocore.exceptions import ClientError, BotoCoreError
78

@@ -20,12 +21,13 @@
2021
2122
"""
2223
# snippet-start:[neptune.python.data.query.opencypher.main]
24+
2325
# Replace with your actual Neptune endpoint URL
2426
NEPTUNE_ENDPOINT = "https://<your-neptune-endpoint>:8182"
2527

2628
def main():
2729
"""
28-
Entry point: Create Neptune client and execute the OpenCypher EXPLAIN query.
30+
Entry point: Create Neptune client and execute different OpenCypher queries.
2931
"""
3032
config = Config(connect_timeout=10, read_timeout=30, retries={'max_attempts': 3})
3133

@@ -35,34 +37,63 @@ def main():
3537
config=config
3638
)
3739

38-
execute_opencypher_explain_query(neptune_client)
39-
40+
execute_open_cypher_query_without_params(neptune_client)
41+
execute_open_cypher_query_with_params(neptune_client)
42+
execute_open_cypher_explain_query(neptune_client)
4043

41-
def execute_opencypher_explain_query(neptune_client):
44+
def execute_open_cypher_query_without_params(client):
45+
"""
46+
Executes a simple OpenCypher query without parameters.
4247
"""
43-
Executes an OpenCypher EXPLAIN query on Amazon Neptune.
48+
try:
49+
print("\nRunning OpenCypher query without parameters...")
50+
resp = client.execute_open_cypher_query(
51+
openCypherQuery="MATCH (n {code: 'ANC'}) RETURN n"
52+
)
53+
print("Results:")
54+
print(resp['results'])
4455

45-
:param neptune_client: Boto3 Neptunedata client
56+
except Exception as e:
57+
print(f"Error in simple OpenCypher query: {str(e)}")
58+
59+
60+
def execute_open_cypher_query_with_params(client):
61+
"""
62+
Executes an OpenCypher query using parameters.
4663
"""
4764
try:
48-
print("Executing OpenCypher EXPLAIN query...")
65+
print("\nRunning OpenCypher query with parameters...")
66+
parameters = {'code': 'ANC'}
67+
resp = client.execute_open_cypher_query(
68+
openCypherQuery="MATCH (n {code: $code}) RETURN n",
69+
parameters=json.dumps(parameters)
70+
)
71+
print("Results:")
72+
print(resp['results'])
73+
74+
except Exception as e:
75+
print(f"Error in parameterized OpenCypher query: {str(e)}")
4976

50-
response = neptune_client.execute_open_cypher_explain_query(
77+
def execute_open_cypher_explain_query(client):
78+
"""
79+
Runs an OpenCypher EXPLAIN query in debug mode.
80+
"""
81+
try:
82+
print("\nRunning OpenCypher EXPLAIN query (debug mode)...")
83+
resp = client.execute_open_cypher_explain_query(
5184
openCypherQuery="MATCH (n {code: 'ANC'}) RETURN n",
5285
explainMode="debug"
5386
)
54-
55-
results = response.get("results")
56-
if results:
57-
# `results` might be bytes or string, decode if necessary
58-
if isinstance(results, bytes):
59-
print("Explain Results:")
60-
print(results.decode("utf-8"))
61-
else:
62-
print("Explain Results:")
63-
print(results)
64-
else:
87+
results = resp.get('results')
88+
if results is None:
6589
print("No explain results returned.")
90+
else:
91+
try:
92+
print("Explain Results:")
93+
print(results.read().decode('UTF-8'))
94+
except Exception as e:
95+
print(f"Error in OpenCypher EXPLAIN query: {str(e)}")
96+
6697
except ClientError as e:
6798
print(f"Neptune error: {e.response['Error']['Message']}")
6899
except BotoCoreError as e:

0 commit comments

Comments
 (0)