How to create a session in Cassandra? - cassandra

Total Cassandra newbie here, using Python client.
from cassandra.cluster import Cluster
cluster = Cluster(['127.0.0.1'])
session = cluster.connect()
I get error:
Exception in thread event_loop (most likely raised during interpreter shutdown):
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
File "/usr/lib/python2.7/threading.py", line 504, in run
File "/usr/local/lib/python2.7/dist-packages/cassandra_driver-1.0.2-py2.7-linux-x86_64.egg/cassandra/io/asyncorereactor.py", line 52, in _run_loop
: __exit__
I want to create my first table and can't get past session.
query = "create table timeseries (
event_type text,
insertion_time timestamp,
event blob,
PRIMARY KEY (event_type, insertion_time)
)
WITH CLUSTERING ORDER BY (insertion_time DESC);"
session.execute(query)

Related

Why am I not able to create table in SQLyog using pycharm, while pycharm is connecting to SQLyog?

I was trying to connect and make tables in database using python. I was using pycharm for that. I successfully connect pycharm with SQLyog database.
import pymysql
def CreateConn():
return pymysql.connect(host="localhost",database="myfirstDB",user="root",password="",port=3306)
CreateConn()
but as I was trying to make table by code , it shows some lines of error which I don't understand. I changed SQL database engine to SQLite and also tried to changed IDE to jupyter, still it shows error don't know why.
I tried below code for table creation in SQLyog;
def CreateTable():
conn=CreateConn()
cursor=conn.cursor() #helping to execute your query
query="create table student(sid int primary key auto_increment,name VARCHAR(50),email VARCHAR(50),city VARCHAR(50)"
cursor.execute(query)
conn.commit()
print("table created")
conn.close()
CreateTable()
I expected below table in SQLyog database;
expected result of above code in SQLyog database using pycharm
what is got as a result is below error lines;
Traceback (most recent call last):
File "C:\Users\asus\PycharmProjects\pythonProject\Database\Database.py", line 29, in <module>
CreateTable() #CALLING CREATE TABLE FUNCTION
File "C:\Users\asus\PycharmProjects\pythonProject\Database\Database.py", line 24, in CreateTable
cursor.execute(query)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\cursors.py", line 148, in execute
result = self._query(query)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\cursors.py", line 310, in _query
conn.query(q)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\connections.py", line 548, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result
result.read()
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\connections.py", line 1156, in read
first_packet = self.connection._read_packet()
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\connections.py", line 725, in _read_packet
packet.raise_for_error()
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1")

getting error while creating a single table instead of create_all

I am trying to create a table on demand.
def create_host_table(name):
"""Create host server status table.
Arguments:
name (str): name of the host.
"""
class HostMachineStatus(Base):
__tablename__ = '{0}{1}{2}'.format(HOST_TABLE_PREFIX, name, HOST_TABLE_SUFFIX)
id = Column(Integer, primary_key=True)
status = Column(String(7)) # online or offline
datemodified = Column('datemodified', TIMESTAMP, server_default=text(
'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
)
)
datecreated= Column('datecreated', TIMESTAMP)
status_time_delta = ('status_time_delta', TIMESTAMP) # duration how long was online or offline.
table_name = HostMachineStatus.__tablename__
Base.metadata.create_all(engine, Base.metadata.tables[table_name],checkfirst=True)
but the above code throws error:
Traceback (most recent call last):
File "server_status.py", line 97, in <module>
main()
File "server_status.py", line 93, in main
create_host_table(host)
File "server_status.py", line 71, in create_host_table
Base.metadata.create_all(engine, Base.metadata.tables[table_name],checkfirst=True)
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/sql/schema.py", line 4316, in create_all
ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2049, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1618, in _run_visitor
visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/sql/visitors.py", line 138, in traverse_single
return meth(obj, **kw)
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/sql/ddl.py", line 754, in visit_metadata
[t for t in tables if self._can_create_table(t)]
TypeError: 'Table' object is not iterable
From the documentation (emphasis mine):
tables – Optional list of Table objects, which is a subset of the total tables in the MetaData (others are ignored).
So the issue is that you are passing a single table, not a list of tables. (The "is not iterable" in the error output is also a strong hint here.) Try this instead:
Base.metadata.create_all(engine, [Base.metadata.tables[table_name]], checkfirst=True)

Cassandra ODBC parameter binding

I've installed DataStax Community Edition, and added DataStax ODBC connector. Now I try to access the database via pyodbc:
import pyodbc
connection = pyodbc.connect('Driver=DataStax Cassandra ODBC Driver;Host=127.0.0.1',
autocommit = True)
cursor = connection.cursor()
cursor.execute('CREATE TABLE Test (id INT PRIMARY KEY)')
cursor.execute('INSERT INTO Test (id) VALUES (1)')
for row in cursor.execute('SELECT * FROM Test'):
print row
It works fine and returns
>>> (1, )
However when I try
cursor.execute('INSERT INTO Test (id) VALUES (:id)', {'id': 2})
I get
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 11, in <module>
cursor.execute('INSERT INTO Test (id) VALUES (:id)', {'id': 2})
pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')
Alternatives do neither work:
cursor.execute('INSERT INTO Test (id) VALUES (:1)', (2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 11, in <module>
cursor.execute('INSERT INTO Test (id) VALUES (?)', (2))
pyodbc.ProgrammingError: ('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')
and
cursor.execute('INSERT INTO Test (id) VALUES (?)', (2))
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.Error: ('HY000', "[HY000] [DataStax][CassandraODBC] (15) Error while preparing a query in Cassandra: [33562624] : line 1:31 no viable alternative at input '1' (...Test (id) VALUES (:[1]...) (15) (SQLPrepare)")
My Cassandra version is 2.2.3, ODBC driver is from https://downloads.datastax.com/odbc-cql/1.0.1.1002/
According to pyodbc Documentation your query should be
cursor.execute('INSERT INTO Test (id) VALUES (?)', 2)
More details on pyodbc Insert
As per the comment got a thread which says it is a open bug in pyodbc
BUG

KeyError Exception when import data in Cassandra

I am using Cassandra 2.2 and I created table 'teste':
CREATE TABLE teste(
idflight UUID,
unique_carrier text,
taiNumber text,
airTime int,
depDelay int,
PRIMARY KEY (unique_carrier, depDelay, idflight)
);
Then I tried to import data from a CSV file using COPY FROM command:
COPY teste(idflight, unique_carrier, tailNumber, airTime, depDelay) FROM 'out.csv' WITH HEADER = true AND DELIMITER = ',';
When running this command occurs the following error:
Process ImportProcess-2:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/bin/cqlsh.py", line 2306, in run
cqltypes = [table_meta.columns[name].typestring for name in self.columns]
KeyError: 'tailnumber'
Process ImportProcess-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/bin/cqlsh.py", line 2306, in run
cqltypes = [table_meta.columns[name].typestring for name in self.columns]
KeyError: 'tailnumber'
Process ImportProcess-3:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/bin/cqlsh.py", line 2306, in run
cqltypes = [table_meta.columns[name].typestring for name in self.columns]
KeyError: 'tailnumber'
What should I do to correct this problem?

Pyspark error while querying cassandra to convert into dataframes

I am getting the following error while executing the command:
user = sc.cassandraTable("DB NAME", "TABLE NAME").toDF()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/src/spark/spark-1.4.1/python/pyspark/sql/context.py", line 60, in toDF
return sqlContext.createDataFrame(self, schema, sampleRatio)
File "/usr/local/src/spark/spark-1.4.1/python/pyspark/sql/context.py", line 333, in createDataFrame
schema = self._inferSchema(rdd, samplingRatio)
File "/usr/local/src/spark/spark-1.4.1/python/pyspark/sql/context.py", line 220, in _inferSchema
raise ValueError("Some of types cannot be determined by the "
ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling
Load into a Dataframe directly this will also avoid any python level code for interpreting types.
sqlContext.read.format("org.apache.spark.sql.cassandra").options(keyspace="ks",table="tb").load()

Resources