Created
September 20, 2023 01:24
-
-
Save zseta/4facbc32910dae724d4d8ff1662856cb to your computer and use it in GitHub Desktop.
Python meetup demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Basic usage" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"DEMO project used in this notebook: github.com/scylladb/care-pet" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from cassandra.cluster import Cluster" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<cassandra.cluster.Session at 0x7ffab0778be0>" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# basic example\n", | |
"cluster = Cluster(['172.25.0.3'])\n", | |
"session = cluster.connect()\n", | |
"\n", | |
"session" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[Row(owner_id=UUID('8c32db47-c776-44fe-a62e-b2031687c1be'), address='home', name='Martin Elliott')]\n" | |
] | |
} | |
], | |
"source": [ | |
"# query data\n", | |
"query = \"SELECT * FROM carepet.owner LIMIT 5\"\n", | |
"result = session.execute(query).all()\n", | |
"print(result)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"8c32db47-c776-44fe-a62e-b2031687c1be home Martin Elliott\n" | |
] | |
} | |
], | |
"source": [ | |
"for row in result:\n", | |
" print(row.owner_id, row.address, row.name)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<cassandra.cluster.ResultSet at 0x7ffab07780d0>" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# insert data\n", | |
"import uuid\n", | |
"\n", | |
"columns = \",\".join([\"owner_id\", \"address\", \"name\"])\n", | |
"new_owner_id = uuid.uuid4()\n", | |
"values = [new_owner_id, \"742 Evergreen Terrace, Springfield\", \"Homer Simpson\"]\n", | |
"insert_query = f\"\"\"\n", | |
" INSERT INTO carepet.owner ({ columns })\n", | |
" VALUES (%s, %s, %s);\n", | |
"\"\"\"\n", | |
"session.execute(insert_query, values)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"None\n" | |
] | |
} | |
], | |
"source": [ | |
"# check new inserted row\n", | |
"query = \"SELECT * FROM carepet.owner WHERE owner_id = %s;\"\n", | |
"params = [new_owner_id]\n", | |
"result = session.execute(query, params).one()\n", | |
"print(result)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<cassandra.cluster.ResultSet at 0x7ffacccd7c10>" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# delete data\n", | |
"query = \"DELETE FROM carepet.owner WHERE owner_id=%s\"\n", | |
"session.execute(query, [new_owner_id])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Execution profile" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT, ConsistencyLevel\n", | |
"from cassandra.query import dict_factory" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<cassandra.cluster.Session at 0x7ffab0619a50>" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# connect using execution profile\n", | |
"profile = ExecutionProfile(\n", | |
" consistency_level=ConsistencyLevel.LOCAL_QUORUM,\n", | |
" row_factory=dict_factory\n", | |
" )\n", | |
"\n", | |
"cluster = Cluster(['172.25.0.3'], execution_profiles={EXEC_PROFILE_DEFAULT: profile})\n", | |
"session = cluster.connect(keyspace=\"carepet\")\n", | |
"\n", | |
"session" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[{'owner_id': UUID('8c32db47-c776-44fe-a62e-b2031687c1be'), 'address': 'home', 'name': 'Martin Elliott'}]\n" | |
] | |
} | |
], | |
"source": [ | |
"# query result is a list of dictionaries\n", | |
"query = \"SELECT * FROM owner LIMIT 5\"\n", | |
"result = session.execute(query).all()\n", | |
"print(result)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "env", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.12" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment