Skip to main content

Fused in 5 Minutes

Welcome to Fused! This tutorial will get you up and running in just 5 minutes with three simple examples that demonstrate the core concepts of User Defined Functions (UDFs).

What is a UDF?

A User Defined Function (UDF) is a Python function decorated with @fused.udf that can be run anywhere - in the cloud, on your local machine, or embedded in web applications. Read more about why UDFs.

Example 1: Hello World UDF

Let's start with the simplest possible UDF:

import fused

@fused.udf
def udf(x: int = 1):
return f"Hello world {x + x}"

fused.run(udf)

What this does:

Try it:

  1. Copy this code into Fused Workbench
  2. Click "Run" to execute it
  3. You'll see the output: "Hello world 2"

Example 2: Simple Data UDF

Now let's create a UDF that returns structured data:

@fused.udf
def udf(x: int = 5):
import pandas as pd
return pd.DataFrame({"x": range(x)})

What this does:

  • Creates a pandas DataFrame with numbers from 0 to x-1
  • Returns structured data that can be visualized or processed further
  • Default value creates a DataFrame with 5 rows

Try it:

  1. Paste this code into a new UDF in Workbench
  2. Run it to see a table with values 0, 1, 2, 3, 4
  3. Try changing the parameter x to see different amounts of data

Example 3: Simple Data File UDF

Finally, let's read real data from a file:

@fused.udf
def udf(x: int = 10):
import pandas as pd
# Read from a public dataset
df = pd.read_parquet("s3://fused-asset/infra/building_msft_us.parquet")
return df.head(x)

What this does:

  • Reads a parquet file from cloud storage (S3)
  • Returns the first x rows of the dataset
  • This example uses Microsoft building footprint data

Try it:

  1. Paste this code into Workbench
  2. Run it to see building data
  3. Try the File Explorer in Workbench to browse other available datasets

Next Steps

Congratulations! You've just created your first three UDFs. Here's what you can explore next:

  • File Explorer: Browse available datasets in the Fused catalog
  • Map View: Visualize geospatial data on an interactive map
  • Sharing: Share your UDFs with others using shareable URLs
  • Advanced Features: Explore caching, async operations, and more complex data transformations

Key Concepts Learned

UDF Decorator: @fused.udf transforms any Python function into a distributed, scalable function
Type Hints: Parameters should have type hints and default values
Data Returns: UDFs can return strings, DataFrames, or other data structures
Cloud Integration: Easily read from cloud storage like S3

Ready to build more complex UDFs? Check out our Core Concepts and Examples sections!