Skip to content

Deequ - how to compute file freshness

0

I'm trying to read parquet files from s3. I want to have a metric to measure data freshness. How can I do this using pyDeequ. My data doesnt have any created timestamp, so I was wondering if I can use the folder timestamp to compute data freshness

asked 2 years ago243 views

2 Answers
0

Hi,

Did you envision to use AWS Glue Data Quality from the AWS Glue Data Catalog to obtain the metrics that you need ?

See https://aws.amazon.com/blogs/big-data/getting-started-with-aws-glue-data-quality-from-the-aws-glue-data-catalog/ for all details.

Best,

Didier

EXPERT

answered 2 years ago

  • Hi Didier

    My initial approach was using Glue Data Quaility.

    Below is the rule for Filefreshness and Filesize, bucket consist of parquet of files:

    FileSize "s3://bucket/" < 25 GB, FileFreshness "s3://bucket" > (now() - 48 hours)

    But the rule errors out with the Failure reason: "Rule (or nested rule) not supported due to: Rule type not recognized"

    Not sure what is the issue, I have all the permissions required for it.

  • I ran these exact rules (with my own bucket)

    Rules = [
        FileSize "s3://sam-bucket/" < 25 GB,
        FileFreshness "s3://sam-bucket/" > (now() - 48 hours)
    ]
    
    and they both passed
    
0

To compute file freshness for your parquet files stored in S3 using pyDeequ, you can leverage the S3 file metadata, specifically the last modified time of the files or folders. Since your data doesn't have a created timestamp, using the folder timestamp is a good alternative approach.

Here's how you can achieve this:

  1. First, you'll need to read the S3 file metadata to get the last modified time. You can do this using the AWS SDK for Python (boto3).

  2. Once you have the timestamp, you can add it as a new column to your DataFrame.

  3. Then, you can use pyDeequ to define and compute a DataFreshness metric based on this new column.

Here's a general outline of the process:

  1. Get the S3 file metadata:

    import boto3
    
    s3 = boto3.client('s3')
    response = s3.head_object(Bucket='your-bucket-name', Key='path/to/your/folder/')
    last_modified = response['LastModified']
  2. Read your parquet files and add the timestamp:

    from pyspark.sql.functions import lit
    from awsglue.context import GlueContext
    from pyspark.context import SparkContext
    
    sc = SparkContext.getOrCreate()
    glueContext = GlueContext(sc)
    spark = glueContext.spark_session
    
    df = spark.read.parquet("s3://your-bucket-name/path/to/your/folder/")
    df = df.withColumn("file_ts", lit(last_modified))
  3. Use pyDeequ to compute the DataFreshness metric:

    from pydeequ.checks import *
    from pydeequ.verification import *
    
    check = Check(spark, CheckLevel.Warning, "Data Freshness Check")
    check.hasDataFreshness("file_ts", lambda x: x < timedelta(hours=24), "Data should be less than 24 hours old")
    
    verificationResult = VerificationSuite(spark) \
        .onData(df) \
        .addCheck(check) \
        .run()

This approach allows you to measure data freshness based on the folder timestamp, even when your data doesn't contain a created timestamp. The DataFreshness check in pyDeequ will evaluate whether the files in your S3 folder have been modified within the specified time frame (in this example, within the last 24 hours).

Remember to adjust the time threshold in the DataFreshness check according to your specific requirements. Also, ensure you have the necessary permissions to access the S3 bucket and read the file metadata.

Sources
DataFreshness - AWS Glue
Using the Parquet format in AWS Glue - AWS Glue

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.