File size: 5,556 Bytes
065fee7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Data Science Library Integrations"
]
},
{
"cell_type": "markdown",
"source": [
"# Overview\n",
"`redshift_connector` provides integration with [pandas](https://github.com/pandas-dev/pandas) and [numpy](https://github.com/numpy/numpy) to enable everyone who works with data a simple interface for reading and writing data to and from Amazon Redshift."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Retrieving data as a `pandas.DataFrame`\n",
"`redshift_connector`'s `fetch_dataframe()` method allows users to directly retrieve result sets as a `pandas.DataFrame`. An example workflow using this function is shown below"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"import pandas\n",
"import redshift_connector\n",
"with redshift_connector.connect(\n",
" host='examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com',\n",
" database='dev',\n",
" user='awsuser',\n",
" password='my_password'\n",
") as conn:\n",
" with conn.cursor() as cursor:\n",
" cursor.execute(\"create Temp table book(bookname varchar,author varchar)\")\n",
" cursor.executemany(\"insert into book (bookname, author) values (%s, %s)\",\n",
" [\n",
" ('One Hundred Years of Solitude', 'Gabriel García Márquez'),\n",
" ('A Brief History of Time', 'Stephen Hawking')\n",
"\n",
" ])\n",
" cursor.execute(\"select * from book\")\n",
" result: pandas.DataFrame = cursor.fetch_dataframe()\n",
" print(result)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Retrieving data as a `numpy.ndarray`\n",
"`redshift_connector`'s `fetch_numpy_array()` method allows users to directly retrieve result sets as a `numpy.ndarray`. An example workflow using this function is shown below"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"import numpy\n",
"import redshift_connector\n",
"with redshift_connector.connect(\n",
" host='examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com',\n",
" database='dev',\n",
" user='awsuser',\n",
" password='my_password'\n",
") as conn:\n",
" with conn.cursor() as cursor:\n",
" cursor.execute(\"create Temp table book(bookname varchar,author varchar)\")\n",
" cursor.executemany(\"insert into book (bookname, author) values (%s, %s)\",\n",
" [\n",
" ('One Hundred Years of Solitude', 'Gabriel García Márquez'),\n",
" ('A Brief History of Time', 'Stephen Hawking')\n",
"\n",
" ])\n",
" cursor.execute(\"select * from book\")\n",
" result: numpy.ndarray = cursor.fetch_numpy_array()\n",
" print(result)\n"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Inserting a `pandas.DataFrame` into an Amazon Redshift database table\n",
"`redshift_connector`'s `write_dataframe()` method allows users to directly insert a`pandas.DataFrame` to a database table. An example workflow using this function is shown below"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import redshift_connector\n",
"\n",
"df: pd.DataFrame = pd.DataFrame(\n",
" np.array(\n",
" [\n",
" [\"One Hundred Years of Solitude\", \"Gabriel García Márquez\"],\n",
" [\"A Brief History of Time\", \"Stephen Hawking\"],\n",
" ]\n",
" ),\n",
" columns=[\"bookname\", \"author\"],\n",
")\n",
"\n",
"with redshift_connector.connect(\n",
" host='examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com',\n",
" database='dev',\n",
" user='awsuser',\n",
" password='my_password'\n",
") as conn:\n",
" with conn.cursor() as cursor:\n",
" cursor.execute(\"create Temp table book(bookname varchar,author varchar)\")\n",
" cursor.write_dataframe(df, \"book\")\n",
" cursor.execute(\"select * from book;\")\n",
" result = cursor.fetchall()\n",
" print(result)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|