|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2020 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +from typing import Optional, Sequence, Tuple, Union |
| 19 | + |
| 20 | +from google.auth import credentials as auth_credentials |
| 21 | + |
| 22 | +from google.cloud.aiplatform import datasets |
| 23 | +from google.cloud.aiplatform.datasets import _datasources |
| 24 | +from google.cloud.aiplatform import initializer |
| 25 | +from google.cloud.aiplatform import schema |
| 26 | +from google.cloud.aiplatform import utils |
| 27 | + |
| 28 | + |
| 29 | +class TimeSeriesDataset(datasets._Dataset): |
| 30 | + """Managed time series dataset resource for AI Platform""" |
| 31 | + |
| 32 | + _supported_metadata_schema_uris: Optional[Tuple[str]] = ( |
| 33 | + schema.dataset.metadata.time_series, |
| 34 | + ) |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def create( |
| 38 | + cls, |
| 39 | + display_name: str, |
| 40 | + gcs_source: Optional[Union[str, Sequence[str]]] = None, |
| 41 | + bq_source: Optional[str] = None, |
| 42 | + project: Optional[str] = None, |
| 43 | + location: Optional[str] = None, |
| 44 | + credentials: Optional[auth_credentials.Credentials] = None, |
| 45 | + request_metadata: Optional[Sequence[Tuple[str, str]]] = (), |
| 46 | + encryption_spec_key_name: Optional[str] = None, |
| 47 | + sync: bool = True, |
| 48 | + ) -> "TimeSeriesDataset": |
| 49 | + """Creates a new tabular dataset. |
| 50 | +
|
| 51 | + Args: |
| 52 | + display_name (str): |
| 53 | + Required. The user-defined name of the Dataset. |
| 54 | + The name can be up to 128 characters long and can be consist |
| 55 | + of any UTF-8 characters. |
| 56 | + gcs_source (Union[str, Sequence[str]]): |
| 57 | + Google Cloud Storage URI(-s) to the |
| 58 | + input file(s). May contain wildcards. For more |
| 59 | + information on wildcards, see |
| 60 | + https://cloud.google.com/storage/docs/gsutil/addlhelp/WildcardNames. |
| 61 | + examples: |
| 62 | + str: "gs://bucket/file.csv" |
| 63 | + Sequence[str]: ["gs://bucket/file1.csv", "gs://bucket/file2.csv"] |
| 64 | + bq_source (str): |
| 65 | + BigQuery URI to the input table. |
| 66 | + example: |
| 67 | + "bq://project.dataset.table_name" |
| 68 | + project (str): |
| 69 | + Project to upload this model to. Overrides project set in |
| 70 | + aiplatform.init. |
| 71 | + location (str): |
| 72 | + Location to upload this model to. Overrides location set in |
| 73 | + aiplatform.init. |
| 74 | + credentials (auth_credentials.Credentials): |
| 75 | + Custom credentials to use to upload this model. Overrides |
| 76 | + credentials set in aiplatform.init. |
| 77 | + request_metadata (Sequence[Tuple[str, str]]): |
| 78 | + Strings which should be sent along with the request as metadata. |
| 79 | + encryption_spec_key_name (Optional[str]): |
| 80 | + Optional. The Cloud KMS resource identifier of the customer |
| 81 | + managed encryption key used to protect the dataset. Has the |
| 82 | + form: |
| 83 | + ``projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key``. |
| 84 | + The key needs to be in the same region as where the compute |
| 85 | + resource is created. |
| 86 | +
|
| 87 | + If set, this Dataset and all sub-resources of this Dataset will be secured by this key. |
| 88 | +
|
| 89 | + Overrides encryption_spec_key_name set in aiplatform.init. |
| 90 | + sync (bool): |
| 91 | + Whether to execute this method synchronously. If False, this method |
| 92 | + will be executed in concurrent Future and any downstream object will |
| 93 | + be immediately returned and synced when the Future has completed. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + time_series_dataset (TimeSeriesDataset): |
| 97 | + Instantiated representation of the managed time series dataset resource. |
| 98 | +
|
| 99 | + """ |
| 100 | + |
| 101 | + utils.validate_display_name(display_name) |
| 102 | + |
| 103 | + api_client = cls._instantiate_client(location=location, credentials=credentials) |
| 104 | + |
| 105 | + metadata_schema_uri = schema.dataset.metadata.time_series |
| 106 | + |
| 107 | + datasource = _datasources.create_datasource( |
| 108 | + metadata_schema_uri=metadata_schema_uri, |
| 109 | + gcs_source=gcs_source, |
| 110 | + bq_source=bq_source, |
| 111 | + ) |
| 112 | + |
| 113 | + return cls._create_and_import( |
| 114 | + api_client=api_client, |
| 115 | + parent=initializer.global_config.common_location_path( |
| 116 | + project=project, location=location |
| 117 | + ), |
| 118 | + display_name=display_name, |
| 119 | + metadata_schema_uri=metadata_schema_uri, |
| 120 | + datasource=datasource, |
| 121 | + project=project or initializer.global_config.project, |
| 122 | + location=location or initializer.global_config.location, |
| 123 | + credentials=credentials or initializer.global_config.credentials, |
| 124 | + request_metadata=request_metadata, |
| 125 | + encryption_spec=initializer.global_config.get_encryption_spec( |
| 126 | + encryption_spec_key_name=encryption_spec_key_name |
| 127 | + ), |
| 128 | + sync=sync, |
| 129 | + ) |
| 130 | + |
| 131 | + def import_data(self): |
| 132 | + raise NotImplementedError( |
| 133 | + f"{self.__class__.__name__} class does not support 'import_data'" |
| 134 | + ) |
0 commit comments