GithubHelp home page GithubHelp logo

devtocosmodb's Introduction

title published description tags
Uploading your JSON data to Azure Cosmos DB for MongoDB API
false
Azure April post on uploading data to Cosmos DB
azure, mongodb, cosmosdb, azureapril

If you have built an application and are currently storing the data in a static JSON file, you may want to consider the MongoDB API for Microsoft Azure's Cosmos DB. You will have the document data storage you require for your application with the full management of Microsoft Azure with Cosmos DB along with the ability to scale out globally. This will permit you to create replication to regions where your customers are.

Image by Microsoft

Today we'll take an existing database from an application and use some simple native MongoDB tools to teach you how to easily move some data over to Cosmos DB.

Required:

This example will focus on using a bash terminal which is possible on Mac OS X, Linux and Windows using Windows Subsystem for Linux.

To get started, the JSON file that's provided in the repository is a pretty simple dummy data DB created using json-generator.com.

Example JSON Document

You can create your own if you like, but to make it simple this tutorial provides a 1052 entry example document database to import into Cosmos DB.

Go to the Azure Portal and create an Azure Cosmos DB API for MongoDB deployment for our data.

It's very easy to get started by just typing "Cosmos Azure DB" in the search bar, you can then click on the selection that comes up under "Services."

Search in Azure Portal

Now click Add on the upper left and you'll be brought to the blade where you can begin selecting your new Cosmos DB account. There you can select a subscription you have linked to your account and select a resource group or create a new one. An account name will represent what hostname will be added to the fully qualified domain name created as part of the endpoint you'll connect your applications to. In this example a new resource group is created along with selecting the Azure Cosmos DB for MongoDB API and a region the data will be hosted in.

For this example we won't enable either Geo-Redundancy or Multi-Region writes, these are great ways to expand your data's availability but aren't necessary for a demo.

Adding Cosmos DB

Click next and move on to the Network section, in this section, we'll need to create a virtual network and then permit ourselves to connect to our API endpoint.

We begin by creating a new VNET and selecting the 10.2.0.0/16 network. You will create a default subnet for our tutorial. In more complex solutions, you may want to create the subnet for this to be private only to further restrict your data's exposure. For now this example will use a single 10.2.0.0/24 subnet. Make sure you permit your IP so that you are able to access the endpoint created.

Adding VNET 1 Adding VNET 2

Click Review and Create your new Cosmos DB deployment and validate the settings. Now click Create and wait for your new deployment to be created.

Deploying Cosmos DB

Now let's make sure you've cloned the repository with your database example file:

git clone [email protected]:jaydestro/devtocosmodb.git

Now enter the directory and ensure you can start using the mongo utilities we need:

cd devtocosmodb
mongoimport --version

You should get some form of output like depending on what OS you are using for this tutorial:

mongoimport version: 4.0.2
git version: homebrew
Go version: go1.11
   os: darwin
   arch: amd64
   compiler: gc
OpenSSL version: OpenSSL 1.0.2r  26 Feb 2019

By now the Cosmos DB deployment should be ready to go and get the connection string you need in order to import the data.

created

Click go to resource and find the Cosmos DB portal for your new database where we can begin storing collections of documents we can query using the MongoDB API.

Click Quickstart and find the "Others" section, this will provide you with the login information required to use mongoimport and import your json.

Quickstart info

Now import example-docs.json file to Cosmos DB using mongoimport :

mongoimport -h exampledevto.documents.azure.com:10255 \
 -d exampledevto -c example -u exampledevto \
  -p  YOURPASSWORDHERE \
  --ssl --jsonArray  --file example-docs.json 

An expected output should look something like this:

2019-04-10T13:06:08.163-0400	connected to: exampledevto.documents.azure.com:10255
2019-04-10T13:06:11.051-0400	[##############..........] exampledevto.example	1.36MB/2.25MB (60.5%)
2019-04-10T13:06:14.155-0400	[########################] exampledevto.example	2.25MB/2.25MB (100.0%)
2019-04-10T13:06:14.155-0400	imported 1051 documents

Now let's look at the new collection and documents in the Data Explorer within the portal in Cosmos DB:

Data Explorer

Now go to the "Connection String" section to start connecting this data to an application and select your favorite language such as JavaScript or Golang to query and manipulate the data.

devtocosmodb's People

Contributors

jaydestro avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.