# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # SQLite

# Beispiel mit SQLite, ohne Server

# %load_ext sql
# %sql sqlite://

# + language="sql"
# CREATE TABLE writer (first_name, last_name, year_of_death);
# INSERT INTO writer VALUES ('William', 'Shakespeare', 1616);
# INSERT INTO writer VALUES ('Bertold', 'Brecht', 1956);
# -

# %sql select * from writer

# #Mit MySQL

# Voraussetzung, der MySQL Server läuft!

# %sql mysql://tester:test@localhost/test_db

# + language="sql"
# CREATE TABLE `test_db`.`writer` (`first_name` VARCHAR(30) NOT NULL,`last_name` VARCHAR(45) NULL,
#                                  `year_of_death` DATETIME NULL, PRIMARY KEY (`first_name`));

# + language="sql"
# INSERT INTO writer VALUES ('William', 'Shakespeare', '1616-01-01 00:00');
# INSERT INTO writer VALUES ('Bertold', 'Brecht', '1956-01-01 00:00');
# -

# %sql select * from `test_db`.`writer`;

# %sql DROP TABLE `test_db`.`writer`

# #MongoDB

# Verbindung herstellen

# +
import pymongo

# Connection to Mongo DB
try:
    conn=pymongo.MongoClient()
    print ("Connected successfully!!!")
except (pymongo.errors.ConnectionFailure, e):
   print ("Could not connect to MongoDB: %s" % e )
conn
# -

# Nun mal etwas rumprobieren:

db = conn.mydb
collection = db.my_collection
doc1 = {"first_name":"William","last_name":"Shakespeare","year_of_death":"1616"}
doc2 = {"first_name":"Bertold","last_name":"Brecht","year_of_death":"1956"}
collection.insert(doc1)
collection.insert(doc2)

conn.database_names()

db.collection_names()

collection.find_one()

list(collection.find())[:2]

collection.find({'year_of_death':'1956'}).count()

# # Redis

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)

r.set('<NAME>', '1616')
r.set('<NAME>', '1956')

import pickle


class PicklePerson(object):
    def __init__(self, first_name, last_name, year_of_death):
        self.first_name = first_name
        self.last_name = last_name
        self.year_of_death = year_of_death
    
    def __repr__(self):
        return "Name: " + self.first_name + " " + self.last_name + \
            "\n" + "Death: " + self.year_of_death


bob = pickle.dumps(PicklePerson("William","Shakespeare","1616"))
bert = pickle.dumps(PicklePerson("Bertold","Brecht","1956"))
r.set("bob", bob)
r.set("bert", bert)

pickle.loads(r.get("bob"))

# + active=""
# r.get('1616')
# -

print(r)

# #etcd

import etcd
client = etcd.Client(host='127.0.0.1', port=4001)

# Einen Key schreiben

client.write('/nodes/n1', 1)
client.write('/nodes/n2', 2)

# Den Key lesen

client.read('/nodes/n1').value

# +
directory = client.get("/nodes")

# loop through directory children
for result in directory.children:
  print(result.key + ": " + result.value)
# -

# Ein paar Infos zum Cluster

client.machines



StarcoderdataJupyterScriptsDedupFiltered