NoSQL Databases Categories:
- Key-value stores ---> Redis, DynamoDB, Riak,... etc
- Column-oriented ---> BigTable, Cassandra, SimpleDB,... etc.
- Graph ---> OrientDB, Neo4J, Titan,...etc
- Document oriented ---> MongoDB, CouchDB,... etc
- In order to work with Key-value store databases, here i discus only one database is DynamoDB, first we are install pynamodb.
- PynamoDB provides an ORM like interface to DynamoDB and supports both Python 2 and Python 3.
- The entire DynamoDB API is supported by PynamoDB - including global and local secondary indexes, batch operations, binary attributes, queries, scans, etc.
- You can use Dynamodb-mapper Python library. It's a simple/tiny abstraction layer that allows you to map plain Python object to DynamoDB. It also features a transaction engine.
- For advanced tasks such as table management it is still better to directly use Boto
pip install pynamodb
Creating a model:
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute
class UserModel(Model):
"""
A DynamoDB User
"""
class Meta:
table_name = 'dynamodb-user'
region = 'us-west-1'
email = UnicodeAttribute(hash_key=True)
first_name = UnicodeAttribute()
last_name = UnicodeAttribute()
create the table
UserModel.create_table(read_capacity_units=1, write_capacity_units=1)
Working with column databases:
- in order to work with cassandra database,we need install cassandra-driver.
pip install cassandra-driver
Connecting to Cassandra
from cassandra.cluster import Cluster
cluster = Cluster()
(or)
from cassandra.cluster import Cluster
cluster = Cluster(['192.168.0.1', '192.168.0.2'])
Instantiating a Cluster does not actually connect us to any nodes.
To establish connections and begin executing queries we need a Session,
which is created by calling Cluster.connect():
cluster = Cluster()
session = cluster.connect()
Executing Queries
rows = session.execute('SELECT name, age,email FROM users')
for user_row in rows:
print user_row.name, user_row.age, user_row.email
Working with Graph databases:
pip install neo4j-driver
from neo4j.v1 import GraphDatabase, basic_auth
driver = GraphDatabase.driver("bolt://localhost:7687",
auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
session.run("CREATE (a:Person {name: {name}, title: {title}})",
{"name": "Arthur", "title": "King"})
result = session.run("MATCH (a:Person) WHERE a.name = {name} "
"RETURN a.name AS name, a.title AS title",
{"name": "Arthur"})
for record in result:
print("%s %s" % (record["title"], record["name"]))
session.close()
pip install py2neo
from py2neo import Graph, Path
graph = Graph()
tx = graph.cypher.begin()
for name in ["Alice", "Bob", "Carol"]:
tx.append("CREATE (person:Person {name:{name}}) RETURN person", name=name)
alice, bob, carol = [result.one for result in tx.commit()]
friends = Path(alice, "KNOWS", bob, "KNOWS", carol)
graph.create(friends)
Working with document oriented databases:
In order to work with mongoDb database,
first we need to install pymongo .
pip install pymongo
import pymongo
Making a Connection with MongoClient
from pymongo import MongoClient
client = MongoClient()
client = MongoClient('localhost', 27017)
client = MongoClient('mongodb://localhost:27017/')
Getting a Database
db = client.test_database
(or)
db = client['test-database']
Getting a Collection
collection = db.test_collection
(or)
collection = db['test-collection']
Documents
import datetime
post = {"author": "Mike",
"text": "My first blog post!",
"tags": ["mongodb", "python", "pymongo"],
"date": datetime.datetime.utcnow()}
Inserting a Document
posts = db.posts
post_id = posts.insert_one(post).inserted_id
post_id
ObjectId('...')
No comments:
Post a Comment