Connect to ORACLE via R, using the info in sql developer - r

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.

Related

Is it possible to install Rstudio server on Linux without root access?

I'm an undergraduate research assistant working on a Linux server without root privilege. I'm trying to install the Rstudio server but the Rstudio website only provides the installation method for sudoers. Is it possible to install it without root access? I'm asking because I'm really not sure if I could get access from the manager. Any help will be appreciated!
No, you can't install it without root access. But there are a couple of things you could do to piece together a solution. Here are two options:
Extract the server and run it directly
You have to be root to install packages, so you can't install the .deb/.rpm file yourself. However, you could extract the contents of the file to a directory inside your home directory and run RStudio Server from there, by executing the rserver program in a regular shell.
Note that this will probably require an afternoon of editing the rserver.conf file to tell it where to find the rest of the files in the installation (since it presumes they are installed in /usr/lib by default). You can get some inspiration for how to do this here: https://github.com/rstudio/rstudio/blob/master/src/cpp/conf/rserver-dev.conf
Run the desktop version and forward the graphics
The other route is to run RStudio Desktop on the server; we make several builds of RStudio Desktop that are installer-less and can just be unpacked into your home directory. Then run an X11 server on your own computer and an X11 client on the RStudio server, so that the RStudio Desktop instance appears on your computer instead of the server.
Yes, you can run rserver without root priveliges.
For RStudio 1.4 I patched the following line into src/cpp/core/LogOptions.cpp
const FilePath kDefaultLogPath = core::system::xdg::userDataDir().completePath("log");
Then you need to set the system environment variables to some location read-writeable for the user, like
RSTUDIO_CONFIG_DIR=$HOME/.config/rstudio
RSTUDIO_CONFIG_HOME=$HOME/.config/rstudio
RSTUDIO_DATA_HOME=$HOME/.local/share/rstudio
And start rserver with the option
--server-data-dir={directory writeable for user}
--server-pid-file={file-path creatable for user}
--database-config-file={config-file}
With these adjustments it runs for me when I start it as a simple user (no root privileges) with
rserver --auth-none=1 --www-frame-origin=same --www-port={port} --www-verify-user-agent=0 --server-data-dir={my-tmp-path} --server-pid-file={my-tmp-path}/rstudio.pid --database-config-file={my-tmp-path}/db.conf}
ATTENTION:
But be aware, that anyone who can reach your system and the specified port from the network has access to the running RStudio in his browser and therefore can run any command in the name of the user on your system now.

Determine where the driver information is stored for odbc

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.

Install R package on SQL Server 2016

How to install a particular R package on SQL Server 2016.Package like 'ggplot2'.There is the command in R is 'Install.Packages("ggplot2")'.But how we can install this package on SQL Server 2016.
Have a look at the MSDN documentation.
You can also look at a blog post I wrote about how to install Facebook Prophet on SQL Server R Services.
Hope this helps!
Niels
I kept getting errors regarding package management for SQL Server 2017 "Error: The package management feature is not enabled for the current user or not supported on SQL Server version 14".
What finally worked for me was to go to the R version installed with SQL, then install the R packages as I normally would through R, but for the System Library instead of the User Library:
install.packages("tidyverse", lib="C:\\Program Files\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\R_SERVICES\\library")
You need to run R as admin to execute the code above, otherwise you will get an error that the folder is not writeable.
If you run in R the command:
.libPaths()
it will show you the user path and the system path. The packages you install normally forces them to go to the user path. I suspect SQL Server can't find it if it's in the user path.

Reload Custom R Package from Source

I have created a custom package and would like to deploy it to a remote machine. Here is my current long workflow:
Create custom package 'my_package_0.1.0.tar.gz'
scp package to remote machine
create Remote session
install.packages("/path/to/my_package0.1.0.tar.gz")
library('my_package')
When others connect to the machine, they have to run install and library:
install.packages("/path/to/my_package0.1.0.tar.gz")
library('my_package')
Is there a way I can share a custom package and have the workflow be:
Create remote session
Load package with library('my_package')
Feedback in comments says the best practice is to install the package in a shared location.
Here is how you can find a good place to install the packages.
Running the following shows where libraries are loaded from
.libPaths()
# rserve2 rserve2 /opt/deployr/9.0.1/rserve/R
#root root /usr/lib64/microsoft-r/3.3/lib64/R/library
There are two locations the R server is looking for libraries. One is owned by root so we shouldn't deploy here. The other location rserve2 has owndership and looks promising. We should create a library subfolder to store the shared packages.
Based on this information, the work flow should be:
Create custom package 'my_package_0.1.0.tar.gz'
scp package to remote machine
create Remote session
install.packages("/path/to/my_package0.1.0.tar.gz", lib='/opt/deployr/9.0.1/rserve/R/library/')
library('my_package')
When others connect to the machine, they can load the shared library:
library('my_package')

AWS RedShift - .NET Core (ODBC Support?)

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.

Resources