To run this, simply run:
python cassandra-cql.py hostname port script_file [-f]
This will soon be packaged for pip.
# |
#! /usr/bin/env python | |
import python_include as superlib | |
from sys import argv | |
if len(argv) < 4 or (len(argv) == 5 and argv[4] != '-f'): | |
print ''' | |
This file is used to inject a full *.cql file into a Cassandra database. | |
To run this script, all three command line parameters are required: | |
python cassandra-cql.py hostname port script_file [-f] | |
hostname The address of the server you are connecting to | |
port The port of the cassandra server (usually 9160) | |
script_file The *.cql file to be executed | |
-f Force the script to continue if it hits any error | |
An example script file would be: | |
USE keyspace; | |
CREATE COLUMNFAMILY projects ( | |
KEY uuid PRIMARY KEY, | |
project_id int, | |
name text | |
); | |
''' | |
else: | |
superlib.execute_file(argv[1], argv[2], argv[3], force=(len(argv) > 4)) |
#! /usr/bin/env python | |
''' | |
This file is used to inject a full *.cql file into a Cassandra database. | |
To run this script, all three command line parameters are required: | |
python cassandra-cql.py hostname port script_file | |
An example script file would be: | |
USE keyspace; | |
CREATE COLUMNFAMILY projects ( | |
KEY uuid PRIMARY KEY, | |
project_id int, | |
name text | |
); | |
''' | |
import cql | |
import sys | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
def disable(self): | |
self.HEADER = '' | |
self.OKBLUE = '' | |
self.OKGREEN = '' | |
self.WARNING = '' | |
self.FAIL = '' | |
self.ENDC = '' | |
def execute_file(host, port, filename, force): | |
connection = cql.connect(host=host, port=port, cql_version='3.0.0') | |
cursor = connection.cursor() | |
cql_file = open(filename) | |
cql_command_list = ''.join(cql_file.readlines()).split(";") | |
for cql_command in cql_command_list: | |
if cql_command.replace('\n', ''): | |
print '\n{command}'.format(command=cql_command.strip('\n')) | |
try: | |
cursor.execute('{command};'.format(command=cql_command.replace('\n', ' '))) | |
print '{color_start}Success!{color_end}'.format(color_start=bcolors.OKGREEN, color_end=bcolors.ENDC) | |
except cql.ProgrammingError as e: | |
print '{color_start}{error}{color_end}'.format(error=e, color_start=bcolors.FAIL, color_end=bcolors.ENDC) | |
if not force: | |
print '\n{color_start}Execution of script {filename} failed!\nSee the error message above for details.{color_end}'.format(color_start=bcolors.FAIL, | |
color_end=bcolors.ENDC, | |
filename=filename) | |
sys.exit(-1) | |
print '\n{color_start}Execution of script {filename} complete!{color_end}'.format(color_start=bcolors.OKBLUE, | |
color_end=bcolors.ENDC, | |
filename=filename) |
cql==1.4.0 |