Search

Showing posts with label databricks. Show all posts
Showing posts with label databricks. Show all posts

Wednesday, 26 April 2023

How to create / register and drop a UDF in Databricks Spark using Python

Below is an example of how to create and drop a UDF in Python using Databricks:

Creating a UDF:

from pyspark.sql.functions import udf
from pyspark.sql.types import IntegerType
# Define the function you want to use in your UDF
def multiply_by_two(x):
    return x * 2
# Create the UDF using the function you defined
multiply_by_two_udf = udf(multiply_by_two, IntegerType())
# Register the UDF so it can be used in your Spark SQL queries
spark.udf.register("multiply_by_two", multiply_by_two_udf)


Dropping a UDF:

# Unregister the UDF so it can no longer be used in your Spark SQL queries
spark.catalog.dropTempView("multiply_by_two")

Note that in the above examples, we're using spark to access the SparkSession object, which is the entry point to using Spark functionality. If you're running this code in a Databricks notebook, the SparkSession object is automatically created for you.

Tuesday, 25 April 2023

SUBSTRING_INDEX function in Databricks Spark

SUBSTRING_INDEX is a string manipulation function that can be used in Databricks, which is a cloud-based big data processing platform based on Apache Spark.

The SUBSTRING_INDEX function allows you to extract a substring from a string by specifying a delimiter and the number of occurrences of the delimiter to consider from the left or right of the string. The syntax for the function is as follows:

SUBSTRING_INDEX(string, delimiter, count)

Where:
string: the input string to extract the substring from.
delimiter: the delimiter used to split the input string.
count: the number of occurrences of the delimiter to consider. If count is positive, the function will extract the substring from the left of the string, starting from the beginning. If count is negative, the function will extract the substring from the right of the string, starting from the end.
Here is an example usage of SUBSTRING_INDEX in Databricks:

%sql
SELECT SUBSTRING_INDEX('www.example.com', '.', 2)

This would return the output www.example as it extracts the first two occurrences of the delimiter "." from the left of the input string "www.example.com".