Skip to content

Instantly share code, notes, and snippets.

@joanteixi
joanteixi / Synapse Serverless CREATE EXTERNAL TABLE from DELTA
Last active May 24, 2023 11:59
Create external table from Delta for Synapse Serverless to a Storage Account
CREATE MASTER KEY ENCRYPTION BY PASSWORD='dwdweoifjidw$_fwefjwoifj%%123';
CREATE DATABASE SCOPED CREDENTIAL ServiceIdentity WITH IDENTITY = 'Managed Identity';
CREATE schema stg
GO
CREATE SCHEMA silver
GO
@joanteixi
joanteixi / query.kql
Created March 15, 2023 15:48
KQL get rows copied in datafactory
ADFActivityRun
| where ActivityType == 'Copy'
| where Status == 'Succeeded'
| where EffectiveIntegrationRuntime == 'IRAzureVM'
| extend d = parse_json(Output)
| project TimeGenerated, CorrelationId, d.rowsCopied, ActivityRunId, PipelineRunId, PipelineName
@joanteixi
joanteixi / deleteAll.sql
Created March 6, 2023 14:58
T-SQL: Delete All Values in Tables
DECLARE @TableName NVARCHAR(128)
DECLARE @SQL NVARCHAR(500)
DECLARE TableCursor CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
@joanteixi
joanteixi / addStructure.py
Created December 2, 2022 10:32
Add databriks structure to field when it is null and not null in some rows.
def addStructure(df, field, structFields):
'''
Receive a df, a field and structure in a dictionary.
'''
# compruebas si todos los valores son nulos, pq cuando lo son no funciona el replace de null por una estructura.
validator = df.select(df[field]).distinct()
if validator.schema[field].dataType.typeName() == 'string':
otherwiseField = None
else:
otherwiseField = df[field]
@joanteixi
joanteixi / read_csv_file.py
Created October 25, 2022 14:39
Spark read csv file from line N
# reads a csv
def read_csv_from_rown_number(path, row_number = 1, sep=';', schema = None, ):
# load csv as raw text abd add a Id clumns
df = spark.read.text(path)
df = df.withColumn('idRowNbField', f.monotonically_increasing_id())
df = df.where('idRowNbField >= %s' % row_number)
#extract header and clean header
header = df.limit(1).select('value').collect()[0]
header_list= list(header.asDict()['value'].split(sep))
@joanteixi
joanteixi / get_storage_size.ps1
Created October 4, 2022 08:43
Azure Storage get size by folder
# Run first "connect-azAccount" to set AAD user authentication
$filesystemName = 'storageFilesystem'
$path = 'folder'
$storageName = 'storagName'
$ctx = New-AzStorageContext -StorageAccountName $storageName -UseConnectedAccount
$Files = Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem $filesystemName -Path $path -Recurse | Where-Object IsDirectory -eq $false
@joanteixi
joanteixi / mount.py
Created August 8, 2022 06:59
Databricks mount Azure Storage with pyspark version
def mountContainer(appId, secret, tenantId, storageName, containerName, mountPoint):
configs = {"fs.azure.account.auth.type": "OAuth",
"fs.azure.account.oauth.provider.type": "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
"fs.azure.account.oauth2.client.id": appId,
"fs.azure.account.oauth2.client.secret": secret,
"fs.azure.account.oauth2.client.endpoint": f"https://login.microsoftonline.com/{tenantId}/oauth2/token"}
# Optionally, you can add <directory-name> to the source URI of your mount point.
dbutils.fs.mount(
source = f"abfss://{containerName}@{storageName}.dfs.core.windows.net/",
@joanteixi
joanteixi / readme.md
Created June 10, 2022 16:37
Change Keys in Selfhosted Integration Runtime

Open file C:\Program Files\Microsoft Integration Runtime\5.0\PowerShellScript\RegisterIntegrationRuntime.ps1 Modify the parameters:

image

And execute (with the selfhostes integration runtime Agent Enabled!)

@joanteixi
joanteixi / devops.yml
Created June 8, 2022 06:51
Azure Devops snippets
# Task to list files in dir...
- script: |
tree
workingDirectory: $(Agent.BuildDirectory)\$(artifactPipe)
@joanteixi
joanteixi / compact_wsl.ps1
Last active January 17, 2022 18:30
Compact WSL disk space
cd C:\Users\joan.teixido\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState
wsl --shutdown
optimize-vhd -Path .\ext4.vhdx -Mode full
cd C:\Users\joan.teixido\AppData\Local\Docker\wsl\data
optimize-vhd -Path .\ext4.vhdx -Mode full