Skip to content

Commit 4e772f1

Browse files
committed
Notebook for blog post
1 parent b076500 commit 4e772f1

File tree

1 file changed

+302
-0
lines changed

1 file changed

+302
-0
lines changed

examples/python-blog-post.ipynb

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "e57c4da7",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"# Change this if needed to point to your MarkLogic instance.\n",
11+
"base_url = \"http://localhost\"\n",
12+
"\n",
13+
"# The admin account that can be used to create a new user and role.\n",
14+
"adminUser = \"admin\"\n",
15+
"adminPassword = \"admin\"\n",
16+
"\n",
17+
"# The user to create which will be used for all of the examples.\n",
18+
"user = \"python-blog-user\"\n",
19+
"password = \"pyth0n\"\n",
20+
"role_name = \"python-blog-role\"\n",
21+
"\n",
22+
"import json\n",
23+
"import requests\n",
24+
"from requests.auth import HTTPDigestAuth\n",
25+
"\n",
26+
"admin_session = requests.Session()\n",
27+
"admin_session.auth = HTTPDigestAuth(adminUser, adminPassword)\n",
28+
"\n",
29+
"user_session = requests.Session()\n",
30+
"user_session.auth = HTTPDigestAuth(user, password)"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"id": "be67fafe",
37+
"metadata": {
38+
"scrolled": true
39+
},
40+
"outputs": [],
41+
"source": [
42+
"# Create a MarkLogic role that allows a user to run all of the examples below. \n",
43+
"# See https://docs.marklogic.com/guide/security/intro for more information on MarkLogic security.\n",
44+
"\n",
45+
"admin_session.post(\n",
46+
" f\"{base_url}:8002/manage/v2/roles\",\n",
47+
" headers={\"Content-type\": \"application/json\"},\n",
48+
" data=json.dumps({\n",
49+
" \"role-name\":role_name, \n",
50+
" \"role\":[\"tde-admin\", \"rest-evaluator\", \"qconsole-user\"], \n",
51+
" \"privilege\":[\n",
52+
" {\"privilege-name\":\"xdmp:document-get\", \"action\":\"http://marklogic.com/xdmp/privileges/xdmp-document-get\", \"kind\":\"execute\"}, \n",
53+
" {\"privilege-name\":\"unprotected-collections\", \"action\":\"http://marklogic.com/xdmp/privileges/unprotected-collections\", \"kind\":\"execute\"}, \n",
54+
" {\"privilege-name\":\"any-uri\", \"action\":\"http://marklogic.com/xdmp/privileges/any-uri\", \"kind\":\"execute\"}\n",
55+
" ]\n",
56+
" })\n",
57+
")"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": null,
63+
"id": "65ba6b95",
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"# Create a new MarkLogic user with the role that was just created.\n",
68+
"\n",
69+
"admin_session.post(\n",
70+
" f'{base_url}:8002/manage/v2/users',\n",
71+
" headers={'Content-type': 'application/json'},\n",
72+
" data=json.dumps({\"user-name\": user, \"password\": password, \"role\":[role_name]}),\n",
73+
")"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"id": "36d53116",
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"# Load 500 JSON employee documents into the Documents database.\n",
84+
"\n",
85+
"r = requests.get(\"https://raw.githubusercontent.com/marklogic/marklogic-spark-connector/master/src/test/resources/500-employees.json\")\n",
86+
"\n",
87+
"for employee in json.loads(r.text):\n",
88+
" user_session.put(\n",
89+
" f'{base_url}:8000/v1/documents',\n",
90+
" params={'uri': employee['uri'], 'collection': 'employee'},\n",
91+
" data=json.dumps(employee['value'])\n",
92+
" )\n",
93+
"\n",
94+
"print(\"Finished loading employees.\")"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"id": "b99041a8",
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"# Search all employee documents, printing out the response from MarkLogic.\n",
105+
"\n",
106+
"r = user_session.get(\n",
107+
" f'{base_url}:8000/v1/search',\n",
108+
" headers={'Accept': 'application/json'},\n",
109+
" params={'collection': 'employee'}\n",
110+
")\n",
111+
"print(json.dumps(json.loads(r.text), indent=2))"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": null,
117+
"id": "5e280dce",
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"# Search employee documents containing \"San Jose\" and verify only 1 result is returned.\n",
122+
"\n",
123+
"r = user_session.get(\n",
124+
" f'{base_url}:8000/v1/search',\n",
125+
" headers={'Accept': 'application/json'},\n",
126+
" params={'collection': 'employee', 'q': '\"San Jose\"'}\n",
127+
")\n",
128+
"results = json.loads(r.text)\n",
129+
"assert results['total'] == 1\n",
130+
"print(json.dumps(results, indent=2))"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": null,
136+
"id": "2c59059f",
137+
"metadata": {},
138+
"outputs": [],
139+
"source": [
140+
"# Update an employee to contain the phrase \"San Jose\".\n",
141+
"\n",
142+
"url = f'{base_url}:8000/v1/documents'\n",
143+
"uri = '/data/employees/093caccf-f7ed-4572-a8fa-6390caf4d20e.json'\n",
144+
"\n",
145+
"r = user_session.get(url, params={'uri': uri})\n",
146+
"doc = json.loads(r.text)\n",
147+
"doc['City'] = 'San Jose'\n",
148+
"doc['State'] = 'CA'\n",
149+
"user_session.put(url, params={'uri':uri}, data=json.dumps(doc))"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"id": "e7be39a6",
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"# Search for San Jose again and verify that 2 results are now returned.\n",
160+
"\n",
161+
"r = user_session.get(\n",
162+
" f'{base_url}:8000/v1/search',\n",
163+
" headers={'Accept': 'application/json'},\n",
164+
" params={'collection': 'employee', 'q': '\"San Jose\"'}\n",
165+
")\n",
166+
"results = json.loads(r.text)\n",
167+
"assert results['total'] == 2\n",
168+
"print(json.dumps(results, indent=2))"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": null,
174+
"id": "3cef35af",
175+
"metadata": {},
176+
"outputs": [],
177+
"source": [
178+
"# Load a TDE template into the Schemas database. \n",
179+
"# This creates a view projecting rows from the employee documents.\n",
180+
"# See https://docs.marklogic.com/guide/app-dev/TDE for more information.\n",
181+
"\n",
182+
"template = {\n",
183+
" \"template\": {\n",
184+
" \"context\": \"/\",\n",
185+
" \"collections\": [\"employee\"],\n",
186+
" \"rows\": [\n",
187+
" {\n",
188+
" \"schemaName\": \"example\",\n",
189+
" \"viewName\": \"employee\",\n",
190+
" \"columns\": [\n",
191+
" {\"name\": \"GivenName\", \"scalarType\": \"string\", \"val\": \"GivenName\"},\n",
192+
" {\"name\": \"Surname\", \"scalarType\": \"string\", \"val\": \"Surname\"},\n",
193+
" {\"name\": \"BaseSalary\", \"scalarType\": \"double\", \"val\": \"BaseSalary\"},\n",
194+
" {\"name\": \"City\", \"scalarType\": \"string\", \"val\": \"City\"},\n",
195+
" {\"name\": \"Department\", \"scalarType\": \"string\", \"val\": \"Department\"},\n",
196+
" ],\n",
197+
" }\n",
198+
" ],\n",
199+
" }\n",
200+
"}\n",
201+
"\n",
202+
"user_session.put(\n",
203+
" f'{base_url}:8000/v1/documents',\n",
204+
" data=json.dumps(template),\n",
205+
" headers={'Content-Type': 'application/json'},\n",
206+
" params={\n",
207+
" 'uri': '/employee-template.json',\n",
208+
" 'collection': 'http://marklogic.com/xdmp/tde',\n",
209+
" 'database': 'Schemas'\n",
210+
" }\n",
211+
")"
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": null,
217+
"id": "d1c4c057",
218+
"metadata": {},
219+
"outputs": [],
220+
"source": [
221+
"# Retrieve employees as JSON rows using an Optic query, printing the response from MarkLogic.\n",
222+
"# See https://docs.marklogic.com/11.0/guide/optic-guide/en/getting-started-with-optic.html for more information.\n",
223+
"\n",
224+
"optic_query = 'op.fromView(\"example\", \"employee\").where(op.eq(op.col(\"City\"), \"San Jose\"))'\n",
225+
"r = user_session.post(\n",
226+
" f'{base_url}:8000/v1/rows?column-types=header', \n",
227+
" headers={\n",
228+
" 'Content-type': 'application/vnd.marklogic.querydsl+javascript',\n",
229+
" 'Accept': 'application/json'\n",
230+
" },\n",
231+
" data=optic_query\n",
232+
")\n",
233+
"results = json.loads(r.text)\n",
234+
"print(json.dumps(results, indent=2))"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": null,
240+
"id": "f0debd3a",
241+
"metadata": {},
242+
"outputs": [],
243+
"source": [
244+
"# Retrieve employees as CSV data and create a pandas DataFrame.\n",
245+
"\n",
246+
"import pandas as pd\n",
247+
"import io\n",
248+
"\n",
249+
"query = 'op.fromView(\"example\", \"employee\", \"\").where(op.eq(op.col(\"City\"), \"San Jose\"))'\n",
250+
"r = user_session.post(\n",
251+
" f'{base_url}:8000/v1/rows', \n",
252+
" headers={\n",
253+
" 'Content-type': 'application/vnd.marklogic.querydsl+javascript',\n",
254+
" 'Accept': 'text/csv'\n",
255+
" },\n",
256+
" data=query\n",
257+
")\n",
258+
"\n",
259+
"df = pd.read_csv(io.StringIO(r.text))\n",
260+
"print(df.head())"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": null,
266+
"id": "d2b55d7d",
267+
"metadata": {},
268+
"outputs": [],
269+
"source": [
270+
"# Cleanup = delete the user and role and the 500 documents.\n",
271+
"\n",
272+
"admin_session.delete(f'{base_url}:8002/manage/v2/roles/{role_name}')\n",
273+
"admin_session.delete(f'{base_url}:8002/manage/v2/users/{user}')\n",
274+
"admin_session.delete(\n",
275+
" f'{base_url}:8000/v1/search',\n",
276+
" params={'collection': 'employee'}\n",
277+
")"
278+
]
279+
}
280+
],
281+
"metadata": {
282+
"kernelspec": {
283+
"display_name": "Python 3 (ipykernel)",
284+
"language": "python",
285+
"name": "python3"
286+
},
287+
"language_info": {
288+
"codemirror_mode": {
289+
"name": "ipython",
290+
"version": 3
291+
},
292+
"file_extension": ".py",
293+
"mimetype": "text/x-python",
294+
"name": "python",
295+
"nbconvert_exporter": "python",
296+
"pygments_lexer": "ipython3",
297+
"version": "3.11.3"
298+
}
299+
},
300+
"nbformat": 4,
301+
"nbformat_minor": 5
302+
}

0 commit comments

Comments
 (0)