Niks
Niks

Reputation: 1007

How to create table with GUID / Autogenerate key in DataBricks

I am working on AWS databricks version of apache spark. Would like to create table schema's with primary key and foreign keys. I believe GUID or Autogenerate key is best practice to create any primary key. But how to create on databrick, I am looking for. Help appreciated

Upvotes: 1

Views: 2529

Answers (1)

Rahul
Rahul

Reputation: 767

Try this

 df = spark.table('your table name')

import uuid
from pyspark.sql.functions import udf

uuidUdf= udf(lambda : str(uuid.uuid4()),StringType())
df = df.withColumn("id",uuidUdf())

Upvotes: 2

Related Questions