151 lines
3.9 KiB
Plaintext
151 lines
3.9 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"#### Writing to MySQL\n",
|
||
|
"\n",
|
||
|
"1. Insure MySQL is actually installed on the system, \n",
|
||
|
"2. There is a database called demo created on the said system\n",
|
||
|
"\n",
|
||
|
"The cell below creates a dataframe that will be stored within postgreSQL"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"2.0.0\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"#\n",
|
||
|
"# Writing to PostgreSQL database\n",
|
||
|
"#\n",
|
||
|
"import transport\n",
|
||
|
"from transport import providers\n",
|
||
|
"import pandas as pd\n",
|
||
|
"_data = pd.DataFrame({\"name\":['James Bond','Steve Rogers','Steve Nyemba'],'age':[55,150,44]})\n",
|
||
|
"myw = transport.factory.instance(provider=providers.MYSQL,database='demo',table='friends',context='write',auth_file=\"/home/steve/auth-mysql.json\")\n",
|
||
|
"myw.write(_data,if_exists='replace') #-- default is append\n",
|
||
|
"print (transport.__version__)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"#### Reading from MySQL\n",
|
||
|
"\n",
|
||
|
"The cell below reads the data that has been written by the cell above and computes the average age within a MySQL (simple query). \n",
|
||
|
"\n",
|
||
|
"- Basic read of the designated table (friends) created above\n",
|
||
|
"- Execute an aggregate SQL against the table\n",
|
||
|
"\n",
|
||
|
"**NOTE**\n",
|
||
|
"\n",
|
||
|
"It is possible to use **transport.factory.instance** or **transport.instance** they are the same. It allows the maintainers to know that we used a factory design pattern."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
" name age\n",
|
||
|
"0 James Bond 55\n",
|
||
|
"1 Steve Rogers 150\n",
|
||
|
"2 Steve Nyemba 44\n",
|
||
|
"--------- STATISTICS ------------\n",
|
||
|
" _counts avg\n",
|
||
|
"0 3 83.0\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"\n",
|
||
|
"import transport\n",
|
||
|
"from transport import providers\n",
|
||
|
"myr = transport.instance(provider=providers.POSTGRESQL,database='demo',table='friends',auth_file='/home/steve/auth-mysql.json')\n",
|
||
|
"_df = myr.read()\n",
|
||
|
"_query = 'SELECT COUNT(*) _counts, AVG(age) from friends'\n",
|
||
|
"_sdf = myr.read(sql=_query)\n",
|
||
|
"print (_df)\n",
|
||
|
"print ('--------- STATISTICS ------------')\n",
|
||
|
"print (_sdf)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"The cell bellow show the content of an auth_file, in this case if the dataset/table in question is not to be shared then you can use auth_file with information associated with the parameters.\n",
|
||
|
"\n",
|
||
|
"**NOTE**:\n",
|
||
|
"\n",
|
||
|
"The auth_file is intended to be **JSON** formatted"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"{'host': 'klingon.io',\n",
|
||
|
" 'port': 3306,\n",
|
||
|
" 'username': 'me',\n",
|
||
|
" 'password': 'foobar',\n",
|
||
|
" 'database': 'demo',\n",
|
||
|
" 'table': 'friends'}"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"{\n",
|
||
|
" \"host\":\"klingon.io\",\"port\":3306,\"username\":\"me\",\"password\":\"foobar\",\n",
|
||
|
" \"database\":\"demo\",\"table\":\"friends\"\n",
|
||
|
"}"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3",
|
||
|
"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.9.7"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|