-
Notifications
You must be signed in to change notification settings - Fork 80
Insert only impala load template #1233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BoBaeva
merged 2 commits into
main
from
person/bobaeva/insert-only-impala-load-template
Oct 11, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...dk-impala/src/vdk/plugin/impala/templates/load/fact/insert/00-fact-snapshot-definition.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2021 VMware, Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from pydantic import BaseModel | ||
| from vdk.api.job_input import IJobInput | ||
| from vdk.plugin.impala.templates.template_arguments_validator import ( | ||
| TemplateArgumentsValidator, | ||
| ) | ||
|
|
||
|
|
||
| class FactDailySnapshotParams(BaseModel): | ||
| target_schema: str | ||
| target_table: str | ||
| source_schema: str | ||
| source_view: str | ||
|
|
||
|
|
||
| class FactDailySnapshot(TemplateArgumentsValidator): | ||
| TemplateParams = FactDailySnapshotParams | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__() | ||
|
|
||
|
|
||
| def run(job_input: IJobInput): | ||
| FactDailySnapshot().get_validated_args(job_input, job_input.get_arguments()) |
3 changes: 3 additions & 0 deletions
3
...mpala/src/vdk/plugin/impala/templates/load/fact/insert/01-test-if-view-matches-target.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| SELECT * FROM {source_schema}.{source_view} LIMIT 0 | ||
| UNION ALL | ||
| SELECT * FROM {target_schema}.{target_table} LIMIT 0; |
7 changes: 7 additions & 0 deletions
7
...ins/vdk-impala/src/vdk/plugin/impala/templates/load/fact/insert/02-insert-into-target.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| -- /* +SHUFFLE */ below is a query hint to Impala. | ||
| -- Do not remove! https://www.cloudera.com/documentation/enterprise/5-9-x/topics/impala_hints.html | ||
| INSERT INTO TABLE {target_schema}.{target_table} {_vdk_template_insert_partition_clause} /* +SHUFFLE */ | ||
| ( | ||
| SELECT * | ||
| FROM {source_schema}.{source_view} | ||
| ); |
1 change: 1 addition & 0 deletions
1
...ts/vdk-plugins/vdk-impala/src/vdk/plugin/impala/templates/load/fact/insert/03-refresh.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| REFRESH {target_schema}.{target_table}; |
1 change: 1 addition & 0 deletions
1
...-plugins/vdk-impala/src/vdk/plugin/impala/templates/load/fact/insert/04-compute-stats.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| COMPUTE STATS {target_schema}.{target_table}; |
43 changes: 43 additions & 0 deletions
43
...k-plugins/vdk-impala/src/vdk/plugin/impala/templates/load/fact/insert/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| ### Purpose: | ||
|
|
||
| This template can be used to load raw data from Data Lake to target Table in Data Warehouse. In summary, it appends all records from the source table to the target table. | ||
|
|
||
| ### Template Name (template_name): | ||
|
|
||
| - "insert" | ||
|
|
||
| ### Template Parameters (template_args): | ||
|
|
||
| - target_schema - Data Warehouse schema, where target data is loaded | ||
| - target_table - Data Warehouse table of DW type table, where target data is loaded | ||
| - source_schema - Data Lake schema, where source raw data is loaded from | ||
| - source_view - Data Lake view, where source raw data is loaded from | ||
|
|
||
| ### Prerequisites: | ||
|
|
||
| In order to use this template you need to ensure the following: | ||
| - {source_schema}.{source_view} exists | ||
| - {target_schema}.{target_table} exists | ||
| - {source_schema}.{source_view} has the exact same schema as {target_schema}.{target_table} | ||
|
|
||
| ### Sample Usage: | ||
|
|
||
| Say there is SDDC-related 'Snapshot Periodic Fact Table' called 'fact_vmc_utilization_cpu_mem_every5min_daily' in 'history' schema. | ||
| Updating it with the latest raw data from a Data Lake (from source view called 'vw_fact_vmc_utilization_cpu_mem_every5min_daily' in 'default' schema) is done in the following manner: | ||
|
|
||
| ```python | ||
| def run(job_input): | ||
| # . . . | ||
| template_args = { | ||
| 'source_schema': 'default', | ||
| 'source_view': 'vw_fact_vmc_utilization_cpu_mem_every5min_daily', | ||
| 'target_schema': 'history', | ||
| 'target_table': 'fact_vmc_utilization_cpu_mem_every5min_daily' | ||
| } | ||
| job_input.execute_template('insert', template_args) | ||
| # . . . | ||
| ``` | ||
|
|
||
| ### Example | ||
|
|
||
| See all templates in [our example documentation](https://github.com/vmware/versatile-data-kit/wiki/SQL-Data-Processing-templates-examples). |
6 changes: 6 additions & 0 deletions
6
projects/vdk-plugins/vdk-impala/src/vdk/plugin/impala/templates/load/fact/insert/params.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [ | ||
| "target_schema", | ||
| "target_table", | ||
| "source_schema", | ||
| "source_view" | ||
| ] |
88 changes: 88 additions & 0 deletions
88
...vdk-plugins/vdk-impala/tests/functional/jobs/insert_template_job/01_prepare_input_data.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Copyright 2021 VMware, Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from vdk.api.job_input import IJobInput | ||
|
|
||
|
|
||
| __author__ = "VMware, Inc." | ||
| __copyright__ = ( | ||
| "Copyright 2019 VMware, Inc. All rights reserved. -- VMware Confidential" | ||
| ) | ||
|
|
||
|
|
||
| def run(job_input: IJobInput) -> None: | ||
| # Step 1: create a table that represents the current state | ||
|
|
||
| # job_input.execute_query(u''' | ||
| # DROP TABLE IF EXISTS `{target_schema}`.`{target_table}` | ||
| # ''') | ||
| job_input.execute_query( | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS `{target_schema}`.`{target_table}` ( | ||
| `dim_sddc_sk` STRING, | ||
| `dim_org_id` INT, | ||
| `dim_date_id` TIMESTAMP, | ||
| `host_count` BIGINT, | ||
| `cluster_count` BIGINT | ||
| ) STORED AS PARQUET | ||
| """ | ||
| ) | ||
| job_input.execute_query( | ||
| """ | ||
| INSERT OVERWRITE TABLE `{target_schema}`.`{target_table}` VALUES ( | ||
| ("sddc01-r01", 1, "2019-11-18", 5 , 1), | ||
| ("sddc02-r01", 2, "2019-11-18", 4 , 1) | ||
| ) | ||
| """ | ||
| ) | ||
|
|
||
| # Step 2: create a table that represents the next snapshot | ||
|
|
||
| # job_input.execute_query(u''' | ||
| # DROP TABLE IF EXISTS `{source_schema}`.`{source_view}` | ||
| # ''') | ||
| job_input.execute_query( | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS `{source_schema}`.`{source_view}` ( | ||
| `dim_sddc_sk` STRING, | ||
| `dim_org_id` INT, | ||
| `dim_date_id` TIMESTAMP, | ||
| `host_count` BIGINT, | ||
| `cluster_count` BIGINT | ||
| ) STORED AS PARQUET | ||
| """ | ||
| ) | ||
| job_input.execute_query( | ||
| """ | ||
| INSERT OVERWRITE TABLE `{source_schema}`.`{source_view}` VALUES ( | ||
| ("sddc01-r01", 1, "2019-11-19", 5 , 1), | ||
| ("sddc01-r01", 1, "2019-11-20", 10, 2) | ||
| ) | ||
| """ | ||
| ) | ||
|
|
||
| # Step 3: Create a table containing the state expected after updating the current state with the next snapshot | ||
|
|
||
| # job_input.execute_query(u''' | ||
| # DROP TABLE IF EXISTS `{expect_schema}`.`{expect_table}` | ||
| # ''') | ||
| job_input.execute_query( | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS `{expect_schema}`.`{expect_table}` ( | ||
| `dim_sddc_sk` STRING, | ||
| `dim_org_id` INT, | ||
| `dim_date_id` TIMESTAMP, | ||
| `host_count` BIGINT, | ||
| `cluster_count` BIGINT | ||
| ) STORED AS PARQUET | ||
| """ | ||
| ) | ||
| job_input.execute_query( | ||
| """ | ||
| INSERT OVERWRITE TABLE `{expect_schema}`.`{expect_table}` VALUES ( | ||
| ("sddc01-r01", 1, "2019-11-18", 5 , 1), | ||
| ("sddc02-r01", 2, "2019-11-18", 4 , 1), | ||
| ("sddc01-r01", 1, "2019-11-19", 5 , 1), | ||
| ("sddc01-r01", 1, "2019-11-20", 10, 2) | ||
| ) | ||
| """ | ||
| ) | ||
15 changes: 15 additions & 0 deletions
15
...dk-plugins/vdk-impala/tests/functional/jobs/insert_template_job/02_run_insert_template.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Copyright 2021 VMware, Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from vdk.api.job_input import IJobInput | ||
|
|
||
| __author__ = "VMware, Inc." | ||
| __copyright__ = ( | ||
| "Copyright 2019 VMware, Inc. All rights reserved. -- VMware Confidential" | ||
| ) | ||
|
|
||
|
|
||
| def run(job_input: IJobInput) -> None: | ||
| job_input.execute_template( | ||
| template_name="insert", | ||
| template_args=job_input.get_arguments(), | ||
| ) |
92 changes: 92 additions & 0 deletions
92
...s/vdk-impala/tests/functional/jobs/insert_template_partition_job/01_prepare_input_data.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # Copyright 2021 VMware, Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from vdk.api.job_input import IJobInput | ||
|
|
||
|
|
||
| __author__ = "VMware, Inc." | ||
| __copyright__ = ( | ||
| "Copyright 2019 VMware, Inc. All rights reserved. -- VMware Confidential" | ||
| ) | ||
|
|
||
|
|
||
| def run(job_input: IJobInput) -> None: | ||
| # Step 1: create a table that represents the current стате | ||
|
|
||
| # job_input.execute_query(u''' | ||
| # DROP TABLE IF EXISTS `{target_schema}`.`{target_table}` | ||
| # ''') | ||
| job_input.execute_query( | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS `{target_schema}`.`{target_table}` ( | ||
| `dim_sddc_sk` STRING, | ||
| `dim_org_id` INT, | ||
| `dim_date_id` TIMESTAMP, | ||
| `host_count` BIGINT | ||
| ) PARTITIONED BY (`cluster_count` BIGINT) STORED AS PARQUET | ||
| """ | ||
| ) | ||
| job_input.execute_query( | ||
| """ | ||
| TRUNCATE `{target_schema}`.`{target_table}` | ||
| """ | ||
| ) | ||
| job_input.execute_query( | ||
| """ | ||
| INSERT OVERWRITE TABLE `{target_schema}`.`{target_table}` PARTITION (cluster_count) VALUES ( | ||
| ("sddc01-r01", 1, "2019-11-18", 5 , 1), | ||
| ("sddc02-r01", 2, "2019-11-18", 4 , 1) | ||
| ) | ||
| """ | ||
| ) | ||
|
|
||
| # Step 2: create a table that represents the next snapshot | ||
|
|
||
| # job_input.execute_query(u''' | ||
| # DROP TABLE IF EXISTS `{source_schema}`.`{source_view}` | ||
| # ''') | ||
| job_input.execute_query( | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS `{source_schema}`.`{source_view}` ( | ||
| `dim_sddc_sk` STRING, | ||
| `dim_org_id` INT, | ||
| `dim_date_id` TIMESTAMP, | ||
| `host_count` BIGINT, | ||
| `cluster_count` BIGINT | ||
| ) STORED AS PARQUET | ||
| """ | ||
| ) | ||
| job_input.execute_query( | ||
| """ | ||
| INSERT OVERWRITE TABLE `{source_schema}`.`{source_view}` VALUES ( | ||
| ("sddc01-r01", 1, "2019-11-19", 5 , 1), | ||
| ("sddc01-r01", 1, "2019-11-20", 10 , 2) | ||
| ) | ||
| """ | ||
| ) | ||
|
|
||
| # Step 3: Create a table containing the state expected after updating the current state with the next snapshot | ||
|
|
||
| # job_input.execute_query(u''' | ||
| # DROP TABLE IF EXISTS `{expect_schema}`.`{expect_table}` | ||
| # ''') | ||
| job_input.execute_query( | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS `{expect_schema}`.`{expect_table}` ( | ||
| `dim_sddc_sk` STRING, | ||
| `dim_org_id` INT, | ||
| `dim_date_id` TIMESTAMP, | ||
| `host_count` BIGINT, | ||
| `cluster_count` BIGINT | ||
| ) STORED AS PARQUET | ||
| """ | ||
| ) | ||
| job_input.execute_query( | ||
| """ | ||
| INSERT OVERWRITE TABLE `{expect_schema}`.`{expect_table}` VALUES ( | ||
| ("sddc01-r01", 1, "2019-11-18", 5 , 1), | ||
| ("sddc02-r01", 2, "2019-11-18", 4 , 1), | ||
| ("sddc01-r01", 1, "2019-11-19", 5 , 1), | ||
| ("sddc01-r01", 1, "2019-11-20", 10 , 2) | ||
| ) | ||
| """ | ||
| ) |
15 changes: 15 additions & 0 deletions
15
.../vdk-impala/tests/functional/jobs/insert_template_partition_job/02_run_insert_template.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Copyright 2021 VMware, Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from vdk.api.job_input import IJobInput | ||
|
|
||
| __author__ = "VMware, Inc." | ||
| __copyright__ = ( | ||
| "Copyright 2019 VMware, Inc. All rights reserved. -- VMware Confidential" | ||
| ) | ||
|
|
||
|
|
||
| def run(job_input: IJobInput) -> None: | ||
| job_input.execute_template( | ||
| template_name="insert", | ||
| template_args=job_input.get_arguments(), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.