管理連線設定檔

您可以使用 Datastream API,以程式輔助方式建立及管理連線設定檔,儲存來源和目的地的驗證和連線資訊。您可以發出 REST API 呼叫,或使用 Google Cloud CLI。舉例來說,您可以為 Oracle、MySQL、PostgreSQL、SQL Server、Salesforce 和 MongoDB 來源,以及 BigQuery 和 Cloud Storage 目的地建立設定檔。

您也可以擷取、列出、更新或刪除連線設定檔,以及探索來源資料庫的結構。如需指令參考資料,請參閱 gcloud datastream connection-profiles。如需端對端逐步操作說明,請參閱「使用 Datastream API」。

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

下列程式碼顯示一項要求,使用 Secret 和 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 或互連網路連線的外部來源)。這類通訊會出現在虛擬私有雲對等互連連線中。

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": "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": "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": "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",
  "sqlserverProfile": {
    "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 說明文件。

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

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": {
    "bucket": "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": {
    "bucket": "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 參數,要求主體就只需包含您指定的欄位 (在本例中為 username 和 password 欄位,分別以 oracle_profile.username 和 oracle_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\":\
"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

例如:

PATCH -d {\"private_connectivity\":{\"private_connection\":\
"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

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 說明文件。

後續步驟