finish the cassandra setup

This commit is contained in:
Andreas Zweili 2018-09-03 22:24:50 +02:00
parent 992ced99dd
commit 1dc4bfeb0f
4 changed files with 107 additions and 134 deletions

View File

@ -1,62 +0,0 @@
"""
Python by Techfossguru
Copyright (C) 2017 Satish Prasad
"""
import logging
from cassandra.cluster import Cluster, BatchStatement
from cassandra import ConsistencyLevel
from cassandra.query import SimpleStatement
class PythonCassandraExample:
def __init__(self):
self.cluster = None
self.session = None
self.keyspace = None
self.log = None
def __del__(self):
self.cluster.shutdown()
def createsession(self):
self.cluster = Cluster(['localhost'])
self.session = self.cluster.connect(self.keyspace)
def getsession(self):
return self.session
# How about Adding some log info to see what went wrong
def setlogger(self):
log = logging.getLogger()
log.setLevel('INFO')
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(
"%(asctime)s [%(levelname)s] %(name)s: %(message)s"))
log.addHandler(handler)
self.log = log
def insert_data(self):
insert_sql = self.session.prepare(
"INSERT INTO employee (emp_id, ename , sal,city) VALUES (?,?,?,?)")
batch = BatchStatement()
batch.add(insert_sql, (1, 'LyubovK', 2555, 'Dubai'))
batch.add(insert_sql, (2, 'JiriK', 5660, 'Toronto'))
batch.add(insert_sql, (3, 'IvanH', 2547, 'Mumbai'))
batch.add(insert_sql, (4, 'YuliaT', 2547, 'Seattle'))
self.session.execute(batch)
self.log.info('Batch Insert Completed')
def select_data(self):
rows = self.session.execute('select * from employee limit 5;')
for row in rows:
print(row.ename, row.sal)
def update_data(self):
pass
def delete_data(self):
pass

View File

@ -4,9 +4,8 @@ Copyright (C) 2017 Satish Prasad
"""
import logging
from cassandra.cluster import Cluster, BatchStatement
from cassandra import ConsistencyLevel
from cassandra.query import SimpleStatement
from cassandra.cluster import Cluster
import time
class PythonCassandraExample:
@ -14,7 +13,7 @@ class PythonCassandraExample:
def __init__(self):
self.cluster = None
self.session = None
self.keyspace = None
self.keyspace = 'userdb'
self.log = None
def __del__(self):
@ -37,55 +36,31 @@ class PythonCassandraExample:
log.addHandler(handler)
self.log = log
# Create Keyspace based on Given Name
def createkeyspace(self, keyspace):
"""
:param keyspace: The Name of Keyspace to be created
:return:
"""
# Before we create new lets check if exiting keyspace; we will drop
# that and create new
rows = self.session.execute(
"SELECT keyspace_name FROM system_schema.keyspaces")
if keyspace in [row[0] for row in rows]:
self.log.info("dropping existing keyspace...")
self.session.execute("DROP KEYSPACE " + keyspace)
self.log.info("creating keyspace...")
self.session.execute("""
CREATE KEYSPACE %s
WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '2' }
""" % keyspace)
self.log.info("setting keyspace...")
self.session.set_keyspace(keyspace)
def create_table(self):
c_sql = """
CREATE TABLE IF NOT EXISTS employee (emp_id int PRIMARY KEY,
ename varchar,
sal double,
city varchar);
"""
self.session.execute(c_sql)
self.log.info("Employee Table Created !!!")
# lets do some batch insert
def insert_data(self):
insert_sql = self.session.prepare(
"INSERT INTO employee (emp_id, ename , sal,city) VALUES (?,?,?,?)")
batch = BatchStatement()
batch.add(insert_sql, (1, 'LyubovK', 2555, 'Dubai'))
batch.add(insert_sql, (2, 'JiriK', 5660, 'Toronto'))
batch.add(insert_sql, (3, 'IvanH', 2547, 'Mumbai'))
batch.add(insert_sql, (4, 'YuliaT', 2547, 'Seattle'))
self.session.execute(batch)
self.log.info('Batch Insert Completed')
def select_data(self):
rows = self.session.execute('select * from employee limit 5;')
evil_query = (
"select username, hobby from user where hobby='Datenbanken';")
allowed_query = ("select username, hobby from user;")
print()
print()
print()
print()
print()
start_time = time.time()
rows = self.session.execute(allowed_query)
print("--- %s seconds ---" % (time.time() - start_time))
counter = 1
for row in rows:
print(row.ename, row.sal)
counter += 1
print(counter)
print()
print()
print()
print()
print()
try:
rows = self.session.execute(evil_query)
except Exception as e:
print(e.args)
def update_data(self):
pass
@ -98,7 +73,4 @@ if __name__ == '__main__':
example1 = PythonCassandraExample()
example1.createsession()
example1.setlogger()
example1.createkeyspace('techfossguru')
example1.create_table()
example1.insert_data()
example1.select_data()

View File

@ -5,6 +5,41 @@ Copyright (C) 2017 Satish Prasad
"""
import logging
from cassandra.cluster import Cluster, BatchStatement
import string
import random
from random import randint
primary_key = 1
hobbies = []
hobbies.append('Golf')
hobbies.append('Schwimmen')
hobbies.append('Datenbanken')
def randomword(length):
return ''.join(random.choice(
string.ascii_lowercase) for i in range(length))
def create_batch(insert_sql):
global primary_key
counter = 1
batch = BatchStatement()
while 15 > counter:
param_counter = 0
params = []
while 13 > param_counter:
name = randomword(6)
params.append(name)
param_counter += 1
batch.add(insert_sql, (primary_key, params[0], params[1], params[2],
params[3], params[4], params[5], params[6],
params[7], hobbies[randint(0, 2)], params[9],
params[10], params[11], params[12]))
counter += 1
primary_key += 1
return batch
class PythonCassandraExample:
@ -60,20 +95,57 @@ class PythonCassandraExample:
def create_table(self):
c_sql = """
CREATE TABLE IF NOT EXISTS employee (emp_id int PRIMARY KEY,
ename varchar,
sal double,
city varchar);
CREATE TABLE IF NOT EXISTS user (id int PRIMARY KEY,
username varchar,
email varchar,
first_name varchar,
last_name varchar,
middle_name varchar,
street_name varchar,
telephonenumber varchar,
mobile_number varchar,
hobby varchar,
salutation varchar,
gender varchar,
city varchar,
country varchar);
"""
self.session.execute(c_sql)
self.log.info("Employee Table Created !!!")
self.log.info("User Table Created !!!")
# lets do some batch insert
def insert_data(self):
insert_sql = self.session.prepare(
"""
INSERT INTO user (id,
username,
email,
first_name,
last_name,
middle_name,
street_name,
telephonenumber,
mobile_number,
hobby,
salutation,
gender,
city,
country) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)
"""
)
counter = 1
while 10000 > counter:
batch = create_batch(insert_sql)
self.session.execute(batch)
counter += 1
self.log.info('Batch Insert Completed')
# lets do some batch insert
if __name__ == '__main__':
example1 = PythonCassandraExample()
example1.createsession()
example1.setlogger()
example1.createkeyspace('techfossguru')
example1.createkeyspace('userdb')
example1.create_table()
example1.insert_data()
example1.select_data()
#example1.select_data()

View File

@ -6,6 +6,7 @@ mariadb_connection = mariadb.connect(user='vagrant',
cursor = mariadb_connection.cursor()
cursor.execute("""create table if not exists test (
id bigint auto_increment primary key,
username varchar(128),
first_name varchar(128),
middle_name varchar(128)),
last_name varchar(128)),
@ -19,16 +20,6 @@ cursor.execute("""create table if not exists test (
hobby varchar(128)),
gender varchar(128)),
salutation varchar(128)),
last_name varchar(128)),
last_name varchar(128)),
last_name varchar(128)),
last_name varchar(128)),
last_name varchar(128)),
last_name varchar(128)),
last_name varchar(128)),
last_name varchar(128)),
last_name varchar(128)),
last_name varchar(128)),
""")
cursor.execute("""INSERT INTO test (first_name,last_name) VALUES (%s,%s)""",