管理連線設定檔

本頁面說明如何使用 Datastream API 執行下列操作:

  • 為來源 Oracle 資料庫、來源 MySQL 資料庫、來源 PostgreSQL 資料庫、來源 Salesforce 機構、BigQuery 目的地和 Cloud Storage 目的地建立連線設定檔
  • 擷取、更新及刪除連線設定檔的相關資訊
  • 探索來源或目的地連線設定檔的結構

使用 Datastream API 的方法有兩種。您可以發出 REST API 呼叫,也可以使用 Google Cloud CLI (CLI)。

如要瞭解如何使用 gcloud 管理 Datastream 連線設定檔,請按一下「Google Cloud SDK 說明文件」

為來源 Oracle 資料庫建立連線設定檔

下列程式碼顯示使用密鑰和 IP 允許清單連線方法,為 Oracle 資料庫建立連線設定檔的要求。

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",
    "databaseService": "DATABASE"
  },
  "secretManagerStoredPassword": "SECRET_MANAGER_RESOURCE",
  "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",
    "databaseService": "ORCL",
  },
  "secretManagerStoredPassword": "projects/myProjectId/secrets/mySecret/versions/latest",
  "staticServiceIpConnectivity": {}
}

下列程式碼顯示為 Oracle 資料庫建立連線設定檔的要求,並將連線方法指定為私人連線 (虛擬私有雲對等互連)。

這個方法會在 Datastream 與來源資料庫之間建立安全連線 (內部 Google Cloud,或透過 VPN 或互連網路連線的外部來源)。這類通訊會出現在 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 資料庫建立連線設定檔,請參閱 Google Cloud SDK 說明文件

為來源 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 資料庫建立連線設定檔,請參閱 Google Cloud SDK 說明文件

為來源 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 建立連線設定檔,請參閱 Google Cloud SDK 說明文件

為來源 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": {}
}

gcloud

如要進一步瞭解如何使用 gcloud 建立連線設定檔,請參閱 Google Cloud SDK 說明文件

為 Salesforce 來源建立連線設定檔

下列程式碼顯示要求,使用使用者名稱和密碼做為驗證方法,為來源 Salesforce 執行個體建立連線設定檔。

REST

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "CONNECTION_PROFILE_NAME",
  "salesforceProfile": {
    "domain": "DOMAIN_NAME",
    "userCredentials": {
      "username": "USERNAME",
      "password": "PASSWORD",
      "securitytoken": "SECURITY_TOKEN"
    }
  }
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=saleforce-cp
{
  "displayName": "My Salesforce connection profile",
  "salesforceProfile": {
    "domain": "domain.my.salesforce.com",
    "userCredentials": {
      "username": "mySalesforceUser",
      "password": "12345",
      "securitytoken": "C08120F510542FFB1C3640F57AF19E2D5D700556A25F7D665C3B428407709D8C"
    }
  }
}
  

gcloud

如要進一步瞭解如何使用 gcloud 建立連線設定檔,請參閱 Google Cloud SDK 說明文件

下列程式碼顯示要求,使用 OAuth 2.0 用戶端憑證做為驗證方法,為來源 Salesforce 執行個體建立連線設定檔。

REST

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "CONNECTION_PROFILE_NAME",
  "salesforceProfile": {
    "domain": "DOMAIN_NAME",
    "oauth2ClientCredentials": {
      "clientId": "CLIENT_ID",
      "clientSecret": "CLIENT_SECRET"
    }
  }
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=saleforce-cp
{
  "displayName": "My Salesforce connection profile",
  "salesforceProfile": {
    "domain": "domain.my.salesforce.com",
    "oauth2ClientCredentials": {
      "clientId": "myClientId",
      "clientSecret": "projects/myProject/secrets/sf-client-secret"
    }
  }
}
  

gcloud

如要進一步瞭解如何使用 gcloud 建立連線設定檔,請參閱 Google Cloud SDK 說明文件

為 MongoDB 來源建立連線設定檔

下列程式碼顯示要求,使用 SRV 連線字串格式為來源 MongoDB 執行個體建立連線設定檔。

REST

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "CONNECTION_PROFILE_NAME",
  "mongodbProfile": {
    "hostAddresses": [ {
      "hostname": "HOST_URI"
    } ],
    "username": "USERNAME",
    "password": "PASSWORD",
    "srvConnectionFormat": {}
    },
    "staticServiceIpConnectivity": {}
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=mongodb-cp
{
  "displayName": "mongodb-cp",
  "mongodbProfile": {
    "hostAddresses": [ {
      "hostname": "cluster85828.ebeek.mongodb.net"
    } ],
    "username": "Cluster85828",
    "password": "password",
    "srvConnectionFormat": {}
    },
    "staticServiceIpConnectivity": {}
}
  

gcloud

如要進一步瞭解如何使用 gcloud 建立連線設定檔,請參閱 Google Cloud SDK 說明文件

下列程式碼顯示要求,可使用標準連線字串格式,為來源 MongoDB 執行個體建立連線設定檔。

REST

POST https://datastream.googleapis.com/v1/projects/PROJECT_ID/locations/
LOCATION/connectionProfiles?connectionProfileId=CONNECTION_PROFILE_ID
{
  "displayName": "CONNECTION_PROFILE_NAME",
  "mongodbProfile": {
    "hostAddresses": [ {
      "hostname": "HOST_IP",
      "port": "PORT_NUMBER"
    } ],
    "replica_set": "SET_ID",
    "username": "USERNAME",
    "password": "PASSWORD",
    "standardConnectionFormat": {
      "directConnection": false
    }
  },
  "staticServiceIpConnectivity": {}
}

例如:

POST https://datastream.googleapis.com/v1/projects/myProjectId/locations/
us-central1/connectionProfiles?connectionProfileId=mongodb-cp
{
  "displayName": "mongodb-cp",
  "mongodbProfile": {
    "hostAddresses": [ {
      "hostname": "34.19.10.194",
      "port": "27017"
    } ],
    "replicaset": "rs0"
    "username": "root",
    "password": "password",
    "standardConnectionFormat": {
      "directConnection": false
    }
  },
  "staticServiceIpConnectivity": {}
}
  

gcloud

如要進一步瞭解如何使用 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 建立連線設定檔,請參閱 Google Cloud SDK 說明文件

為 Cloud Storage 目的地建立連線設定檔

下列程式碼顯示為 Cloud Storage bucket 建立連線設定檔的要求。由於 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 擷取連線設定檔的相關資訊,請參閱 Google Cloud SDK 說明文件

列出連線設定檔

下列程式碼顯示擷取所有連線設定檔相關資訊的要求。

REST

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

gcloud

如要進一步瞭解如何使用 gcloud 擷取所有連線設定檔的相關資訊,請參閱 Google Cloud SDK 說明文件

更新連線設定檔

下列程式碼顯示變更來源資料庫現有連線設定檔使用者名稱和密碼的要求。

在要求中使用 updateMask 參數時,要求主體中只需要包含您指定的欄位 (在本範例中,為 usernamepassword 欄位,分別以 oracle_profile.usernameoracle_profile.password 旗標表示)。

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

下列程式碼顯示變更指派給連線設定檔的私人連線設定要求。這個連線設定檔使用虛擬私有雲對等互連網路連線方式。

在本範例中,您要將 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 更新連線設定檔,請參閱 Google Cloud SDK 說明文件

刪除連線設定檔

下列程式碼顯示刪除連線設定檔的要求。連線設定檔刪除後,使用該設定檔的任何串流都會失敗。

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 刪除連線設定檔,請參閱 Google Cloud SDK 說明文件

探索來源資料庫的結構

使用 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 探索來源資料庫的結構,請參閱 Google Cloud SDK 說明文件

後續步驟