通过DBI和ODBC使用R连接到Teradata

时间:2019-12-06 14:38:04

标签: r odbc teradata teradata-sql-assistant r-dbi

我使用 Teradata SQL Assistant 连接到Teradata。连接参数包括服务器地址和驱动程序(出于隐私原因更改了服务器信息),如下所示:

Name: my_teradata_connection
Teradata Server Info: 00.11.22.333
Data Source: Teradata Database ODBC Driver 16.20
UID: My_User_ID
PWD: My_PWD

我正在尝试使用DBIodbc软件包使用R连接到Teradata。

con <- DBI::dbConnect(odbc::odbc(),
                      Driver = "[your driver's name]",
                      Host   = "[your server's path]",
                      DBCName = "[IP Address or Alias]"
                      UID    = rstudioapi::askForPassword("Database user"),
                      PWD    = rstudioapi::askForPassword("Database password"))

显然Driver应该是Teradata Database ODBC Driver 16.20。但是我应该把Teradata Server Info放在哪里00.11.22.333放在哪里?它应该填充Host还是DBCName参数?不管它不填充哪一个,那里都会有什么?

1 个答案:

答案 0 :(得分:2)

通常,在包括ODBC连接在内的大多数DB-API中, server host 是同义词,在其中您不会同时看到这两个关键字(当然也有例外)。具体来说,对于每个odbc documentationdbConnect保持可选 server参数:

dbConnect(
     drv,
     dsn = NULL,
     ...,
     timezone = "UTC",
     timezone_out = "UTC",
     encoding = "",
     bigint = c("integer64", "integer", "numeric", "character"),
     timeout = 10,
     driver = NULL,
     server = NULL,
     database = NULL,
     uid = NULL,
     pwd = NULL,
     dbms.name = NULL,
     .connection_string = NULL
)

但是,...表示特定于相应驱动程序的其他ODBC驱动程序关键字,这里是Terdata ODBC驱动程序。

  

...附加的ODBC关键字,这些关键字将与其他参数一起加入   形成最终的连接字符串


并且从用于Teradata 16.20的ODBC驱动程序中,documentationDriverDBCName必需的关键字。DBCName似乎与服务器主机给出IP地址或别名指示。

  

DBCName = <IP-addr-or-alias>

| Keyword/Synonym                                             | Description                                                                             |
|-------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| Driver=<driver-path>                                        | [Required] The full path to the ODBC Driver for Teradata shared objects…                |
| Description=<data-source-desc>                              | [Optional] Descriptive text about the data source.                                      |
| DBCName=<IP-addr-or-alias>                                  | [Required] The IP address or FQDN (fully qualified domain name) of the Teradata server… |
| Username=<name> or UID=<name>                               | [Optional] The default username for logging onto a Teradata server system.              |
| Password=<password>                                         | [Optional] The password required for the default Username.                              |
| DefaultDatabase=<database-name> Or Database=<database-name> | [Optional] The default database associated with the specified data source…              |
| UPTMode                                                     | Default = NOTSET…                                                                       |

因此,在R中,仅使用DBCName。下面添加了可选的Database关键字。

# KEYWORD APPROACH
con <- DBI::dbConnect(odbc::odbc(),
                      Driver   = "Teradata Database ODBC Driver 16.20",
                      DBCName  = "00.11.22.333",
                      Database = "myDatabase",
                      UID      = rstudioapi::askForPassword("Database user"),
                      PWD      = rstudioapi::askForPassword("Database password"))

# CONNECTION STRING APPROACH
con_str = "Driver={Teradata Database ODBC Driver 16.20};DBCName=00.11.22.333;Database=myDatabase;"

con <- DBI::dbConnect(odbc::odbc(),
                      .connection_string = con_str,
                      UID = rstudioapi::askForPassword("Database user"),
                      PWD = rstudioapi::askForPassword("Database password")