From edc1b7950ad986a35f3e2cc82f174aa319625726 Mon Sep 17 00:00:00 2001 From: ShawnGilleran <92932985+ShawnGilleran@users.noreply.github.com> Date: Fri, 13 Sep 2024 15:25:29 -0500 Subject: [PATCH] Create LiveRamp_Sample_Notebook.ipynb --- .../LiveRamp/LiveRamp_Sample_Notebook.ipynb | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 samples/LiveRamp/LiveRamp_Sample_Notebook.ipynb diff --git a/samples/LiveRamp/LiveRamp_Sample_Notebook.ipynb b/samples/LiveRamp/LiveRamp_Sample_Notebook.ipynb new file mode 100644 index 0000000..f0d1310 --- /dev/null +++ b/samples/LiveRamp/LiveRamp_Sample_Notebook.ipynb @@ -0,0 +1,87 @@ +{ + "metadata": { + "kernelspec": { + "display_name": "Streamlit Notebook", + "name": "streamlit" + } + }, + "nbformat_minor": 5, + "nbformat": 4, + "cells": [ + { + "cell_type": "markdown", + "id": "1299f7da-e99a-4fa8-b85f-3a8c50f8d029", + "metadata": { + "name": "cell8", + "collapsed": false + }, + "source": "LiveRamp Quickstart example SQL\n\n This will help a developer who has the LiveRamp Native Application for Identity installed\n It will perform the following functions\n 1. Match a table on HEMS (Assuming you have to HEM tables)\n 2. Setup and initiate the LiveRamp Native Identity Resolution\n 3. Attempt a match on RampIDs with you and a partner\n 4. Setup and initiate the LiveRamp Native Identity Translation\n 5. Match using the RampIDs between You, a partner and the Translation Table Output\n\n\n If you need additional support reach out to your LiveRamp representative\n" + }, + { + "cell_type": "code", + "id": "3775908f-ca36-4846-8f38-5adca39217f2", + "metadata": { + "language": "python", + "name": "cell1" + }, + "source": "# Import python packages\nimport streamlit as st\nimport pandas as pd\n\n# We can also use Snowpark for our analyses!\nfrom snowflake.snowpark.context import get_active_session\nsession = get_active_session()\n", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "id": "31e1ab1a-a144-4220-9bdf-653f3cf4dea0", + "metadata": { + "language": "sql", + "name": "cell5" + }, + "outputs": [], + "source": "// **************************************************************************************\n// **************************************************************************************\n// 1. Start Match Using HEMS\n// **************************************************************************************\n// **************************************************************************************\n\n-- create the match report on hashed emails\nWITH total_table1 AS (\n SELECT COUNT(*) AS total_count1\n FROM \"\"\n),\ntotal_table2 AS (\n SELECT COUNT(*) AS total_count2\n FROM \"\"\n),\nmatching_rows AS (\n SELECT COUNT(*) AS match_count\n FROM \"\" t1\n INNER JOIN \"\" t2 ON t1.hashed_email = t2.hashed_email\n)\n-- Calculate the match rate\nSELECT (match_count::FLOAT / (total_count1)) * 100 AS match_rate_percentage\nFROM total_table1, total_table2, matching_rows;\n\n\n// **************************************************************************************\n// **************************************************************************************\n// 1. END Match Using HEMS\n// **************************************************************************************\n// **************************************************************************************\n", + "execution_count": null + }, + { + "cell_type": "code", + "id": "22dc9741-7355-4a0e-bd77-d22c080e5f43", + "metadata": { + "language": "sql", + "name": "cell6" + }, + "outputs": [], + "source": "// **************************************************************************************\n// **************************************************************************************\n// 2. Start Identity Resolution\n// **************************************************************************************\n// **************************************************************************************\n\n--Define the role used to grant permissions to native app.\nset role_name = 'ACCOUNTADMIN';\n\n--Update this section with the appropriate variables\nset customer_db_name = '';\nset customer_schema_name=concat($customer_db_name,'.','');\nset customer_input_table_name=concat($customer_schema_name,'.','');\nset customer_meta_table_name=concat($customer_schema_name,'.','');\nset customer_metrics_table_name=concat($customer_schema_name,'.','');\nset customer_logging_table_name=concat($customer_schema_name,'.','');\n\n--name of database where LiveRamp share is loaded\n-- V1 set native_app_db_name='LR_APP_SHARE_RESOLUTION_DEMO';\nset application_name = '';\nset native_app_schema_name = 'LR_APP_SCHEMA';\nset native_job_schema_name = 'LR_JOB_SCHEMA';\n--Create output table for results (please update)\nset output_table_name = '';\n\n\nSET final_output = concat($application_name,'.',$native_job_schema_name,'.',$output_table_name);\n--replace \n\n// Defining metadata\n\n--Switch to the database and schema that has your input table for Resolution\nUSE DATABASE identifier($customer_db_name);\nUSE schema identifier($customer_schema_name);\n\n--Metadata table schema\ncreate or replace table identifier($customer_meta_table_name) as\nselect\n TO_VARCHAR(DECRYPT(ENCRYPT('', 'HideFromLogs'), 'HideFromLogs'), 'utf-8') as client_id,\n TO_VARCHAR(DECRYPT(ENCRYPT('', 'HideFromLogs'), 'HideFromLogs'), 'utf-8') as client_secret,\n 'resolution' as execution_mode,\n 'pii' as execution_type,\n parse_json($$\n {\n \"name\": [\"\"],\n \"streetAddress\": [\"\"],\n \"city\": \"\",\n \"state\": \"\",\n \"zipCode\": \"\",\n \"phone\": \"\",\n \"email\": \"\"\n }\n $$) as target_columns,\n 1 as limit;\n\ngrant usage on database identifier ($customer_db_name) to application identifier($application_name);\ngrant usage on schema identifier ($customer_schema_name) to application identifier($application_name);\ngrant select on table identifier ($customer_input_table_name) to application identifier($application_name);\ngrant select on table identifier ($customer_meta_table_name) to application identifier($application_name);\n\n\nuse database identifier ($application_name);\nuse schema lr_app_schema;\n\n--Identity Resolution Call\ncall lr_resolution_and_transcoding(\n\t$customer_input_table_name,\n\t$customer_meta_table_name,\n\t$output_table_name, \n $customer_logging_table_name,\n\t$customer_metrics_table_name\n);\n\n\nselect * from \"\"\norder by ts desc\nlimit 120;\n\nselect * from \"\"\norder by event_time desc\nlimit 30;\n\n\ncall check_for_output(\n\t$output_table_name\n);\n\n// **************************************************************************************\n// **************************************************************************************\n// 2. End Identity Resolution\n// **************************************************************************************\n// **************************************************************************************\n", + "execution_count": null + }, + { + "cell_type": "code", + "id": "9a897809-eadb-44a4-b0bb-085e2e9e56b8", + "metadata": { + "language": "sql", + "name": "cell7" + }, + "outputs": [], + "source": "// **************************************************************************************\n// **************************************************************************************\n// 3. Start Match before Transcoding\n// **************************************************************************************\n// **************************************************************************************\n\n-- create the match report\nWITH total_table1 AS (\n SELECT COUNT(*) AS total_count1\n FROM \"\"\n),\ntotal_table2 AS (\n SELECT COUNT(*) AS total_count2\n FROM \"\"\n),\nmatching_rows AS (\n SELECT COUNT(*) AS match_count\n FROM \"\" t1\n INNER JOIN \"\" t2 ON t1.rampid = t2.rampid\n)\n-- Calculate the match rate\nSELECT (match_count::FLOAT / (total_count1)) * 100 AS match_rate_percentage\nFROM total_table1, total_table2, matching_rows;\n// **************************************************************************************\n// **************************************************************************************\n// 3. END Match before Transcoding\n// **************************************************************************************\n// **************************************************************************************\n", + "execution_count": null + }, + { + "cell_type": "code", + "id": "0969be34-14cc-48c7-a2be-c2a2301bfc5b", + "metadata": { + "language": "sql", + "name": "cell4" + }, + "outputs": [], + "source": "// **************************************************************************************\n// **************************************************************************************\n// 4. Start A Transcoding setup\n// **************************************************************************************\n// **************************************************************************************\n\n\nCREATE OR REPLACE TABLE \"\"\nAS SELECT RAMPID , '' AS TARGET_DOMAIN , 'RampID' AS TARGET_TYPE FROM \"');\nset customer_input_table_name=concat($customer_schema_name,'.','');\nset customer_meta_table_name=concat($customer_schema_name,'.','');\nset customer_metrics_table_name=concat($customer_schema_name,'.','');\nset customer_logging_table_name=concat($customer_schema_name,'.','');\n\n--name of database where LiveRamp share is loaded\n-- V1 set native_app_db_name='LR_APP_SHARE_RESOLUTION_DEMO';\nset application_name = '';\nset native_app_schema_name = 'LR_APP_SCHEMA';\nset native_job_schema_name = 'LR_JOB_SCHEMA';\n--Create output table for results (please update)\nset output_table_name = '';\n\n\ncreate or replace table identifier($customer_meta_table_name) as\nselect\n TO_VARCHAR(DECRYPT(ENCRYPT('', 'HideFromLogs'), 'HideFromLogs'), 'utf-8') as client_id,\n TO_VARCHAR(DECRYPT(ENCRYPT('', 'HideFromLogs'), 'HideFromLogs'), 'utf-8') as client_secret,\n 'transcoding' as execution_mode,\n 'transcoding' as execution_type,\n '' as target_column,\n '' as target_domain_column,\n '' as target_type_column;\n \n--Switch to Native App database and schema\nUSE DATABASE identifier($customer_db_name);\nUSE schema identifier($customer_schema_name);\n\n\n-- Grant permissions\ngrant usage on database identifier ($customer_db_name) to application identifier($application_name);\ngrant usage on schema identifier ($customer_schema_name) to application identifier($application_name);\ngrant select on table identifier ($customer_input_table_name) to application identifier($application_name);\ngrant select on table identifier ($customer_meta_table_name) to application identifier($application_name);\n\n\nuse database identifier ($application_name);\nuse schema lr_app_schema;\n\n--Identity Resolution Call\ncall lr_resolution_and_transcoding(\n\t$customer_input_table_name,\n\t$customer_meta_table_name,\n\t$output_table_name, \n $customer_logging_table_name,\n\t$customer_metrics_table_name\n);\n\n----Checking for Output\ncall check_for_output(\n\t$output_table_name\n);\n\n\n// **************************************************************************************\n// **************************************************************************************\n// 4. END Transcoding A setup\n// **************************************************************************************\n// **************************************************************************************\n", + "execution_count": null + }, + { + "cell_type": "code", + "id": "8d50cbf4-0c8d-4950-86cb-114990437ac9", + "metadata": { + "language": "sql", + "name": "cell2" + }, + "source": "// **************************************************************************************\n// **************************************************************************************\n// 5. Start Match Rate on RampID\n// **************************************************************************************\n// **************************************************************************************\n\n\nWITH total_table1 AS (\n SELECT COUNT(*) AS total_count1\n FROM \"\"\n),\ntotal_table2 AS (\n SELECT COUNT(*) AS total_count2\n FROM \"\"\n),\nmatching_rows AS (\n SELECT COUNT(*) AS match_count\n FROM \"\" t1\n INNER JOIN \"\" t3\n ON t1.RampID = t3.RampID\n INNER JOIN \"\" t2\n ON t3.TRANSCODED_IDENTIFIER = t2.RampID\n)\n-- Calculate the match rate\nSELECT (match_count::FLOAT / (total_count1)) * 100 AS match_rate_percentage\nFROM total_table1, total_table2, matching_rows;\n\n// **************************************************************************************\n// **************************************************************************************\n// 5. END Transcoding Match Rate on RampID\n// **************************************************************************************\n// **************************************************************************************\n", + "execution_count": null, + "outputs": [] + } + ] +}