create the basic setup for the presentation

This commit is contained in:
Andreas Zweili 2018-09-02 21:31:37 +02:00
parent f5e9d1c2ca
commit 737771c11c
5 changed files with 180 additions and 0 deletions

49
Vagrantfile vendored Normal file
View File

@ -0,0 +1,49 @@
# coding: utf-8
# -*- mode: ruby -*-
# vi: set ft=ruby :
BOX_IMAGE = "debian/stretch64"
Vagrant.configure("2") do |config|
config.vm.box = BOX_IMAGE
config.vm.hostname = "cassandra-vs-mariadb"
config.vm.provider "virtualbox" do |v|
v.memory = 1024
v.cpus = 2
end
config.vm.network "forwarded_port", guest: 3306, host: 3306
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
#Begin des Installationsscripts
config.vm.provision "shell", inline: <<-SHELL
DEBIAN_FRONTEND=noninteractive
echo "deb http://www.apache.org/dist/cassandra/debian 311x main" > /etc/apt/sources.list.d/cassandra.sources.list
wget -q https://www.apache.org/dist/cassandra/KEYS
apt-key add KEYS
apt-get update
apt-get dist-upgrade -y
debconf-set-selections <<< 'mariadb-server mariadb-server/root_password password root'
debconf-set-selections <<< 'mariadb-server mariadb-server/root_password_again password root'
#zu installierende Pakete
apt-get install -y mariadb-server python3-mysqldb \
python3-pip openjdk-8-jdk cassandra python-dev libmariadbclient-dev \
libssl-dev python3-venv
sed -i "s/^bind-address/#bind-address/" /etc/mysql/my.cnf
mysql -u root -proot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES; SET GLOBAL max_connect_errors=10000;"
service mysql restart
mysql < /vagrant/create_mariadb_database.sql
adduser vagrant mysql
mkdir /home/vagrant/venv
python3 -m venv /home/vagrant/venv/
source /home/vagrant/venv/bin/activate
pip3 install wheel
pip3 install -r /vagrant/requirements.txt
deactivate
SHELL
end

104
cassandra-test.py Normal file
View File

@ -0,0 +1,104 @@
"""
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
# 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;')
for row in rows:
print(row.ename, row.sal)
def update_data(self):
pass
def delete_data(self):
pass
if __name__ == '__main__':
example1 = PythonCassandraExample()
example1.createsession()
example1.setlogger()
example1.createkeyspace('techfossguru')
example1.create_table()
example1.insert_data()
example1.select_data()

View File

@ -0,0 +1,14 @@
-- Creates a test DB
--
-- Author: Andreas Zweili
-- 2018-09-02
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
create database if not exists testdb;
flush privileges;
ALTER DATABASE `testdb` CHARACTER SET utf8;
create user 'vagrant'@'localhost';
GRANT ALL ON *.* TO 'vagrant'@'localhost';

10
mariadb-test.py Normal file
View File

@ -0,0 +1,10 @@
#!/usr/bin/python
import mysql.connector as mariadb
mariadb_connection = mariadb.connect(user='vagrant',
database='testdb')
cursor = mariadb_connection.cursor()
cursor.execute("create table if not exists test (a bigint auto_increment primary key, first_name varchar(128), last_name varchar(128))")
cursor.execute("INSERT INTO test (first_name,last_name) VALUES (%s,%s)", ("first_name", "last_name"))
mariadb_connection.commit()

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
wheel
cassandra-driver
mysql-connector