43

Build a Low Latency, Globally Distributed Python App using Macrometa's G...

 4 years ago
source link: https://www.tuicool.com/articles/baANzyy
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this tutorial, I'm going to show you how to quickly and easily build a distributed database backend that spans across North America (Los Angeles & Ashburn VA), Europe (Frankfurt) and Asia (Mumbai, India).

We're going to build simple python application on Macrometa's serverless geo-distributed database cloud ( https://www.macrometa.co ) to create a data serving backend that will span across multiple worldwide data centers.

This is so that when end users run your app, it doesn't need to fetch data from a database that is thousands of miles and hundreds of milliseconds away but instead gets its data from the closest running database in a region closest to it.

So if you're wondering what Macrometa is? Macrometa is a geo-distributed database cloud service and available for free for developers to build apps on. Macrometa provides the following capabilities:

  1. A Serverless real-time NoSQL geo-distributed database that serves data as Key/Value pairs, JSON Documents, Streams, Graphs and time series. The geo-distributed bit is important here because that what makes Macrometa special - it can very efficiently, quickly and accurately replicate and synchronize your data across 100s of worldwide locations while hiding the complexities in providing correct concurrent behavior.
  2. A fully integrated Stream engine to let you query and manipulate data in motion without needing to wait for it to be ingested into the database (I will do a separate tutorial on this in a few days).
  3. A serverless compute runtime that provides a Web Assembly in V8 engine for you to run your code with hyper-locality to your data (this will be coming out in the second half of 2019 and is not in the currently available product)/

Learn more at https://www.macrometa.co

So with that background - let's dive right in.

There are 2 ways to do this tutorial:

  1. For those who want to code - the repl.it below is for you. You can see how to build a simple application to call Macrometa's APIs in Python to do interesting things.
  2. For those of you who prefer a web console or GUI - go to https://www.macrometa.co/quickstart

Building an application consists of 3 basic steps:

Essential Reading : Learn React from Scratch! (2019 Edition)
  1. Create a geo fabric or use the global fabric.
  2. Create a collection for your data (a collection is like a table in SQL).
  3. Send queries to CReate, Update, Delete (CRUD) documents (rows) in your collection.
from c8 import C8Client
import json

print("Connecting to Macrometa global Fabric ...")
client = C8Client(protocol='https', host='try.macrometa.io', port=443)
#Add your tenant details, user id and password below
fabric = client.fabric(
    tenant='OutKast630',
    name='_system',
    username='root',
    password='KleverP@$$word')
print("\nConnected to Macrometa global Fabric ...")

print("\nGetting geo fabric details...")
print(json.dumps(fabric.fabrics_detail(), indent=4))

# creating the address book collection & add some contacts if it doesnt exist
if not fabric.has_collection('addresses'):
    addresses = fabric.create_collection('addresses')
    print("\n\nCreated addresses collection...")

else:
    print("\n\naddresses collection exists")
    addresses = fabric.collection('addresses')

    print("\n\n Inserting contacts into addresses")
cursor = fabric.c8ql.execute(
    'FOR persons IN [ { firstname: "Joseph", lastname: "Smith", email: "[email protected]" },{ firstname: "Astrid", lastname: "Young", email: "[email protected]" },{ firstname: "Boris", lastname: "Balastikov", email: "[email protected]" },{ firstname: "Sherlock", lastname: "Jones", email: "[email protected]" },{ firstname: "Alpha", lastname: "Simpson", email: "[email protected]" },{ firstname: "Jose", lastname: "Garcia", email: "[email protected]" },{ firstname: "Lee", lastname: "Ki", email: "[email protected]" },{ firstname: "Mark", lastname: "Goldfine", email: "[email protected]" },{ firstname: "Ramesh", lastname: "Sriram", email: "[email protected]" }] INSERT persons INTO addresses'
)
docs = [document for document in cursor]
print("\n\nInserted some contact data...")

#Run a query using the built in query language C8QL
print("\n\nExecuting C8QL to retrieve all contacts...")
cursor = fabric.c8ql.execute('FOR contact IN addresses RETURN contact')
docs = [document for document in cursor]
print(json.dumps((docs), indent=4))

#Truncate the collection so that we leave it all cleaned up for the next person
print("\n\nTruncating collection to zero docs ...")
addresses.truncate()
cursor = fabric.c8ql.execute('FOR contact IN addresses RETURN contact')
docs = [document for document in cursor]
print(json.dumps((docs), indent=4))
print("\nCollection truncated ...")

print("And we're done!")

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK