管理连接配置文件

概览

在本部分中,您将学习如何使用 Datastream API 执行以下操作:

  • 为 Oracle 源数据库、源 MySQL 数据库、源 PostgreSQL 数据库、BigQuery 目标位置和 Cloud Storage 目标位置创建连接配置文件
  • 检索有关更新和删除连接配置文件的信息
  • 探索来源或目标连接配置文件的结构

您可以通过两种方式使用 Datastream API。您可以进行 REST API 调用,也可以使用 Google Cloud CLI (CLI)。

如需简要了解如何使用 gcloud 管理 Datastream 连接配置文件,请点击 Google Cloud SDK 文档

为源 Oracle 数据库创建连接配置文件

以下代码显示了一个请求,它为 Oracle 数据库创建一个连接配置文件,并将连接方法指定为使用静态 IP 地址(IP 许可名单)。

REST

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "[connection-profile-display-name]",
  "oracleProfile": {
    "hostname": "HOST_ID_ADDRESS",
    "port": PORT,
    "username": "USERNAME",
    "password": "PASSWORD",
    "databaseService": "DATABASE"
  },
  "staticServiceIpConnectivity": {}
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=myOracleCP
{
  "displayName": "my Oracle database",
  "oracleProfile": {
    "hostname": "1.2.3.4",
    "port": 1521,
    "username": "admin",
    "password": "12345"
    "databaseService": "ORCL",
  },
  "staticServiceIpConnectivity": {}
}

以下代码显示了一个请求,它为 Oracle 数据库创建一个连接配置文件,并将连接方法指定为专用连接(VPC 对等互连)。

此方法可在 Datastream 和源数据库(在 Google Cloud 内部或通过 VPN 或 Interconnect 连接的外部源)之间建立安全连接。此通信通过 VPC 对等互连连接进行。

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "[connection-profile-display-name]",
  "oracleProfile": {
    "hostname": "HOST_ID_ADDRESS",
    "port": PORT,
    "username": "USERNAME",
    "password": "PASSWORD",
    "databaseService": "DATABASE"
  },
  "privateConnectivity": {
    "privateConnection": "https://datastream.googleapis.com/v1/projects/
    PROJECT_ID/locations/LOCATION/privateConnections/
    [private-connectivity-configuration-id]"
  }
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=myOracleVPCPeeringCP
{
  "displayName": "my Oracle database",
  "oracleProfile": {
    "hostname": "1.2.3.4",
    "port": 1521,
    "username": "admin",
    "password": "12345"
    "databaseService": "ORCL",
  },
    "privateConnectivity": {
      "privateConnection": "https://datastream.googleapis.com/v1/projects/
       myProjectId/locations/us-central1/privateConnections/myPrivateConnection"
  }
}

创建连接配置文件后,您可以调用 connectionProfiles/get 方法来查看其相关信息。您将看到如下所示的输出:

{
  "name": "projects/projectId/location/us-central1/connectionProfiles/myOracleVPCPeeringCP",
  "createTime": "2019-12-22T16:17:37.159786963Z",
  "updateTime": "2019-12-22T16:17:37.159786963Z",
  "displayName": "my Oracle database",
  "oracleProfile": {
    "hostname": "1.2.3.4",
    "port": 1521,
    "databaseService": "ORCL",
    "username": "admin"
  },
  "privateConnectivity": {
    "privateConnection": "https://datastream.googleapis.com/v1/projects/
     myProjectId/locations/us-central1/privateConnections/myPrivateConnection"
  }
}

gcloud

如需详细了解如何使用 gcloud 为源 Oracle 数据库创建连接配置文件,请点击此处

为源 MySQL 数据库创建连接配置文件

以下代码显示了一个请求,它为 MySQL 数据库创建一个连接配置文件,并将连接方法指定为使用静态 IP 地址(IP 许可名单)。

REST

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "[connection-profile-display-name]",
  "mysqlProfile": {
    "hostname": "HOST_ID_ADDRESS",
    "port": PORT,
    "username": "USERNAME",
    "password": "PASSWORD"
  },
  "staticServiceIpConnectivity": {}
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=mysql-cp
{
  "displayName": "my MySQL database",
  "mysqlProfile": {
    "hostname": "1.2.3.4",
    "port": 3306,
    "username": "root",
    "password": "12345"
  },
  "staticServiceIpConnectivity": {}
}

gcloud

如需详细了解如何使用 gcloud 为源 MySQL 数据库创建连接配置文件,请点击此处

为源 PostgreSQL 数据库创建连接配置文件

以下代码展示了一个为 PostgreSQL 数据库创建连接配置文件的请求。

REST

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "[connection-profile-display-name]",
  "postgresqlProfile": {
    "hostname": "HOST_ID_ADDRESS",
    "port": PORT,
    "username": "USERNAME",
    "password": "PASSWORD",
    "database": "DATABASE"
  },
  "staticServiceIpConnectivity": {}
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=postgres-cp
{
  "displayName": "my PostgreSQL database",
  "postgresqlProfile": {
    "hostname": "1.2.3.4",
    "database": "postgres",
    "port": 5432,
    "username": "root",
    "password": "12345"
  },
  "staticServiceIpConnectivity": {}
}

gcloud

如需详细了解如何使用 gcloud 创建连接配置文件,请点击此处

为源 SQL Server 数据库创建连接配置文件

以下代码展示了一个为 SQL Server 数据库创建连接配置文件的请求,并将连接方法指定为使用静态 IP 地址(IP 许可名单)。

REST

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "[connection-profile-display-name]",
  "sqlserverProfile": {
    "hostname": "HOST_ID_ADDRESS",
    "port": PORT,
    "username": "USERNAME",
    "password": "PASSWORD",
    "database": "DATABASE"
  },
  "staticServiceIpConnectivity": {}
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=sqlserver-cp
{
  "displayName": "my SQL Server database",
  "mysqlProfile": {
    "hostname": "1.2.3.4",
    "port": 1433,
    "username": "root",
    "password": "12345"
    "database": "database1"
  },
  "staticServiceIpConnectivity": {}
}

Google Cloud CLI

如需详细了解如何使用 gcloud 创建连接配置文件,请参阅 Google Cloud SDK 文档

为 BigQuery 目标位置创建连接配置文件

以下代码展示了为 BigQuery 目标位置创建连接配置文件的请求。

REST

POST https://datastream.googleapis.com/v1/projects/[project-id/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "connection-profile-display-name",
  "bigqueryProfile":  {}
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=myBigqueryCP
{
  "displayName": "my BigQuery destination",
  "bigqueryProfile": {}
}

gcloud

如需详细了解如何使用 gcloud 创建连接配置文件,请点击此处

为 Cloud Storage 目标位置创建连接配置文件

以下代码显示了为 Cloud Storage 存储桶创建连接配置文件的请求。由于 Cloud Storage 与 Datastream 位于同一网络中,因此无需特殊连接。因此,未指定连接方法。

REST

POST https://datastream.googleapis.com/v1/projects/[project-id/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "connection-profile-display-name",
  "gcsProfile": {
    "bucketName": "bucket-name",
    "rootPath": "prefix-inside-bucket"
  }
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=myGcsCP
{
  "displayName": "my Cloud Storage bucket",
  "gcsProfile": {
    "bucketName": "myBucket",
    "rootPath": "prefix-inside-bucket"
  }
}

gcloud

如需详细了解如何使用 gcloud 为 Cloud Storage 创建连接配置文件,请参阅 Cloud SDK 文档

获取连接配置文件的相关信息

以下代码展示了一个请求,检索已创建的 Oracle 源数据库、源 MySQL 数据库、BigQuery 目标位置或 Cloud Storage 目标位置的连接配置文件的相关信息。

REST

GET https://datastream.googleapis.com/v1/projects/project-id/locations/
LOCATION/connectionProfiles/CONNECTION_PROFILE_ID

例如:

GET https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles/myOracleCP

gcloud

如需详细了解如何使用 gcloud 检索连接配置文件的相关信息,请点击此处

列出连接配置文件

以下代码显示了一个检索所有连接配置文件的相关信息的请求。

REST

GET https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles

gcloud

如需详细了解如何使用 gcloud 检索所有连接配置文件的相关信息,请点击此处

更新连接配置文件

以下代码显示了一个请求,它更改源数据库的现有连接配置文件的用户名和密码。

通过使用 updateMask 参数,使只有您指定的字段必须包含在请求正文中(例如,分别由 oracle_profile.usernameoracle_profile.password 表示的 usernamepassword 字段)。

REST

PATCH https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles/CONNECTION_PROFILE_ID?
updateMask=oracle_profile.username,oracle_profile.password
{
  "oracleProfile": {
    "username": "USERNAME",
    "password": "PASSWORD"
  }
}

例如:

PATCH https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles/myOracleCP?
updateMask=oracle_profile.username,oracle_profile.password
{
  "oracleProfile": {
    "username": "username",
    "password": "password"
  }
}

以下代码显示了一个更改分配给连接配置文件的专用连接配置的请求。此连接配置文件使用 VPC 对等互连网络连接方法。

在此示例中,您要将 new_private_connection 配置分配给连接配置文件。

PATCH -d {\"private_connectivity\":{\"private_connection_name\":\
"https://datastream.googleapis.com/v1/
projects/PROJECT_ID/locations/LOCATION/privateConnections/
new_private_connection\"}}" -H "Authorization: Bearer $TOKEN" -H
"Content-Type: application/json" https://datastream.googleapis.com/v1/
projects/PROJECT_ID/locations/LOCATION/connectionProfiles/
CONNECTION_PROFILE_ID?update_mask=private_connectivity.private_connection_name

例如:

PATCH -d {\"private_connectivity\":{\"private_connection_name\":\
"https://datastream.googleapis.com/v1/
projects/myProjectId/locations/us-central1/privateConnections/
new_private_connection\"}}" -H "Authorization: Bearer $TOKEN" -H
"Content-Type: application/json"
https://datastream.googleapis.com/v1/projects/
myProjectId/locations/us-central1/connectionProfiles/
myOracleVPCPeeringCP?update_mask=private_connectivity.private_connection_name

gcloud

如需详细了解如何使用 gcloud 更新连接配置文件,请点击此处

删除连接配置文件

以下代码显示了一个删除连接配置文件的请求。删除连接配置文件后,使用它的任何数据流都将失败。

REST

DELETE https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles/CONNECTION_PROFILE_ID

例如:

DELETE https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles/myOracleCP

gcloud

如需详细了解如何使用 gcloud 删除连接配置文件,请点击此处

了解源数据库的结构

使用 discoverConnectionProfile API 从来源中检索实体(例如架构和表)的列表以及与实体关联的元数据。

API 可以参数的形式接收现有连接配置文件的 ID 或完整的连接配置文件对象定义。函数可以返回单个级别(例如,数据库中的所有架构或架构中的所有表),或者以递归方式返回所有实体(例如架构、表和列)。

REST

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles:discoverConnectionProfile?CONNECTION_PROFILE_ID

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles:discoverConnectionProfile?myOracleCP

gcloud

如需详细了解如何使用 gcloud 发现源数据库的结构,请点击此处