Skip to content

Instantly share code, notes, and snippets.

@HariSekhon
Created March 22, 2024 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HariSekhon/b2dc41588a0725d71f1586b58dfe7936 to your computer and use it in GitHub Desktop.
Save HariSekhon/b2dc41588a0725d71f1586b58dfe7936 to your computer and use it in GitHub Desktop.
snowflake.md from HariSekhon/Knowledge-Base repo: https://github.com/HariSekhon/Knowlege-Base

Snowflake

SnowPipe

--use role snowpipe_role;
use database demo_db;

create or replace stage demo_db.public.snowstage
    url='s3://snowflake-oregon/'
    credentials = (AWS_KEY_ID = '...' AWS_SECRET_KEY = '...' );
show stages;

list @snowstage;

-- Create target table for JSON data
create or replace table public.twitter(json variant);
show tables;

-- Create a pipe to ingest JSON data
create or replace pipe public.snowpipe auto_ingest=true as
    copy into public.twitter
    from @public.snowstage
    file_format = (type = 'JSON');

-- Create a pipe to ingest JSON data
create or replace pipe public.snowpipe2 auto_ingest=true as
    copy into public.twitter
    from @public.snowstage/twitter
    file_format = (type = 'JSON');

show pipes;
show pipes;

--Check COPY_HISTORY
select *
from table(information_schema.copy_history(table_name=>'twitter', start_time=> dateadd(hours, -1, current_timestamp())));

select count(*) from  public.twitter;


select
to_date(json:created_at) date_id,
json:user:name::string user_name,
coalesce(json:extended_tweet:full_text::string, json:text::string) tweet
from public.twitter;

--S3 > COPY INTO TARGET, THEN delete to remove duplicates


-- REQUIRMENT: Top Hashtags
SELECT
--to_date(json:created_at) date_id,
--substring(json:created_at::string, 12,5) time_id,
a.value:text::string hashtag,
count(*)
FROM tweets,
lateral flatten( input => json:entities:hashtags ) a
GROUP BY 1
ORDER BY 2 desc
;
Ported from private Knowledge Base pages 2019+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment