# Google Cloud Storage Import Integration The Data Connector for Google Cloud Storage enables import of the contents of *.tsv and* .csv files stored in your GCS bucket. For export Integration, see [Google Cloud Storage Export Integration](/int/google-cloud-storage-export-integration). ## Prerequisites - Basic knowledge of Treasure Data - An existing Google Service Account - Generate and obtain a JSON key file from Google Developers Console. See [Generating a service account credential](https://cloud.google.com/storage/docs/authentication#generating-a-private-key). ## Static IP Address of Treasure Data Integration If your security policy requires IP whitelisting, you must add Treasure Data's IP addresses to your allowlist to ensure a successful connection. Please find the complete list of static IP addresses, organized by region, at the following link: [https://api-docs.treasuredata.com/en/overview/ip-addresses-integrations-result-workers/](https://api-docs.treasuredata.com/en/overview/ip-addresses-integrations-result-workers/) ## Import from Google Cloud Storage using TD Console ### Create a New Connection When you configure a data connection, you provide authentication to access the integration. In Treasure Data, you configure the authentication and then specify the source information. 1. Open TD Console. 2. Navigate to Integrations Hub >  Catalog 3. Search and select Google Cloud Storage. ![](/assets/image-20191206-003009.5434e471c33eac02c339db73bbed82434413cc8071dba89ed7deb8ddd0aadf75.276f5576.png) 4. The following dialog opens. ![](/assets/image-20191015-135359.53e904e28e902844ebd25df2bb8175971839b949f891dfca43c844b31eddb9a3.276f5576.png) 5. Create a New Google Cloud Storage Connector 6. Set the following parameters: | **Parameters** | **Description** | | --- | --- | | **Authentication mode** | Select a JSON keyfile. This method uses the JSON keyfile generated from the Google Developers Console. | | **JSON Keyfile** | Copy and paste the contents of the JSON keyfile generated from the Google Developers Console in this field. | | **Application Name** | *Treasure Data GCS Output* is the default value. As this is an arbitrary client name associated with API requests, you can leave the default value (Treasure Data GCS Output). | ### Name Your Connection 1. Type a name for your connection. 2. Select **Done.** ### Transfer Your Google Cloud Storage Account Data to Treasure Data After creating the authenticated connection, you are automatically taken to Authentications. 1. Search for the connection you created. 2. Select **New Source**. 3. Type a name for your **Source** in the Data Transfer field**.** 4. Click **Next**. ![](/assets/image-20200812-232831.affaea454b73d8f5b11d439a2861c2bebbc1ec0f2064f926499cb200aca2c3fd.276f5576.png) ### Source Table 1. Select **Next**. 2. The Source Table dialog opens. Edit the following parameters ![](/assets/image-20200812-233240.257493d38be492509c623f3cd8c55696fe1c29868b4ece21d6a07df9da44204f.276f5576.png) | **Parameters** | **Description** | | --- | --- | | **Bucket** | Google Cloud Storage bucket name (Ex. *your_bucket_name*) | | **Path Prefix** | Prefix of target keys. (Ex. *logs/data_*) | | **Path Regex** | regexp to match file paths. If a file path doesn’t match with this pattern, the file is skipped. (Ex. *.csv$* # in this case, a file is skipped if its' path doesn’t match with this pattern.) | | **Start after path** | Inserts *last_path* parameter so that the first execution skips files before the path. (Ex. *logs/data_20170101.csv*) | | **Incremental** | Enables incremental loading. If incremental loading is enabled, config diff for the next execution will include *last_path* parameter so that next execution skips files before the path. Otherwise, *last_path* will not be included. | Example: CloudFront Log Amazon CloudFront is a web service that speeds up the distribution of your static and dynamic web content. You can configure CloudFront to create log files that contain detailed information about every user request that CloudFront receives. If you enable logging, you can save CloudFront log files, shown as follows: ``` [your_bucket] - [logging] - [E231A697YXWD39.2017-04-23-15.a103fd5a.gz] [your_bucket] - [logging] - [E231A697YXWD39.2017-04-23-15.b2aede4a.gz] [your_bucket] - [logging] - [E231A697YXWD39.2017-04-23-16.594fa8e6.gz] [your_bucket] - [logging] - [E231A697YXWD39.2017-04-23-16.d12f42f9.gz] ``` In this case, the **Source Table** setting should be as shown: - **Bucket**: your_bucket - **Path Prefix**: logging/ - **Path Regex**: *.gz$* (Not Required) - **Start after path**: logging/E231A697YXWD39.2017-04-23-15.b2aede4a.gz (Assuming that you want to import the log files from 2017-04-23-16.) - **Incremental**: true (If you want to schedule this job.) ### Data Settings 1. Select **Next**. 2. The **Data Settings** page opens. Optionally, edit the data settings or skip this page of the dialog. ![](/assets/image-20200812-233941.d8cb1d6af994f3003283498c0e8f4fdd0616979d4c642b7f1d77133672b71b99.276f5576.png) ![](/assets/google-cloud-storage-import-integration-2025-02-24-1.50f24e1fb8d74b8ce877136a26f616367cb488af0944ab631593ae069465fa36.276f5576.png) ### Parser | **Parameters** | **Description** | | --- | --- | | **Type** | Following types are supported: - Avro - CSV - json - Query String | | **Default timezone** | Changes time zone of timestamp columns if the value itself doesn’t include time zone. | | **Total file count limit** | Maximum number of files to read. (optional) | | **Schema Settings** | You can name the columns and set the data type. TD parses a value as a specified data type. And then, it stores after converting to Treasure Data schema. - **boolean** - **long** - **timestamp**: will be imported as String type at Treasure Data (Ex. 2017-04-01 00:00:00.000) - **double** - **string** - **json** | ### Data Preview You can see a [preview](/products/customer-data-platform/integration-hub/batch/import/previewing-your-source-data) of your data before running the import by selecting Generate Preview. Data preview is optional and you can safely skip to the next page of the dialog if you choose to. 1. Select **Next**. The Data Preview page opens. 2. If you want to preview your data, select **Generate Preview**. 3. Verify the data. ### Data Placement For data placement, select the target database and table where you want your data placed and indicate how often the import should run. 1. Select **Next.** Under Storage, you will create a new or select an existing database and create a new or select an existing table for where you want to place the imported data. 2. Select a **Database** > **Select an existing** or **Create New Database**. 3. Optionally, type a database name. 4. Select a **Table**> **Select an existing** or **Create New Table**. 5. Optionally, type a table name. 6. Choose the method for importing the data. - **Append** (default)-Data import results are appended to the table. If the table does not exist, it will be created. - **Always Replace**-Replaces the entire content of an existing table with the result output of the query. If the table does not exist, a new table is created. - **Replace on New Data**-Only replace the entire content of an existing table with the result output when there is new data. 7. Select the **Timestamp-based Partition Key** column. If you want to set a different partition key seed than the default key, you can specify the long or timestamp column as the partitioning time. As a default time column, it uses upload_time with the add_time filter. 8. Select the **Timezone** for your data storage. 9. Under **Schedule**, you can choose when and how often you want to run this query. #### Run once 1. Select **Off**. 2. Select **Scheduling Timezone**. 3. Select **Create & Run Now**. #### Repeat Regularly 1. Select **On**. 2. Select the **Schedule**. The UI provides these four options: *@hourly*, *@daily* and *@monthly* or custom *cron*. 3. You can also select **Delay Transfer** and add a delay of execution time. 4. Select **Scheduling Timezone**. 5. Select **Create & Run Now**. After your transfer has run, you can see the results of your transfer in **Data Workbench** > **Databases.** ## Import from Google Cloud Storage using Workflow For sample workflows importing data from GCS, view the [Treasure Boxes](https://github.com/treasure-data/treasure-boxes/tree/master/td_load/gcs). ## Import from Google Cloud Storage via CLI (Toolbelt) Before setting up the connector, install the most current TD Toolbelt. The Data Connector supports parsing of "boolean", "long", "double", "string", and "timestamp" types. ### Create Seed Config File (seed.yml) Prepare *seed.yml* , with your JSON keyfile. You must also specify bucket name and target file name (or prefix for multiple files). ```yaml in: type: gcs bucket: sample_bucket path_prefix: path/to/sample_file # path the the *.csv or *.tsv file on your GCS bucket auth_method: json_key json_keyfile: content: | { "private_key_id": "1234567890", "private_key": "-----BEGIN PRIVATE KEY-----\nABCDEF", "client_id": "...", "client_email": "...", "type": "service_account" } out: mode: append ``` The Data Connector for Google Cloud Storage imports all files that match a specified prefix. (e.g. path_prefix: `path/to/sample_` –> `path/to/sample_201501.csv.gz`, `path/to/sample_201502.csv.gz`, …, `path/to/sample_201505.csv.gz`) ### Guess Fields (Generate load.yml) Use *connector:guess*. This command automatically reads the target file, and intelligently guesses the file format. ```bash td connector:guess seed.yml -o load.yml ``` If you open up *load.yml*, you see the guessed file format definitions including file formats, encodings, column names, and types. ```yaml in: type: gcs bucket: sample_bucket path_prefix: path/to/sample_file auth_method: json_key json_keyfile: content: | { "private_key_id": "1234567890", "private_key": "-----BEGIN PRIVATE KEY-----\nABCDEF", "client_id": "...", "client_email": "...", "type": "service_account" } decoders: - {type: gzip} parser: charset: UTF-8 newline: CRLF type: csv delimiter: ',' quote: '"' escape: '' skip_header_lines: 1 columns: - name: id type: long - name: company type: string - name: customer type: string - name: created_at type: timestamp format: '%Y-%m-%d %H:%M:%S' out: mode: append ``` You can preview how the system parses the file by using the *preview* command. ```bash td connector:preview load.yml ``` The guess command needs over 3 rows and 2 columns in source data file, because it guesses column definition using sample rows from source data. If the system detects your column name or column type unexpectedly, modify *load.yml* directly and preview again. The preview command will download one file from the specified bucket and display the results from that file. This may cause a difference in results from the preview and issue commands. ### Execute Load Job Submit the load job. It may take a couple of hours depending on the size of the data. Users need to specify the database and table where their data is stored. It’s also recommended to specify *--time-column* option since Treasure Data’s storage is partitioned by time. If the option is not given, the Data Connector will choose the first *long* or *timestamp* column as the partitioning time. The type of the column specified by *--time-column* must be either of *long* or *timestamp* type. If your data doesn’t have a time column, you may add it using the *add_time* filter option. More details at [add_time filter plugin](https://docs.treasuredata.com/smart/project-product-documentation/add_time-filter-function). ```bash td connector:issue load.yml --database td_sample_db --table td_sample_table --time-column created_at ``` The preceding command assumes you have already created a *database(td_sample_db)* and a *table(td_sample_table)*. If the database or the table do not exist in TD, this command will not succeed so create the database and table manually or use *--auto-create-table* option with *td connector:issue* command to auto-create the database and table: ```bash td connector:issue load.yml --database td_sample_db --table td_sample_table --time-column created_at --auto-create-table ``` The Data Connector does not sort records server-side. To use time-based partitioning effectively, sort records in files beforehand. If you have a field called *time*, you don’t have to specify the *--time-column* option. ```bash td connector:issue load.yml --database td_sample_db --table td_sample_table ``` ### Scheduled Execution You can schedule a periodic Data Connector execution for incremental Google Cloud Storage file imports. TD manages the scheduler carefully to ensure high availability. By using this feature, you no longer need a *cron* daemon on your local data center. For the scheduled import, the Data Connector for Google Cloud Storage imports all files that match with the specified prefix (e.g. path_prefix: `path/to/sample_` –> `path/to/sample_201501.csv.gz`, `path/to/sample_201502.csv.gz`, …, `path/to/sample_201505.csv.gz`) at first and remembers the last path (`path/to/sample_201505.csv.gz`) for the next execution. On the second and subsequent runs, it will only imports files that comes after the last path in alphabetical (lexicographic) order. (`path/to/sample_201506.csv.gz`, …) ### Create the Schedule A new schedule can be created using the *td connector:create* command. The following are required: the name of the schedule, the cron-style schedule, the database and table where their data will be stored, and the Data Connector configuration file. ```bash td connector:create daily_import "10 0 * * *" td_sample_db td_sample_table load.yml ``` It’s also recommended to specify the *--time-column* option, since Treasure Data’s storage is partitioned by time. ```bash td connector:create daily_import "10 0 * * *" td_sample_db td_sample_table load.yml --time-column created_at ``` The `cron` parameter also accepts three special options: `@hourly`, `@daily` and `@monthly`. By default, schedule is setup in UTC timezone. You can set the schedule in a timezone using -t or --timezone option. The `--timezone` option only supports extended timezone formats like 'Asia/Tokyo', 'America/Los_Angeles' etc. Timezone abbreviations like PST, CST are *not* supported and may lead to unexpected schedules. ### List the Schedules You can see the list of currently scheduled entries by running the command *td connector:list*. ``` $ td connector:list ```