Skip to content

Does S3Tbales feature support concept of Primary Key

0

I am using spark to insert data into S3 tables from S3 buckets and want to enforce unique constraint on a combination of 2 keys. E.g Key1 and Key2. Essentially want to set primary key to be combination of both the keys). Could this be used in upsert operations? . Not sure if this is allowed though in S3 tabes.

Thanks

asked 2 years ago438 views

2 Answers
2

Amazon S3 Tables does not inherently support the concept of primary keys or unique constraints directly. However, you can achieve similar functionality using Delta Lake with AWS Glue. Delta Lake supports upsert operations (insert, update, delete) and can enforce unique constraints on combinations of columns:

  1. Convert Your Data to Delta Format: Use Spark to read your data from S3 and write it to a Delta table.

  2. Define Unique Constraints: When creating the Delta table, define the primary key or unique constraint on the combination of Key1 and Key2.

  3. Perform Upsert Operations: Use Delta Lake's upsert functionality to insert new data or update existing data while enforcing the unique constraint.

from delta import *
from pyspark.sql.session import SparkSession

# Initialize Spark Session with Delta Lake configuration
spark = SparkSession.builder \
    .config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension") \
    .config("spark.sql.catalog.spark_catalog", "org.apache.spark.sql.delta.catalog.DeltaCatalog") \
    .getOrCreate()

# Read data from S3
inputDF = spark.read.format("csv").option("header", "true").load('s3://your-bucket/data.csv')

# Write data to Delta table
inputDF.write.format("delta").mode("overwrite").save("s3://your-bucket/delta-table")

# Perform upsert operation
inputDF.write.format("delta").mode("overwrite").saveAsTable("delta-table")

EXPERT

answered 2 years ago

0

Hello,

Thanks for reaching out to us at AWS re:Post. Dhivyaa this side from S3 Support Team and here to assist further on the question you asked here.

As from the post, I understand that you are using spark to insert data into S3 tables from S3 buckets and want to enforce unique constraint on a combination of 2 keys. Hence, you would like to know if S3Tables feature supports the concept of primary key. Please correct me if I have misunderstood your query.

AWS

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.