In this article, you will see how to query JSON file stored in Amazon S3 using S3 Select. AWS Lambda project (.NET Core – C#) and AWS SDK for .NET are used to query JSON file from Amazon S3 using S3 Select.
Amazon S3 Select
With Amazon S3 Select, you can use simple structured query language (SQL) statements to filter the contents of an Amazon S3 object and retrieve just the subset of data that you need. By using Amazon S3 Select to filter this data, you can reduce the amount of data that Amazon S3 transfers, which reduces the cost and latency to retrieve this data.
Amazon S3 Select works on objects stored in CSV, JSON, or Apache Parquet format. It also works with objects that are compressed with GZIP or BZIP2 (for CSV and JSON objects only), and server-side encrypted objects. You can specify the format of the results as either CSV or JSON, and you can determine how the records in the result are delimited.
Refer this blog to learn more about how Amazon S3 Select Support in the AWS SDK for .NET.
Prerequisites
- Download and install Visual Studio 2022.
- Download and install AWS Toolkit for Visual Studio 2022.
- An Active AWS Account. (See AWS Account Page.)
- User with sufficient access to create AWS resources for this article.(See IAM role in AWS IAM Roles Documentation.)
- User should have programmatic access keys. (See IAM user and Access in the AWS IAM documentation.)
- Download and install Amazon command line interface (AWS CLI). Configure AWS CLI.
- Create S3 bucket in AWS Console and upload sample JSON.
Sample JSON
Create AWS Lambda Project (.NET Core - C#) using Visual Studio
- Open Visual Studio 2022.
- Click Create a new project.
- Search and select AWS Lambda Project (.NET Core – C#) project template. Click Next.
- Enter the project name and click Create.
- Select Empty Function blueprint and click Finish.
Implementation
Add AWSSDK.S3 NuGet Package.
Open Function.cs and replace the code with the following.
using Amazon;
using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.S3.Model;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace S3SelectJSONDemo;
public class Function {
private AmazonS3Client s3Client;
public Function() {
s3Client = new AmazonS3Client(RegionEndpoint.EUWest2);
}
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public async Task FunctionHandler(ILambdaContext context) {
var response = await s3Client.SelectObjectContentAsync(new SelectObjectContentRequest() {
Bucket = "sqlreplicajson",
Key = "MarketDataOneLakh.json",
ExpressionType = ExpressionType.SQL,
Expression = "select s.Quantity,s.Instrument from S3Object[*].marketData[*] s",
InputSerialization = new InputSerialization() {
JSON = new JSONInput() {
JsonType = JsonType.Document
}
},
OutputSerialization = new OutputSerialization() {
JSON = new JSONOutput()
}
});
foreach(var ev in response.Payload) {
Console.WriteLine($ "Received {ev.GetType().Name}!");
if (ev is RecordsEvent records) {
Console.WriteLine("The contents of the Records Event is...");
using(var reader = new StreamReader(records.Payload, System.Text.Encoding.UTF8)) {
Console.Write(reader.ReadToEnd());
}
}
}
}
}
Testing
- Run Mock Lambda Test Tool (Hit F5) to run Lambda locally from Visual Studio.
- In Mock Lambda Test Tool, click Execute Function and view the result in Log Output.
Clean up Resources
Delete all the resources to ensure that you're not charged for any services you aren't using.
Summary
Thus, in this article, you saw how to query JSON file stored in Amazon S3 using S3 Select.