|
6 | 6 | "source": [ |
7 | 7 | "[](https://github.com/awslabs/aws-data-wrangler)\n", |
8 | 8 | "\n", |
9 | | - "# 7 - Databases (Redshift, MySQL and PostgreSQL)\n", |
| 9 | + "# 7 - Redshift, MySQL and PostgreSQL\n", |
10 | 10 | "\n", |
11 | | - "[Wrangler](https://github.com/awslabs/aws-data-wrangler)'s Database module (`wr.db.*`) has two mainly functions that tries to follow the Pandas conventions, but add more data type consistency.\n", |
| 11 | + "[Wrangler](https://github.com/awslabs/aws-data-wrangler)'s Redshift, MySQL and PostgreSQL have two basic function in common that tries to follow the Pandas conventions, but add more data type consistency.\n", |
12 | 12 | "\n", |
13 | | - "- [wr.db.to_sql()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.db.to_sql.html#awswrangler.db.to_sql)\n", |
14 | | - "\n", |
15 | | - "- [wr.db.read_sql_query()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.db.read_sql_query.html#awswrangler.db.read_sql_query)" |
| 13 | + "- [wr.redshift.to_sql()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.redshift.to_sql.html)\n", |
| 14 | + "- [wr.redshift.read_sql_query()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.redshift.read_sql_query.html)\n", |
| 15 | + "- [wr.mysql.to_sql()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.mysql.to_sql.html)\n", |
| 16 | + "- [wr.mysql.read_sql_query()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.mysql.read_sql_query.html)\n", |
| 17 | + "- [wr.postgresql.to_sql()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.postgresql.to_sql.html)\n", |
| 18 | + "- [wr.postgresql.read_sql_query()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.postgresql.read_sql_query.html)" |
16 | 19 | ] |
17 | 20 | }, |
18 | 21 | { |
|
34 | 37 | "cell_type": "markdown", |
35 | 38 | "metadata": {}, |
36 | 39 | "source": [ |
37 | | - "### Creating an engine (SQLAlchemy Engine)\n", |
38 | | - "\n", |
39 | | - "The Wrangler offers basically three diffent ways to create a SQLAlchemy engine.\n", |
40 | | - "\n", |
41 | | - "1 - [wr.catalog.get_engine()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.catalog.get_engine.html#awswrangler.catalog.get_engine): Get the engine from a Glue Catalog Connection.\n", |
| 40 | + "## Connect throught Glue Catalog Connections\n", |
42 | 41 | "\n", |
43 | | - "2 - [wr.db.get_engine()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.db.get_engine.html#awswrangler.db.get_engine): Get the engine from primitives values (host, user, password, etc).\n", |
44 | | - "\n", |
45 | | - "3 - [wr.db.get_redshift_temp_engine()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.db.get_redshift_temp_engine.html#awswrangler.db.get_redshift_temp_engine): Get redshift engine with temporary credentials. " |
| 42 | + "- [wr.redshift.connect()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.redshift.connect.html)\n", |
| 43 | + "- [wr.mysql.connect()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.mysql.connect.html)\n", |
| 44 | + "- [wr.postgresql.connect()](https://aws-data-wrangler.readthedocs.io/en/stable/stubs/awswrangler.postgresql.connect.html)" |
46 | 45 | ] |
47 | 46 | }, |
48 | 47 | { |
|
51 | 50 | "metadata": {}, |
52 | 51 | "outputs": [], |
53 | 52 | "source": [ |
54 | | - "eng_postgresql = wr.catalog.get_engine(\"aws-data-wrangler-postgresql\")\n", |
55 | | - "eng_mysql = wr.catalog.get_engine(\"aws-data-wrangler-mysql\")\n", |
56 | | - "eng_redshift = wr.catalog.get_engine(\"aws-data-wrangler-redshift\")" |
| 53 | + "con_redshift = wr.redshift.connect(\"aws-data-wrangler-redshift\")\n", |
| 54 | + "con_mysql = wr.mysql.connect(\"aws-data-wrangler-mysql\")\n", |
| 55 | + "con_postgresql = wr.postgresql.connect(\"aws-data-wrangler-postgresql\")" |
57 | 56 | ] |
58 | 57 | }, |
59 | 58 | { |
|
72 | 71 | "name": "stdout", |
73 | 72 | "output_type": "stream", |
74 | 73 | "text": [ |
75 | | - "(1,)\n" |
| 74 | + "[1]\n" |
76 | 75 | ] |
77 | 76 | } |
78 | 77 | ], |
79 | 78 | "source": [ |
80 | | - "with eng_postgresql.connect() as con:\n", |
81 | | - " for row in con.execute(\"SELECT 1\"):\n", |
| 79 | + "with con_redshift.cursor() as cursor:\n", |
| 80 | + " for row in cursor.execute(\"SELECT 1\"):\n", |
82 | 81 | " print(row)" |
83 | 82 | ] |
84 | 83 | }, |
|
95 | 94 | "metadata": {}, |
96 | 95 | "outputs": [], |
97 | 96 | "source": [ |
98 | | - "wr.db.to_sql(df, eng_postgresql, schema=\"public\", name=\"tutorial\", if_exists=\"replace\", index=False) # PostgreSQL\n", |
99 | | - "wr.db.to_sql(df, eng_mysql, schema=\"test\", name=\"tutorial\", if_exists=\"replace\", index=False) # MySQL\n", |
100 | | - "wr.db.to_sql(df, eng_redshift, schema=\"public\", name=\"tutorial\", if_exists=\"replace\", index=False) # Redshift" |
| 97 | + "wr.redshift.to_sql(df, con_redshift, schema=\"public\", table=\"tutorial\", mode=\"overwrite\")\n", |
| 98 | + "wr.mysql.to_sql(df, con_mysql, schema=\"test\", table=\"tutorial\", mode=\"overwrite\")\n", |
| 99 | + "wr.postgresql.to_sql(df, con_postgresql, schema=\"public\", table=\"tutorial\", mode=\"overwrite\")" |
101 | 100 | ] |
102 | 101 | }, |
103 | 102 | { |
|
164 | 163 | } |
165 | 164 | ], |
166 | 165 | "source": [ |
167 | | - "wr.db.read_sql_query(\"SELECT * FROM public.tutorial\", con=eng_postgresql) # PostgreSQL\n", |
168 | | - "wr.db.read_sql_query(\"SELECT * FROM test.tutorial\", con=eng_mysql) # MySQL\n", |
169 | | - "wr.db.read_sql_query(\"SELECT * FROM public.tutorial\", con=eng_redshift) # Redshift" |
| 166 | + "wr.redshift.read_sql_query(\"SELECT * FROM public.tutorial\", con=con_redshift)\n", |
| 167 | + "wr.mysql.read_sql_query(\"SELECT * FROM test.tutorial\", con=con_mysql)\n", |
| 168 | + "wr.postgresql.read_sql_query(\"SELECT * FROM public.tutorial\", con=con_postgresql)" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": 6, |
| 174 | + "metadata": {}, |
| 175 | + "outputs": [], |
| 176 | + "source": [ |
| 177 | + "con_redshift.close()\n", |
| 178 | + "con_mysql.close()\n", |
| 179 | + "con_postgresql.close()" |
170 | 180 | ] |
171 | 181 | } |
172 | 182 | ], |
|
0 commit comments