管理連線設定檔

本頁面將說明如何使用 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 或 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": {}
}

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 建立連線設定檔,請按這裡

為 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 探索來源資料庫的結構,請按這裡

後續步驟