All Projects → Azure-Samples → cosmosdb-materialized-views

Azure-Samples / cosmosdb-materialized-views

Licence: MIT License
A full sample that shows how to implement real-time updated Materalized Views with CosmosDB, Change Feed and Azure Functions

Programming Languages

C#
18002 projects
shell
77523 projects
Dockerfile
14818 projects
Batchfile
5799 projects

Projects that are alternatives of or similar to cosmosdb-materialized-views

jlik.me
URL Shortener project.
Stars: ✭ 31 (+55%)
Mutual labels:  azure-functions, cosmosdb, cosmos-db
smart-store
No description or website provided.
Stars: ✭ 73 (+265%)
Mutual labels:  azure-functions, cosmosdb
memealyzer
Memealyzer is an app built to demonstrate some the latest and greatest Azure tech to dev, debug, and deploy microservice applications.
Stars: ✭ 97 (+385%)
Mutual labels:  azure-functions, cosmos-db
CosmicClone
Cosmic Clone is a utility that can backup\clone\restore a azure Cosmos database Collection. It can also anonymize cosmos documents and helps hide personally identifiable data.
Stars: ✭ 113 (+465%)
Mutual labels:  cosmosdb, cosmos-db
backend-csharp
A C# / Azure Functions implementation of the API for https://www.theurlist.com
Stars: ✭ 35 (+75%)
Mutual labels:  azure-functions, cosmosdb
clean-architecture-azure-cosmos-db
A starting point to build a web API to work with Azure Cosmos DB using .NET 5 and Azure Cosmos DB .NET SDK V3, based on Clean Architecture and repository design pattern. Partition key is also implemented through the repository pattern.
Stars: ✭ 277 (+1285%)
Mutual labels:  azure-functions, cosmos-db
Cosmos.Identity
A Cosmos storage provider for ASP.NET Core Identity.
Stars: ✭ 26 (+30%)
Mutual labels:  nosql, cosmosdb
azure-event-driven-data-pipeline
Building event-driven data ingestion pipelines in Azure
Stars: ✭ 13 (-35%)
Mutual labels:  azure-functions, cosmos-db
MCW-OSS-PaaS-and-DevOps
MCW OSS PaaS and DevOps
Stars: ✭ 49 (+145%)
Mutual labels:  cosmosdb, cosmos-db
soda-for-java
SODA (Simple Oracle Document Access) for Java is an Oracle library for writing Java apps that work with JSON (and not only JSON!) in the Oracle Database. SODA allows your Java app to use the Oracle Database as a NoSQL document store.
Stars: ✭ 61 (+205%)
Mutual labels:  nosql, no-sql
Scavenger
A virtual "scavenger hunt" game for mobile devices using Unity, Azure, and PlayFab
Stars: ✭ 19 (-5%)
Mutual labels:  azure-functions, cosmos-db
fl-image-resize
A library to quickly resize images with Azure Functions
Stars: ✭ 15 (-25%)
Mutual labels:  azure-functions
XamarinAzureChallenge
Create an iOS/Android app using Xamarin.Forms and connect it to a serverless Azure Function
Stars: ✭ 59 (+195%)
Mutual labels:  azure-functions
sync-client
SyncProxy javascript client with support for all major embedded databases (IndexedDB, SQLite, WebSQL, LokiJS...)
Stars: ✭ 30 (+50%)
Mutual labels:  nosql
GitHubReadmeWebTrends
An automated tool created using Azure Functions that double checks each Readme to ensure every repository is leveraging Web Trends
Stars: ✭ 13 (-35%)
Mutual labels:  azure-functions
AzUnzipEverything
A simple Azure Function to Unzip files from a blob storage to another one
Stars: ✭ 24 (+20%)
Mutual labels:  azure-functions
dndb
A Deno 🦕 persistent, embeddable and optimized NoSQL database for JS & TS
Stars: ✭ 64 (+220%)
Mutual labels:  nosql
mongodb-atlas-backup
The human way to mongodump and mongorestore your MongoDB Atlas cluster
Stars: ✭ 29 (+45%)
Mutual labels:  nosql
AzureFunctions.Extensions
This provides some useful extensions for Azure Functions.
Stars: ✭ 81 (+305%)
Mutual labels:  azure-functions
ru102js
Source code for the RU102JS Redis for JavaScript Developers Course
Stars: ✭ 21 (+5%)
Mutual labels:  nosql
page_type languages products description urlFragment
sample
csharp
azure
Shows how materialized view can be kept updated in near-real time using a serverless approach with Azure Functions, Cosmos DB and Cosmos DB Change Feed.
real-time-view-cosomos-azure-functions

Near-Real Time Updated Materialized View With Cosmos DB and Azure Functions

This sample shows how materialized view can be kept updated in near-real time using a completely serverless approach with

  • Azure Function
  • Cosmos DB
  • Cosmos DB Change Feed

The high-level architecture is the following one:

High Level Materialized View Architecture

Device simulator writes JSON data to Cosmos DB into raw collection. Such data is exposed by Cosmos DB Change Feed and consumed by an Azure Function (via Change Feed Processor), that get the JSON document and uses it to create or updated the related materialized views, stored in the view collection.

A more detailed discussion on the architecture and solution can be found here:

Real-Time Materialized Views with Cosmos DB

The sample simulates one or more IoT Devices whose generated data needs to be sent, received and processed in near-real time. In this context, "processed" means:

  • Provide, for each device, the sum of the sent value data and also the last sent value
  • Provide one view with all devices and the last data sent by each one

Sample data

The simulated IoT devices will send this sample data:

{
    "deviceId": "036",
    "value": 164.91290226807487,
    "timestamp": "2019-03-22T19:46:20.8633068Z"
}

Processed data

The resulting processed data for each device will look like the following document:

{
    "id": "030",
    "aggregationSum": 3519.8782286699293,
    "lastValue": 155.41897977488998,
    "type": "device",
    "deviceId": "030",
    "lastUpdate": "2019-03-22T19:50:17Z"
}

The document contain aggregated data for the device specified in id, as long as the aggregated value in aggregationSum and the last sent value in the lastValue field.

The global materialized view is where the last value for each device is stored:

{
    "id": "global",
    "deviceId": "global",
    "type": "global",
    "deviceSummary": {
        "035": 104.3423159533843,
        "016": 129.1018793494915,
        ...
        "023": 177.62450146378228,
        "033": 178.97744880941576
    },
}

Values are updated in near-real time by using the Change Feed feature provided by Cosmos DB. The sample is using processing data coming from the Change Feed every second, but it can easily changed to a much lower value if you need more "real time" updates.

Implementation Notes

Data is partitioned by deviceId in both the raw and view collection to make sure order is preserved within data sent by a single device. Collections do not use indexing as only point-lookups are done and since in this sample Cosmos DB is used a key-value store, switching off indexing allows to spare RUs.

Prerequisites

If you don't have an Azure subscription, create a free account before you begin.

In addition:

Getting Started

Make sure you have WSL (Windows System For Linux) installed and have AZ CLI version > 2.0.50. Before running any script also make sure you are authenticated on AZ CLI using

az login

and have selected the Azure Subscription you want to use for the tests:

az account list --output table
az account set --subscription "<YOUR SUBSCRIPTION NAME>"

Clone the sample project

Clone the repository and open the code-samples directory from your command line tool.

git clone https://github.com/Azure-Samples/cosmosdb-materialized-views
cd cosmosdb-materialized-views

Create Azure Resources

To create and configure the Azure Resources needed for the project, you just have to run the deploy.sh script in the script folder.

Script has been tested on Mac OSX and Ubuntu Bash.

./script/deploy

The following resources will be created:

  • Resource Group
  • Azure Storage
  • Azure Function (using Consumption Plan)
  • Application Insight
  • Cosmos DB with 3 Collections (raw and view with 1000 RU each, leases with 400 RU)

By default all deployed resources will have the mvsample prefix and the location will be eastus. If you don't have any specific naming requirements, by default the generated ROOT_NAME will be uniquified by postfixing random numbers to make sure you don't have any name collision with someone else trying the sample at the same time.

If needed you can change the following defauly settings

export ROOT_NAME='mvsample${UNIQUIFIER}'
export LOCATION='eastus'

in ./script/deploy to make sure they work for you.

Run the Producer application

The producer application will generate sample sensor data as described before. The application takes the device ids to generate as parameter:

cd sensor-data-producer
dotnet run 1

The above sample will generate random data for device id 001. If you want to generate more data just specify how many sensor you need to be simulated:

dotnet run 10

will generate data for 10 sensors, from 001 to 010. If you want to generate more workload and you're planning to distribute the work on different clients (using Azure Container Instances or Azure Kubernetes Service for example), you'll find useful the ability to specify which device id range you want to be generated by each running application. For example

dotnet run 15-25

will generate data with Device Ids starting from 015 up to 025.

Docker Image

If you want to build and run the simulator using Docker, just run build.bat to build the docker image. The run it using

docker run -it iot-simulator 1-10

to simulate device from 001 to 010, for example.

Check results

Once the producer is started you can see the result by using Azure Portal or Azure Storage Explorer to look for document create int the view collection of the created Cosmos DB database.

You can also take a look at the Application Insight Live Metric Streams to see in real time function processing incoming data from the Change Feed

Additional Reference

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].