R - connecting to SQL Server using using RSQLServer - r

I want to use RSQLServer instead of RODBC to connect to a database called 'Mkt_DW'. I think my server hostname is my machine DHX number - that's what is returned when I query the hostname in SQL Server 2008 using:
SELECT HOST_NAME() AS HostName, SUSER_NAME() LoggedInUser
I then enter the following code into R Studio:
library(RSQLServer)
library(DBI)
driver <- dbDriver("SQLServer")
url <- "DHX32510;Database=Mkt_DW;Trusted_Connection=TRUE;"
conn <- dbConnect(driver, url)
I get the following error:
Error in rJava::.jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", url, :
java.sql.SQLException: Network error IOException: Connection refused: connect
Can anyone tell me what I'm doing wrong?
Thanks,
Neil

It is because it cannot find the 'sql.yaml' file:
See not from the package author:
"See ?SQLServer. It will look for the YAML file in the following location by default: Sys.getenv("HOME")"
https://github.com/imanuelcostigan/RSQLServer/issues/57

Related

Trouble accessing oracle database using R (odbc package)

A colleague at work is having trouble using the odbc package function. I am trying to find help.
He is using an oracle database using R instead of running our traditional SAS programs, but he has not been successful. We are trying to find out what is causing the error messages below. Can someone help?
Attempt 1:
#Get the Oracle JDBC driver
jdbcDriver =JDBC("oracle.jdbc.OracleDriver",
classPath="C:/instantclient_19_10/ojdbc8.jar")
Create connection string to the Database we want
connect.string <-
glue("jdbc:oracle:thin:#//{host}:{port}/{sid}",
host = "stdbprd01.states.bls.gov",
port = 1521,
sid = "lausonep")
print(connect.string)
#Establish connection to your database
con <- dbConnect(jdbcDriver,
connect.string,
user = "username",
password = rstudioapi::askForPassword("Database password"))
Error in .jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
java.sql.SQLRecoverableException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
Attempt 2:
library(odbc)
con <- DBI::dbConnect(odbc::odbc(),
driver="Oracle in OraClient12Home1",
database="lausprd",
uid="aakre_n",
pwd="!QAZ1qaz#WSX2wsx",
host="stdbprd01.states.bls.gov",
port=1521
)
Error: nanodbc/nanodbc.cpp:1021: IM006: [Oracle][ODBC][ORA]-12560: TNS:protocol adapter error
[Microsoft][ODBC Driver Manager] Driver’s SQLSetConnectAttr failed

How to resolve "Error: could not receive data from server" - database connection in r?

I am running into
Error: could not receive data from server: Software caused connection abort (0x00002745/10053)
upon trying to connect to a postgres database using the DBI package in R. Note that i am in a work environment, so subject to a corporate firewall. Can that explain the error or is there something else that could be happening?
Here is the code I'm using
# Connect to trayaway dev
con <- DBI::dbConnect(
RPostgres::Postgres(),
host = host, port = 5432, dbname = "postgres",
user = user, password = password
)
error below:
Error: could not receive data from server: Software caused connection abort (0x00002745/10053)
solution was found- i tried the same code using wiFi and the code works - when hardwired, connection string fails to connect to database - so this is a corporate firewall issue - thank you,

RODBC connection from R to SQL failing

I'm using R Studio to connect to SQL Server Management using library(RODBC).
Note that the below code works on my local system, but does not work when trying to connect remotely.
This is my connection code:
connection <- RODBC::odbcDriverConnect( 'driver={SQL Server};server=SERVERNAME ;database=DBNAME;trusted_connection=TRUE' )
I get the following warning message:
Warning messages:
1: In RODBC::odbcDriverConnect("driver={SQL Server};server=SERVERNAME;database=DBNAME;trusted_connection=TRUE") :
[RODBC] ERROR: state 08001, code 18, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]SSL Security error
2: In RODBC::odbcDriverConnect("driver={SQL Server};server=SERVERNAME;database=DBNAME;trusted_connection=TRUE") :
[RODBC] ERROR: state 01000, code 772, message [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (SECDoClientHandshake()).
3: In RODBC::odbcDriverConnect("driver={SQL Server};server=SERVERNAME;database=DBNAME;trusted_connection=TRUE") :
ODBC connection failed
It's really odd because I definitely have access to the server and database in question.
It's worth noting that I have access to the server from a local system (above code works) and access to a remote system (code does not work).
Any idea why this might be happening?
RODBC is not a great package, it would be much better to use the RStudio built odbc instead:
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "SERVER",
Database = "DATABASE",
Trusted_Connection = "True" ,
timeout = 5000
)
In the following command line
connection <- RODBC::odbcDriverConnect( 'driver={SQL Server};server=SERVERNAME ;database=DBNAME;trusted_connection=TRUE' )
make trusted_connection = FALSE or no. Its a simple case of SSL error. If you are using linux, for server, use ip address rather than hostname.

Error in connecting R to Redshift

I'm trying to connect RStudio to Amazon Redshift via JDBC and this is what I tried to run:
driver <- JDBC("com.amazon.redshift.jdbc42.Driver", "~/Downloads/RedshiftJDBC42-1.2.1.1001.jar", identifier.quote="`")
# url <- "<JDBCURL>:<PORT>/<DBNAME>?user=<USER>&password=<PW>
url <- "jdbc:redshift://<cluster-name>.<xxxxxx>.us-east-1.redshift.amazonaws.com:5439/<dbname>?user=<username>&password=<password>"
conn <- dbConnect(driver, url)
When executing dbConnect(), I get the following error:
Error in .jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
java.sql.SQLException: [Amazon](500150) Error setting/closing connection: Operation timed out.
Any idea what is causing this and how to fix it?
Update: There was a problem with access through security groups. If you're having a similar issue, check the inbound rules of your security group and make sure they allow access to Redshift via your IP.

RJDBC Error When Trying to Connect to Hive Server: org.apache.thrift.TApplicationException: Invalid method name: 'execute'

I have been trying to use RJDBC package to connect R(on local machine) with Hive(server), and am seeing errors:
Error in .jcall(drv#jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], :
java.sql.SQLException: org.apache.thrift.TApplicationException: Invalid method name: 'execute'
I directly copied the jars that's running on the server to my local machine, which then shouldn't be the problem with the driver versions. I also tried to use earlier versions of RJDBC package, but it didn't work, neither.
I would really appreciate any ideas/suggestions.
My script:
#
hive_connection <- function( hostname, port, lib_dir, hive_jars){
library(RJDBC)
library(DBI)
library(rJava)
library(Rserve)
# lib_dir: directory containing the jars & drivers
hive_class_path <- file.path( lib_dir, hive_jars )
drv <- JDBC( 'org.apache.hadoop.hive.jdbc.HiveDriver', classPath= hive_class_path, "`" )
server <- sprintf( 'jdbc:hive://%s:%s', hostname, port )
return ( dbConnect( drv, server, 'hive','hive') )
}
conn <- hive_connection('hostname',9083,'lib_dir', list.files('lib_dir'))
This is related to driver and port, i was facing same error while connecting hive with jdbc driver. finally i find out right driver and hive service with port. it worked fine.
Try
drv <- JDBC( 'org.apache.hadoop.hive.jdbc.HiveDriver',c(hive_class_path ,pattern="jar$" ,full.names=T) )
I resolved the same issue by following two steps -
Change 1:
drv <- JDBC( 'org.apache.hive.jdbc.HiveDriver', classPath= hive_class_path, "`" )
The change is in the driver, notice I took out .hadoop
Change 2:
server <- sprintf( 'jdbc:hive2://%s:%s', hostname, port )
I added "2" in the url for connection, to connect to hiveServer2.
I got the detailed explanation by reading this - http://jayunit100.blogspot.com/2013/12/the-anatomy-of-jdbc-connection-in-hive.html

Resources