diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..0f8ddcf --- /dev/null +++ b/Vagrantfile @@ -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 diff --git a/cassandra-test.py b/cassandra-test.py new file mode 100644 index 0000000..efec06a --- /dev/null +++ b/cassandra-test.py @@ -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() diff --git a/create_mariadb_database.sql b/create_mariadb_database.sql new file mode 100644 index 0000000..5dfafee --- /dev/null +++ b/create_mariadb_database.sql @@ -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'; diff --git a/mariadb-test.py b/mariadb-test.py new file mode 100644 index 0000000..61b48cc --- /dev/null +++ b/mariadb-test.py @@ -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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c2b1cca --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +wheel +cassandra-driver +mysql-connector