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

Connecting to Teradata with R through DBI and ODBC

我使用 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 参数?无论哪一个它不填充,那里都会有什么?

通常,在包括 ODBC 连接在内的大多数 DB-API 中,serverhost 是同义关键字,您不会同时看到它们,而只会看到一个(当然有例外)。具体来说,根据 odbc documentationdbConnect 维护 optional 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 驱动程序。

...         Additional ODBC keywords, these will be joined with the other arguments to form the final connection string


并且来自 Teradata 16.20 的 ODBC 驱动程序 documentationDriverDBCName 必需的 关键字。DBCName 似乎给定 IP 地址或别名指示,与 serverhost 同义。

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")