How can I connect and run queries against AWS RedShift using .NET Core? Code sample please. I have gone through the AWS docs and .Net Core docs but no luck.
This answer is one for a particular point in time and won't age well...
The EntityFramework Core project is the one I'd keep the closest eye on. The lack of ODBC is well known, especially for those who want to connect to Oracle. For the time being, you may need to fork an Oracle client for .NET core and make modifications as necessary.
I found these projects after a quick Google search that may be able to help you for now...
- https://github.com/LinqDan/oracleclientcore
- https://github.com/LinqDan/Mono.Data.OdbcCore
Longer term, you'll want to keep an eye on these two GitHub issues which are tracking it for EntityFramework Core and the .NETStandard APIs..
https://github.com/dotnet/corefx/issues/13035
https://github.com/aspnet/EntityFramework/issues/7432
Update 6/23/2017:
This is now possible through the Npgsql.EntityFrameworkCore.PostgreSQL NuGet package and associated Entity Framework Core packages. Apparently the PostgreSQL team was tired of waiting around for ODBC support (which still isn't available in the latest netstandard2.0 yet) and wrote their own driver using netstandard - back in the November timeframe. The getting started page on the npgsql website covers it's usage in the old JSON project format - but the dependencies listed are still valid.
Here is how you would use the package...
using (var conn = new NpgsqlConnection("Host=myserver;Username=mylogin;Password=******;Database=music"))
{
conn.Open();
using (var cmd = new NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "SELECT name FROM artists";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
}
}
}
One thing to keep in mind when using this driver - it's written for PostgreSQL, not Redshift. While Redshift is based on PostgreSQL, it's underlying engine is much more like Cassandra than anything else. As a result, Amazon had to make some choices in the development to drop certain things that PostgreSQL does support - such as SQL variables. Because of this, you will have a fairly limiting experience for certain things that you might be used to in other Entity Framework implementations. As long as you stay with using the direct access *Connection, *Command, and DataReader classes and write your own SQL, you should be fine.
At the time of writing this answer there are ODBC drivers readily available for Redshift.
For Windows, you can find the "Amazon Redshift" driver installation file here:
https://docs.aws.amazon.com/redshift/latest/mgmt/install-odbc-driver-windows.html
This will need to be installed on the machine (or machines) that run the application required to connect to Redshift. For automated deployments, it would be a good idea to have a powershell script (or MSBuild step) that checks if it is installed on the production machine and install it if it doesn't exist.
For Linux, the driver can be found here:
https://docs.aws.amazon.com/redshift/latest/mgmt/install-odbc-driver-linux.html
Since most of the world of .NET Core is moving towards using Linux docker containers I will elaborate further on how to set this up in a docker environment using a Dockerfile.
First you will need to create a shell script (e.g. install-driver.sh) that does an apt-get for the relevant odbc driver you want from the link above.
Something like this:
#!/bin/bash
# Install the Redshift ODBC driver manager
apt-get update \
&& apt-get install -y --no-install-recommends unixodbc
if ! curl -s https://s3.amazonaws.com/redshift-downloads/drivers/odbc/[latestversion].deb -o driver.deb; then
echo 'Failed to download Redshift ODBC Driver!' 1>&2
exit 1
fi
# Install the Redshift ODBC driver
apt install ./driver.deb
There are also three files required for configuring the Amazon Redshift ODBC driver on Linux: amazon.redshiftodbc.ini, odbc.ini, and odbcinst.ini. This is explained here:
https://docs.aws.amazon.com/redshift/latest/mgmt/odbc-driver-configure-linux-mac.html
As mentioned in that link, you will have to add the following lines to your .sh file:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export ODBCINI=/etc/odbc.ini
export AMAZONREDSHIFTODBCINI=/etc/amazon.redshiftodbc.ini
export ODBCSYSINI=/usr/local/odbc
Once you have created the .sh and .ini files put them into a common directory (e.g. /odbc/) and include them as part of your Dockerfile commands. Something like this:
FROM microsoft/dotnet:2.1-sdk AS base
WORKDIR /app # or wherever your working directory resides
COPY ./odbc/odbc.ini /etc/odbc.ini
COPY ./odbc/odbcinst.ini /etc/odbcinst.ini
COPY ./odbc/amazon.redshiftodbc.ini /etc/amazon.redshiftodbc.ini
COPY ./odbc/install-driver.sh /tmp/install-driver.sh
RUN chmod +x /tmp/install-driver.sh
RUN /tmp/install-driver.sh
# dotnet restore, build, publish and ENTRYPOINT commands here...
That should be enough for you to run the docker build and docker run commands with the Redshift driver installed within your docker container.
It means that your application code can use OdbcConnection just like any other database driver:
using System;
using System.Data;
using System.Data.Odbc;
// field within class... value should be set from config
private string redshiftConnectionString = "Driver={Amazon Redshift (x64)}; Server=redshiftclusterhostname.region.redshift.amazonaws.com; Database=database; UID=user; PWD=password; Port=5439"
public DataSet ExecuteQuery(string query)
{
var dataSet = new DataSet();
using (var connection = new OdbcConnection(this.redshiftConnectionString))
{
var adapter = new OdbcDataAdapter(query, connection);
adapter.Fill(dataSet);
}
return dataSet;
}
Take note that the Driver= value should be the name of the driver you chose to download and install.
Simply connect to Amazon Redshift via a JDBC or ODBC Driver and access it like a normal SQL database.
You'll use the AWS API to start/stop the cluster, but all queries and requests go via the SQL connection.
Related
I want to use pyodbc in aws qlue python shell but it require odbc driver. Currently I get error like "Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)"
Is there any way to install odbc driver into glue
I wanted to do the same, but there is no straight forward way it seems. I guess, you could do it by adding a driver to your self-built Python .wheel or do some kind of run-time downloading of a driver etc.
I can offer a simpler alternative though:
pymssql does exactly this for you. It's a Python package that comes with the FreeTDS mssql odbc driver included. So it's just a pip install pymssql to get you started. I've tested it successfully on a Glue Pythonshell Job. You'll just need to add the package to the --additional-python-modules parameter of your job, so that is becomes available. Keep in mind, that you might still need to create a Glue Connection and add it to your Job. Even though you will not use the connection directly, you'll need it for the network connectivity from within your Job to your DB-Server.
I run BlacBox Component Builder under Wine/Ubuntu. I need to set up ODBC in order to access databases (PostgresQL in my case) in BBCB programs (Component Pascal). They have a unique and wonderful SQL access library that allows to accept SELECT results directly into a record variable (analogous to structures in the C world or class instances in C++). Such record is called an interactor. No preprocessor is used, and query parameters are taken directly out of program variables (Like so: "SELECT * FROM users WHERE name = :u.name AND age < :maxage", where u is a program variable of type RECORD and .name is it's field, maxage is also a program variable). Anyway, very convenient from a programmer's perspective.
However, BBCB does not have any native DB drivers for Linux, so I have to use the Windows BBCB version in Wine. Some years ago, I was able to set up unixodbc in Ubuntu, and unixodbc's DSNs became immediately available in Wine. Not this time around.
So, the question is, How do I set up ODBC in Ubuntu so that it's DSNs would be visible in Wine?
I found the trick!
BBCB is a 32-bit app, so obviousely it makes calls to 32-bit WINE/winapi functions, and they seemingly can't talk to the unixodbc/64bit.
The trick was to install :i386 versions of unixodbc and the DB odbc driver, then create a 32-bit wine prefix, and then run my app from there. In greater detail:
If not already done,
dpkg --add-architecture i386
sudo apt install unixodbc:i386
install database driver for odbc for your database server; in my case,
sudo apt install postgresql-odbc:i386
Assuming ~/.wine32 does not exist:
WINEPREFIX=~/.wine32 WINEARCH=win32 wine wineboot
(some manuals insist you have to have a wine 64-bit prefix before this)
Now copy BlackBox from it's previous location, or install anew, into the ~/.wine32 prefix, and from now on run BB as follows:
WINEPREFIX=~/.wine32 WINEARCH=win32 <program.exe-file-location>
Now BB's SqlOdbc module can perfectly see the DSNs I set up in unixodbc.
I got my connection from R to a MS SQL Server database working on a Mac. I was able to get the right driver for the connection string by issuing the odbcListDrivers() command. However, I don't know if it was there or not before I started installing things like freetds and brew install --no-sandbox msodbcsql17 mssql-tools.
What I'd like to know is: with the odbcListDrivers(), how do I know what the file is it pulls from (a .ini one, I'm thinking)? I struggled because the configuration files I configured for freetds and another odbc.ini file don't affect what R is reading I believe, and made troubleshooting difficult.
In case I need to configure more drivers or DSN (?) entries.
I am working on a machine without admin rights. I use sql developer to connect to an internal database. I would like to connect via R also.
Is there any way I can do this, without admin rights? Some solutions require me to set up a systemDNS - which I can not do.
Other requires me to install jvm.dll
My environment: Windows7, sqldeveloper, connection method is via TNS file.
Connecting to SQL Developer via R is far more difficult than other databases I've encountered. It's important that you have jdbc6.jar installed on your machine, and that you know the file path to where it was installed. Installing the jar file does not require admin rights. You can install the jar file from Oracle's website.
I use the RJDBC package to connect like so:
library(RJDBC)
jdbcDriver <- JDBC("oracle.jdbc.OracleDriver", classPath = "file path to where ojdbc6.jar is installed on your computer")
jdbcConnection <- dbConnect(jdbcDriver, "jdbc:oracle:thin:#YOUR_SERVER","YOUR_USERNAME","YOUR_PASSWORD")
You can then test the connection with a number of commands; I typically use:
dbListTables(jdbcConnection)
Another favorite of mine is to use dbplyr for dplyr-like functions when working with databases:
library(dbplyr)
tbl(jdbcConnection, "SAMPLE_TABLE_NAME")
The resulting output will be the data from the queried table in tibble form.
You can set the environment variables in your R session.
Sys.setenv(OCI_LIB64="/Path/to/instantclient",OCI_INC="/Path/to/instantclient/sdk/include")
You can put this in the file .Rprofile in your home directory, and RStudio will run it each time you begin a new session. Once you have this in .Rprofile you should be able to install ROracle.
I tried to run the Query from Oracle Database using jybot option as i have some java custom code, that need in my test script along with query from oracle database.
I got the error cx_Oracle is not found.
When run the same using pybot (removed the java custom code) then i didn't get any errors.
If i run the java custom code alone using jybot then also no errors.
Steps i followed to install Database Library are as follows:
Install Visual C++ Compiler
Download and Install VCForPython27.msi (If not have the executable file, please check Important URLs section for link to download)
Set Environment Variables ORACLE_HOME, TNS_ADMIN if not setup already.
Install Database Library
Open the command prompt and run the following command
pip install robotframework-databaselibrary
Install cx_Oracle
Open the command prompt and run the following command
pip install cx_Oracle
Connect to Oracle Database Code:
Connect To Database Using Custom Params cx_Oracle '${Username}/${Password}#${Host}:${Port}/${DatabaseName}'
#{Numbers} Query SELECT NUMBER_V from MASTER_DB WHERE STATUS_V='F' and ROWNUM <= 10
Log ${Numbers[0]}
Any one, please help me how to run the query from oracle database using jybot.
Thanks
Sarada
cx_Oracle can't be sucessfully used under the jybot
Here is my solution:
Install DatabaseLibrary module by
pip install robotframework-databaselibrary
Install JayDeBeApi module by
pip install JayDeBeApi
now you can put these libraries anywhere you like - just copy them from Python\Lib\site-packages
And here is going the trick!
The DatabaseLibrary usage have following sample for jaydebeapi connection:
Connect To Database Using Custom Params | JayDeBeApi | 'oracle.jdbc.driver.OracleDriver', 'my_db_test', 'system', 's3cr3t'
However this is wrong! You have to use the brackets over the username-pass pair! Like this:
Connect To Database Using Custom Params | JayDeBeApi | 'oracle.jdbc.driver.OracleDriver', 'my_db_test', ['system', 's3cr3t']
And please do not forget to use URL connection instead of my_db_test! It should be like this:
jdbc:oracle:thin:#//127.0.0.1:1521/my_db_test
Regards