Skip to content
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

Create LiveRamp_Sample_Notebook.ipynb #119

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions samples/LiveRamp/LiveRamp_Sample_Notebook.ipynb
Original file line number Diff line number Diff line change
@@ -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 \"<Your HEM Input Table>\"\n),\ntotal_table2 AS (\n SELECT COUNT(*) AS total_count2\n FROM \"<Partners HEM Input>\"\n),\nmatching_rows AS (\n SELECT COUNT(*) AS match_count\n FROM \"<Your HEM Input Table>\" t1\n INNER JOIN \"<Partners HEM Input>\" 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 = '<your db>';\nset customer_schema_name=concat($customer_db_name,'.','<your schema>');\nset customer_input_table_name=concat($customer_schema_name,'.','<input Table>');\nset customer_meta_table_name=concat($customer_schema_name,'.','<meta table name>');\nset customer_metrics_table_name=concat($customer_schema_name,'.','<metrics table>');\nset customer_logging_table_name=concat($customer_schema_name,'.','<logging table>');\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 = '<native 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 = '<output table>';\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('<client_id>', 'HideFromLogs'), 'HideFromLogs'), 'utf-8') as client_id,\n TO_VARCHAR(DECRYPT(ENCRYPT('<client_secret>', 'HideFromLogs'), 'HideFromLogs'), 'utf-8') as client_secret,\n 'resolution' as execution_mode,\n 'pii' as execution_type,\n parse_json($$\n {\n \"name\": [\"<up to 4 name column names>\"],\n \"streetAddress\": [\"<up to 7 address column names>\"],\n \"city\": \"<city column>\",\n \"state\": \"<state column>\",\n \"zipCode\": \"<zipcode column>\",\n \"phone\": \"<phone column>\",\n \"email\": \"<email column>\"\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 \"<your logging table>\"\norder by ts desc\nlimit 120;\n\nselect * from \"<your metrics table>\"\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 \"<Your RampID enabled table>\"\n),\ntotal_table2 AS (\n SELECT COUNT(*) AS total_count2\n FROM \"<Partners RampID Enabled Table>\"\n),\nmatching_rows AS (\n SELECT COUNT(*) AS match_count\n FROM \"<Your RampID enabled table>\" t1\n INNER JOIN \"<Partners RampID Enabled Table>\" 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 \"<input table>\"\nAS SELECT RAMPID , '<Partner Domain>' AS TARGET_DOMAIN , 'RampID' AS TARGET_TYPE FROM \"<output of Identity Resolution\";\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 = '<your db>';\nset customer_schema_name=concat($customer_db_name,'.','<your schema>');\nset customer_input_table_name=concat($customer_schema_name,'.','<input Table>');\nset customer_meta_table_name=concat($customer_schema_name,'.','<meta table name>');\nset customer_metrics_table_name=concat($customer_schema_name,'.','<metrics table>');\nset customer_logging_table_name=concat($customer_schema_name,'.','<logging table>');\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 = '<native 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 = '<output table>';\n\n\ncreate or replace table identifier($customer_meta_table_name) as\nselect\n TO_VARCHAR(DECRYPT(ENCRYPT('<client_id>', 'HideFromLogs'), 'HideFromLogs'), 'utf-8') as client_id,\n TO_VARCHAR(DECRYPT(ENCRYPT('<client_secret>', 'HideFromLogs'), 'HideFromLogs'), 'utf-8') as client_secret,\n 'transcoding' as execution_mode,\n 'transcoding' as execution_type,\n '<column to be translated>' as target_column,\n '<column containing target domain>' as target_domain_column,\n '<column containing target type>' 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 \"<Your input RampID enabled Table>\"\n),\ntotal_table2 AS (\n SELECT COUNT(*) AS total_count2\n FROM \"<Partners RampID Enabled Table>\"\n),\nmatching_rows AS (\n SELECT COUNT(*) AS match_count\n FROM \"<Your input RampID enabled Table>\" t1\n INNER JOIN \"<Translation Output Table>\" t3\n ON t1.RampID = t3.RampID\n INNER JOIN \"<Partners RampID Enabled Table>\" 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": []
}
]
}