Skip to content

Instantly share code, notes, and snippets.

View valerybriz's full-sized avatar
🐍
Writing poetry in Python

Valery Briz valerybriz

🐍
Writing poetry in Python
View GitHub Profile
@valerybriz
valerybriz / pyspark_elastic_bulk_loader.py
Last active June 1, 2023 13:46
Pyspark Elastic Bulk Loader
import sys
from pyspark.sql import SparkSession
import logging
APP_NAME = "Elastic Bulk loader"
INPUT_PATH = "gs://PATH/TO/THE/PARQUETS"
OUTPUT_PATH = "gs://PATH/TO/THE/PARQUETS"
ELASTIC_HOST = "http://localhost"
WRITE_MODE = "append"
@valerybriz
valerybriz / snowflake_query_costs.sql
Last active September 17, 2022 11:49
Queries to monitor snowflake costs and queries
-- monitorear queries con los costos más altos
WITH WAREHOUSE_SIZE AS
(
SELECT WAREHOUSE_SIZE, NODES
FROM (
SELECT 'XSMALL' AS WAREHOUSE_SIZE, 1 AS NODES
UNION ALL
SELECT 'SMALL' AS WAREHOUSE_SIZE, 2 AS NODES
UNION ALL
SELECT 'MEDIUM' AS WAREHOUSE_SIZE, 4 AS NODES
@valerybriz
valerybriz / rename_table.py
Last active June 1, 2020 23:03 — forked from dan-hook/rename_table.py
"Rename" an AWS Glue table by creating a new table
import boto3
from settings import AWS_PROFILE
database_name = "test"
new_database_name = "newdb"
table_name = "table_name"
new_table_name = "new_table_name"
session = boto3.session.Session(profile_name=AWS_PROFILE)
client = session.client("glue")
# Hits the database
team_queryset = Team.objects.select_related("user").get(name="tigers")
# The second line doesn't hit the database, because team_queryset.user has been previously prepopulated
user = team_queryset.user
# Hits the database
team_queryset = Team.objects.get(name="tigers")
# Hits the database again to get the related User objects
user = team_queryset.user
Team.objects.all().prefetch_related('users')
user_queryset = User.objects.all()
# The first hit to the database confirms if at least one object exists.
if user_queryset.exists():
# Another database hit to start fetching the rows in batches.
for user in user_queryset.iterator():
print(user.name)
for user in User.objects.exclude(email__isnull=True).iterator():
print(user.name)
@valerybriz
valerybriz / create_2.py
Created May 2, 2019 21:53
Create the query filtering teams named tigers
# Create the query filtering teams named tigers
team = Team.objects.get(name="tigers")
team.users.all() # query performed
team.users.all() # query performed again
@valerybriz
valerybriz / create.py
Created May 2, 2019 21:52
Create the query filtering users
# Create the query filtering users with email [email protected]
users = User.objects.get(email="[email protected]")
users.name # Hit the database and retrieve the name value
users.name # cached version, no database access