Skip to content
This repository was archived by the owner on Jun 29, 2019. It is now read-only.

Commit 87b0939

Browse files
committed
2 parents 90b194b + be4c9c5 commit 87b0939

34 files changed

Lines changed: 15880 additions & 6352 deletions
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Demonstrate Basic Querying of DocumentDB\n",
8+
"\n",
9+
"### Prerequisite:\n",
10+
"1. Install DocumentDB Python SDK (pip install pydocumentdb)\n",
11+
"1. Create DocumentDB account and Document DB database from Azure portal\n",
12+
"1. Download \"DocumentDB Migration tool\" from [here](http://www.microsoft.com/downloads/details.aspx?FamilyID=cda7703a-2774-4c07-adcc-ad02ddc1a44d)\n",
13+
"1. Import JSON data (volcano data) stored on a local file into documentDB with following command parameters to the migration tool. You can also use the GUI tool and enter the source and target location parameters from below. \n",
14+
"\n",
15+
"<code>/s:JsonFile /s.Files:[JSON File Location] /t:DocumentDBBulk /t.ConnectionString:AccountEndpoint=https://[DocDBAccountName].documents.azure.com:443/;AccountKey=[[KEY];Database=volcano /t.Collection:volcano1</code>\n",
16+
"\n",
17+
"Copy of volcano data also be found on a blob: https://cahandson.blob.core.windows.net/samples/volcano.json\n",
18+
"\n",
19+
"Execute rest of the code. "
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 4,
25+
"metadata": {
26+
"collapsed": true
27+
},
28+
"outputs": [],
29+
"source": [
30+
"import pydocumentdb.documents as documents\n",
31+
"import pydocumentdb.document_client as document_client\n",
32+
"import pydocumentdb.errors as errors"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 5,
38+
"metadata": {
39+
"collapsed": true
40+
},
41+
"outputs": [],
42+
"source": [
43+
"#Doc DB access parameters. \n",
44+
"# You can find the DocDB Account name and \"Key\" on Azure Portal. \n",
45+
"# ReadyOnly key is adequate if you are not writing new records\n",
46+
"masterKey = 'ENTER DOC DB Master KEY'\n",
47+
"host = 'https://[ENTER DOCDB ACCOUNT NAME].documents.azure.com:443'\n",
48+
"db = u'volcano'\n",
49+
"collection = 'volcano1'"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 6,
55+
"metadata": {
56+
"collapsed": false
57+
},
58+
"outputs": [],
59+
"source": [
60+
"# client object is the main object to operate with Doc DB\n",
61+
"client = document_client.DocumentClient(host,{'masterKey': masterKey})"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": 7,
67+
"metadata": {
68+
"collapsed": false
69+
},
70+
"outputs": [],
71+
"source": [
72+
"# Get the pointer to the database you want\n",
73+
"database = next((data for data in client.ReadDatabases() if data['id'] == db))"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 8,
79+
"metadata": {
80+
"collapsed": false
81+
},
82+
"outputs": [],
83+
"source": [
84+
"# Get the link to the collection within the database\n",
85+
"coll = next((coll for coll in client.ReadCollections(database['_self']) if coll['id'] == collection))"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 9,
91+
"metadata": {
92+
"collapsed": false
93+
},
94+
"outputs": [],
95+
"source": [
96+
"# Use the Doc DB SQL like query language.\n",
97+
"# Cheat sheet for DocDB SQL: https://azure.microsoft.com/en-us/documentation/articles/documentdb-sql-query-cheat-sheet/\n",
98+
"\n",
99+
"# Query tries to get list of volcanoes within 300 kms from a given coordinates (Redmond, WA in this case)\n",
100+
"# Uses Geospatial Built-in functions ST_DISTANCE( point1, point2 )\n",
101+
"\n",
102+
"query = u'SELECT * \\\n",
103+
"FROM volcanoes v \\\n",
104+
"WHERE ST_DISTANCE(v.Location, { \\\n",
105+
"\t\"type\": \"Point\", \\\n",
106+
"\t\"coordinates\": [-122.19, 47.36] \\\n",
107+
"\t}) < 300 * 1000 \\\n",
108+
"AND v.Type = \"Stratovolcano\" \\\n",
109+
"AND v[\"Last Known Eruption\"] = \"Last known eruption from 1800-1899, inclusive\"'"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 10,
115+
"metadata": {
116+
"collapsed": false
117+
},
118+
"outputs": [
119+
{
120+
"data": {
121+
"text/plain": [
122+
"u'SELECT * FROM volcanoes v WHERE ST_DISTANCE(v.Location, { \\t\"type\": \"Point\", \\t\"coordinates\": [-122.19, 47.36] \\t}) < 300 * 1000 AND v.Type = \"Stratovolcano\" AND v[\"Last Known Eruption\"] = \"Last known eruption from 1800-1899, inclusive\"'"
123+
]
124+
},
125+
"execution_count": 10,
126+
"metadata": {},
127+
"output_type": "execute_result"
128+
}
129+
],
130+
"source": [
131+
"query"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": 11,
137+
"metadata": {
138+
"collapsed": false
139+
},
140+
"outputs": [],
141+
"source": [
142+
"# Run the query\n",
143+
"docs = list(client.QueryDocuments(coll['_self'], {'query': query, 'parameters':[]}))"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 12,
149+
"metadata": {
150+
"collapsed": false
151+
},
152+
"outputs": [
153+
{
154+
"data": {
155+
"text/plain": [
156+
"[{u'Country': u'United States',\n",
157+
" u'Elevation': 4392,\n",
158+
" u'Last Known Eruption': u'Last known eruption from 1800-1899, inclusive',\n",
159+
" u'Location': {u'coordinates': [-121.758, 46.87], u'type': u'Point'},\n",
160+
" u'Region': u'US-Washington',\n",
161+
" u'Status': u'Dendrochronology',\n",
162+
" u'Type': u'Stratovolcano',\n",
163+
" u'Volcano Name': u'Rainier',\n",
164+
" u'_attachments': u'attachments/',\n",
165+
" u'_etag': u'\"0000c501-0000-0000-0000-5696f9700000\"',\n",
166+
" u'_rid': u'+-hhAPg2OAC5AQAAAAAAAA==',\n",
167+
" u'_self': u'dbs/+-hhAA==/colls/+-hhAPg2OAA=/docs/+-hhAPg2OAC5AQAAAAAAAA==/',\n",
168+
" u'_ts': 1452734832,\n",
169+
" u'id': u'682fe1d3-1e2a-c135-d47f-f3351afd03e3'},\n",
170+
" {u'Country': u'United States',\n",
171+
" u'Elevation': 3426,\n",
172+
" u'Last Known Eruption': u'Last known eruption from 1800-1899, inclusive',\n",
173+
" u'Location': {u'coordinates': [-121.694, 45.374], u'type': u'Point'},\n",
174+
" u'Region': u'US-Oregon',\n",
175+
" u'Status': u'Historical',\n",
176+
" u'Type': u'Stratovolcano',\n",
177+
" u'Volcano Name': u'Hood',\n",
178+
" u'_attachments': u'attachments/',\n",
179+
" u'_etag': u'\"0000d202-0000-0000-0000-5696f9730000\"',\n",
180+
" u'_rid': u'+-hhAPg2OADGAgAAAAAAAA==',\n",
181+
" u'_self': u'dbs/+-hhAA==/colls/+-hhAPg2OAA=/docs/+-hhAPg2OADGAgAAAAAAAA==/',\n",
182+
" u'_ts': 1452734835,\n",
183+
" u'id': u'acf67a43-d1db-0d15-4b73-ffd267d6ac9f'},\n",
184+
" {u'Country': u'United States',\n",
185+
" u'Elevation': 3285,\n",
186+
" u'Last Known Eruption': u'Last known eruption from 1800-1899, inclusive',\n",
187+
" u'Location': {u'coordinates': [-121.82, 48.786], u'type': u'Point'},\n",
188+
" u'Region': u'US-Washington',\n",
189+
" u'Status': u'Historical',\n",
190+
" u'Type': u'Stratovolcano',\n",
191+
" u'Volcano Name': u'Baker',\n",
192+
" u'_attachments': u'attachments/',\n",
193+
" u'_etag': u'\"00004105-0000-0000-0000-5696f9770000\"',\n",
194+
" u'_rid': u'+-hhAPg2OAA1BQAAAAAAAA==',\n",
195+
" u'_self': u'dbs/+-hhAA==/colls/+-hhAPg2OAA=/docs/+-hhAPg2OAA1BQAAAAAAAA==/',\n",
196+
" u'_ts': 1452734839,\n",
197+
" u'id': u'1d1913ad-fff9-c2aa-2ff9-9f67deda3578'}]"
198+
]
199+
},
200+
"execution_count": 12,
201+
"metadata": {},
202+
"output_type": "execute_result"
203+
}
204+
],
205+
"source": [
206+
"docs"
207+
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": null,
212+
"metadata": {
213+
"collapsed": true
214+
},
215+
"outputs": [],
216+
"source": []
217+
}
218+
],
219+
"metadata": {
220+
"kernelspec": {
221+
"display_name": "Python 2",
222+
"language": "python",
223+
"name": "python2"
224+
},
225+
"language_info": {
226+
"codemirror_mode": {
227+
"name": "ipython",
228+
"version": 2
229+
},
230+
"file_extension": ".py",
231+
"mimetype": "text/x-python",
232+
"name": "python",
233+
"nbconvert_exporter": "python",
234+
"pygments_lexer": "ipython2",
235+
"version": "2.7.11"
236+
}
237+
},
238+
"nbformat": 4,
239+
"nbformat_minor": 0
240+
}

Data-Science-Virtual-Machine/Samples/Notebooks/IntroToJupyterPython.ipynb

Lines changed: 1697 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)