Skip to content

Commit be0a5db

Browse files
committed
Revamp cosmos-db/gremlin/quickstart-python
1 parent 0d53424 commit be0a5db

File tree

3 files changed

+165
-10
lines changed

3 files changed

+165
-10
lines changed

articles/cosmos-db/gremlin/quickstart-dotnet.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Application requests to most Azure services must be authorized. For the API for
136136
137137
:::code language="csharp" source="~/cosmos-db-apache-gremlin-dotnet-samples/001-quickstart/Program.cs" id="connect_client":::
138138
139-
## Create vertices
139+
### Create vertices
140140
141141
Now that the application is connected to the account, use the standard Gremlin syntax to create vertices.
142142
@@ -176,7 +176,7 @@ Now that the application is connected to the account, use the standard Gremlin s
176176
177177
:::code language="csharp" source="~/cosmos-db-apache-gremlin-dotnet-samples/001-quickstart/Program.cs" id="create_vertices_3":::
178178
179-
## Create edges
179+
### Create edges
180180
181181
Create edges using the Gremlin syntax to define relationships between vertices.
182182
@@ -191,7 +191,7 @@ Create edges using the Gremlin syntax to define relationships between vertices.
191191
192192
:::code language="csharp" source="~/cosmos-db-apache-gremlin-dotnet-samples/001-quickstart/Program.cs" id="create_edges_2":::
193193
194-
## Query vertices & edges
194+
### Query vertices & edges
195195
196196
Use the Gremlin syntax to traverse the graph and discover relationships between vertices.
197197

articles/cosmos-db/gremlin/quickstart-nodejs.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ In this quickstart, you use the `gremlin` library to connect to a newly created
3737

3838
## Setting up
3939

40-
This section walks you through creating an API for Gremlin account and setting up a .NET project to use the library to connect to the account.
40+
This section walks you through creating an API for Gremlin account and setting up a Node.js project to use the library to connect to the account.
4141

4242
### Create an API for Gremlin account
4343

@@ -125,7 +125,7 @@ Application requests to most Azure services must be authorized. For the API for
125125

126126
:::code language="javascript" source="~/cosmos-db-apache-gremlin-javascript-samples/001-quickstart/app.js" id="imports":::
127127

128-
1. Create `accountName` and `accountKey` string variables. Store the `COSMOS_GREMLIN_ENDPOINT` and `COSMOS_GREMLIN_KEY` environment variables as the values for each respective variable.
128+
1. Create `accountName` and `accountKey` variables. Store the `COSMOS_GREMLIN_ENDPOINT` and `COSMOS_GREMLIN_KEY` environment variables as the values for each respective variable.
129129

130130
:::code language="javascript" source="~/cosmos-db-apache-gremlin-javascript-samples/001-quickstart/app.js" id="environment_variables":::
131131

@@ -137,11 +137,10 @@ Application requests to most Azure services must be authorized. For the API for
137137
138138
:::code language="javascript" source="~/cosmos-db-apache-gremlin-javascript-samples/001-quickstart/app.js" id="connect_client":::
139139
140-
## Create vertices
140+
### Create vertices
141141
142142
Now that the application is connected to the account, use the standard Gremlin syntax to create vertices.
143143
144-
145144
1. Use `submit` to run a command server-side on the API for Gremlin account. Create a **product** vertex with the following properties:
146145
147146
| | Value |
@@ -178,7 +177,7 @@ Now that the application is connected to the account, use the standard Gremlin s
178177
179178
:::code language="javascript" source="~/cosmos-db-apache-gremlin-javascript-samples/001-quickstart/app.js" id="create_vertices_3":::
180179
181-
## Create edges
180+
### Create edges
182181
183182
Create edges using the Gremlin syntax to define relationships between vertices.
184183
@@ -193,7 +192,7 @@ Create edges using the Gremlin syntax to define relationships between vertices.
193192
194193
:::code language="javascript" source="~/cosmos-db-apache-gremlin-javascript-samples/001-quickstart/app.js" id="create_edges_2":::
195194
196-
## Query vertices & edges
195+
### Query vertices & edges
197196
198197
Use the Gremlin syntax to traverse the graph and discover relationships between vertices.
199198

articles/cosmos-db/gremlin/quickstart-python.md

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,177 @@ In this quickstart, you use the `gremlinpython` library to connect to a newly cr
3737

3838
## Setting up
3939

40-
This section walks you through creating an API for Gremlin account and setting up a .NET project to use the library to connect to the account.
40+
This section walks you through creating an API for Gremlin account and setting up a Python project to use the library to connect to the account.
4141

4242
### Create an API for Gremlin account
4343

4444
The API for Gremlin account should be created prior to using the Python library. Additionally, it helps to also have the database and graph in place.
4545

4646
[!INCLUDE[Create account, database, and graph](includes/create-account-database-graph-cli.md)]
4747

48+
### Create a new Python console application
4849

50+
Create a Python console application in an empty folder using your preferred terminal.
4951

52+
1. Open your terminal in an empty folder.
5053

54+
1. Create the **app.py** file.
5155

56+
```bash
57+
touch app.py
58+
```
5259

60+
### Install the PyPI package
5361

62+
Add the `gremlinpython` PyPI package to the Python project.
5463

64+
1. Create the **requirements.txt** file.
65+
66+
```bash
67+
touch requirements.txt
68+
```
69+
70+
1. Add the `gremlinpython` package from the Python Package Index to the requirements file.
71+
72+
```requirements
73+
gremlinpython==3.7.0
74+
```
75+
76+
1. Install all the requirements to your project.
77+
78+
```bash
79+
python install -r requirements.txt
80+
```
81+
82+
### Configure environment variables
83+
84+
To use the *NAME* and *URI* values obtained earlier in this quickstart, persist them to new environment variables on the local machine running the application.
85+
86+
1. To set the environment variable, use your terminal to persist the values as `COSMOS_ENDPOINT` and `COSMOS_KEY` respectively.
87+
88+
```bash
89+
export COSMOS_GREMLIN_ENDPOINT="<account-name>"
90+
export COSMOS_GREMLIN_KEY="<account-key>"
91+
```
92+
93+
1. Validate that the environment variables were set correctly.
94+
95+
```bash
96+
printenv COSMOS_GREMLIN_ENDPOINT
97+
printenv COSMOS_GREMLIN_KEY
98+
```
99+
100+
## Code examples
101+
102+
- [Authenticate the client](#authenticate-the-client)
103+
- [Create vertices](#create-vertices)
104+
- [Create edges](#create-edges)
105+
- [Query vertices &amp; edges](#query-vertices--edges)
106+
107+
The code in this article connects to a database named `cosmicworks` and a graph named `products`. The code then adds vertices and edges to the graph before traversing the added items.
108+
109+
### Authenticate the client
110+
111+
Application requests to most Azure services must be authorized. For the API for Gremlin, use the *NAME* and *URI* values obtained earlier in this quickstart.
112+
113+
1. Open the **app.py** file.
114+
115+
1. Import `client` and `serializer` from the `gremlin_python.driver` module.
116+
117+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="imports":::
118+
119+
> [!WARNING]
120+
> Depending on your version of Python, you may also need to import `asyncio` and override the event loop policy:
121+
>
122+
> :::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="import_async_bug_fix":::
123+
>
124+
125+
1. Create `ACCOUNT_NAME` and `ACCOUNT_KEY` variables. Store the `COSMOS_GREMLIN_ENDPOINT` and `COSMOS_GREMLIN_KEY` environment variables as the values for each respective variable.
126+
127+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="environment_variables":::
128+
129+
1. Use `Client` to connect using the account's credentials and the **GraphSON 2.0** serializer.
130+
131+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="authenticate_connect_client":::
132+
133+
### Create vertices
134+
135+
Now that the application is connected to the account, use the standard Gremlin syntax to create vertices.
136+
137+
1. Use `submit` to run a command server-side on the API for Gremlin account. Create a **product** vertex with the following properties:
138+
139+
| | Value |
140+
| --- | --- |
141+
| **label** | `product` |
142+
| **id** | `68719518371` |
143+
| **`name`** | `Kiama classic surfboard` |
144+
| **`price`** | `285.55` |
145+
| **`category`** | `surfboards` |
146+
147+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="create_vertices_1":::
148+
149+
1. Create a second **product** vertex with these properties:
150+
151+
| | Value |
152+
| --- | --- |
153+
| **label** | `product` |
154+
| **id** | `68719518403` |
155+
| **`name`** | `Montau Turtle Surfboard` |
156+
| **`price`** | `600.00` |
157+
| **`category`** | `surfboards` |
158+
159+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="create_vertices_2":::
160+
161+
1. Create a third **product** vertex with these properties:
162+
163+
| | Value |
164+
| --- | --- |
165+
| **label** | `product` |
166+
| **id** | `68719518409` |
167+
| **`name`** | `Bondi Twin Surfboard` |
168+
| **`price`** | `585.50` |
169+
| **`category`** | `surfboards` |
170+
171+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="create_vertices_3":::
172+
173+
### Create edges
174+
175+
Create edges using the Gremlin syntax to define relationships between vertices.
176+
177+
1. Create an edge from the `Montau Turtle Surfboard` product named **replaces** to the `Kiama classic surfboard` product.
178+
179+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="create_edges_1":::
180+
181+
> [!TIP]
182+
> This edge defintion uses the `g.V(['<partition-key>', '<id>'])` syntax. Alternatively, you can use `g.V('<id>').has('category', '<partition-key>')`.
183+
184+
1. Create another **replaces** edge from the same product to the `Bondi Twin Surfboard`.
185+
186+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="create_edges_2":::
187+
188+
### Query vertices &amp; edges
189+
190+
Use the Gremlin syntax to traverse the graph and discover relationships between vertices.
191+
192+
1. Traverse the graph and find all vertices that `Montau Turtle Surfboard` replaces.
193+
194+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="query_vertices_edges":::
195+
196+
1. Write to the console the result of this traversal.
197+
198+
:::code language="python" source="~/cosmos-db-apache-gremlin-python-samples/001-quickstart/app.py" id="output_vertices_edges":::
199+
200+
## Run the code
201+
202+
Validate that your application works as expected by running the application. The application should execute with no errors or warnings. The output of the application includes data about the created and queried items.
203+
204+
1. Open the terminal in the Python project folder.
205+
206+
1. Use `python <filename>` to run the application. Observe the output from the application.
207+
208+
```bash
209+
python app.py
210+
```
55211
56212
## Clean up resources
57213

0 commit comments

Comments
 (0)