Hive - python connection error - python

Here is code :
#!/usr/bin/env python
import pyhs2
try:
with pyhs2.connect(host='localhost',
port=10001,
authMechanism="PLAIN",
user='root',
password='test',
database='test') as conn:
with conn.cursor() as cur:
#Show databases
print cur.getDatabases()
#Execute query
cur.execute("select * from raw_stats")
#Return column info from query
print cur.getSchema()
#Fetch table results
for i in cur.fetch():
print i
except Thrift.TException, tx:
print '%s' % (tx.message)
Error!
Traceback (most recent call last): File "/usr/local/py/test.py", line
8, in database='default') as conn: File
"/usr/lib/python2.6/site-packages/pyhs2/init.py", line 7, in
connect
return Connection(*args, **kwargs) File "/usr/lib/python2.6/site-packages/pyhs2/connections.py", line 46, in
init
transport.open() File "/usr/lib/python2.6/site-packages/pyhs2/cloudera/thrift_sasl.py", line
55, in open
self._trans.open() File "/usr/lib64/python2.6/site-packages/thrift/transport/TSocket.py", line
101, in open
message=message) thrift.transport.TTransport.TTransportException: Could not connect to localhost:10001

It resolved by starting hiveServer2 service and changing the port 10000.

Related

Connectin Clear Db in heroku application with mysql.connector

I am trying to connect to ClearDb with Heroku from python app I use mysql.connector
My code look like this:
conn = mysql.connector.connect(
host="clearDbHost",
user="123qwe",
password="123qwe",
database="heroku_4fsdfsdf30daf8",
port=3306,
autocommit = True
)
curs = conn.cursor()
Is connection crating the tables, but after this is disconnect and can't execute curs.execute() command later in the code I got this error:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 535, in cmd_query
self._cmysql.query(query,
_mysql_connector.MySQLInterfaceError: Lost connection to MySQL server during query
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/app/twitter.py", line 855,
in
getMyFollowersToDatabse() File "/app/twitter.py", line 186, in getMyFollowersToDatabse
curs.execute("INSERT INTO users (username, follow_id) VALUES ('" + user.username + "', '" + user.id + "')") File
"/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/cursor_cext.py",
line 269, in execute
result = self._cnx.cmd_query(stmt, raw=self._raw, File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py",
line 540, in cmd_query
raise errors.get_mysql_exception(exc.errno, msg=exc.msg, mysql.connector.errors.OperationalError: 2013 (HY000): Lost connection
to MySQL server during query
I see in the Clear Db like is havin ?reconnect=true in the end of the database name.I am not using curs.close()

Error while connecting to DB using PyMySQL

I get the following error while connecting to the database.
Traceback (most recent call last):
File "PycharmProjects/test.py", line 13, in <module>
connection = pymysql.connect(host=serverip,user=dbuser,password=dbpassword,db=dbname,port=dbport)
File "PycharmProjects\untitled\venv\lib\site-packages\pymysql\__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "PycharmProjects\untitled\venv\lib\site-packages\pymysql\connections.py", line 325, in __init__
self.connect()
File "PycharmProjects\untitled\venv\lib\site-packages\pymysql\connections.py", line 589, in connect
self.host_info = "socket %s:%d" % (self.host, self.port)
TypeError: %d format: a number is required, not str
Following is my code:
serverip = "1.2.3.4"
dbuser="root"
dbpassword="passwrd"
dbname="db"
dbport="3306"
connection = pymysql.connect(host=serverip,user=dbuser,password=dbpassword,db=dbname,port=dbport)
def main():
cur = connection.cursor()
cur.execute("SELECT VERSION()")
data = cur.fetchone()
print(data)
# disconnect from server
connection.close()
if __name__ == "__main__":
main()
Even if I try changing the hostname to 'test.aws.com' I get the similar error.

2055: Lost connection to MySQL server at 'localhost:3306', system error: Connection not available

When I try to run my code i get this error:
File "script.py", line 36, in <module>
result = cursor.execute(sql_insert_query)
File "/home/rexio18/.local/lib/python2.7/site-packages/mysql/connector/cursor_cext.py", line 266, in execute
raw_as_string=self._raw_as_string)
File "/home/rexio18/.local/lib/python2.7/site-packages/mysql/connector/connection_cext.py", line 405, in cmd_query
errno=2055, values=(addr, 'Connection not available.'))
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: Connection not available.
Code:
for child in root:
for element in child:
for subelement in element:
a = subelement.attrib['currency']
b = subelement.text
connection = mysql.connector.connect(host ='localhost', user ='root', passwd ='admin' , database ='python')
cursor = connection.cursor()
sql_insert_query = "INSERT INTO valoare(moneda, flux) VALUES (%s, %s)",(a, b)
result = cursor.execute(sql_insert_query)
connection.commit()

error using MySQLdb

I am newbie to python,I have simple code to connect database using MySQLdb and Python3.4 running in localhost.
Python Code:
#!c:/Python34/python.exe -u
import cgitb ,cgi
import sys
import MySQLdb
conn = MySQLdb.connect(host='localhost',port=3306,
db='example2',
user='root',
password='tiger')
cursor = conn.cursor()
if conn:
print("sucess")
But getting error ,while executing a code
Error:
Traceback (most recent call last):
File "<pyshell#27>", line 4, in <module>
password='tiger')
File "C:\Python34\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python34\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required (got type str)
Please give some suggestion
You need to remove the single quotes from the port.
conn = MySQLdb.connect(host='localhost',
port=3306,
database='example2',
user='root',
password='tiger')

Python, MySQL and a weird error

I have a bug that I don't know how to fix or even reproduce:
query = "SELECT id, name FROM names ORDER BY id"
results = database.execute(query)
where the class Database contains:
def execute(self, query):
cursor = self.db.cursor()
try:
cursor.execute(query)
return cursor.fetchall()
except:
import traceback
traceback.print_exc(file=debugFile)
return []
This is how I open the database connection:
self.db = MySQLdb.connect(
host=mysqlHost,
user=mysqlUser,
passwd=mysqlPasswd,
db=mysqlDB
)
This is the stacktrace of the error:
File "foo.py", line 169, in application results = config.db.execute(query)
File "Database.py", line 52, in execute
return cursor.fetchall()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 340, in fetchall
self._check_executed()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 70, in _check_executed
self.errorhandler(self, ProgrammingError, "execute() first")
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: execute() first
Do you have any ideas of why this is happening and how can I fix it? I searched on the internet and I found out that the reason may be having 2 cursors, but I have only one.
try this in your traceback it's for debugging:
except ProgrammingError as ex:
if cursor:
print "\n".join(cursor.messages)
# You can show only the last error like this.
# print cursor.messages[-1]
else:
print "\n".join(self.db.messages)
# Same here you can also do.
# print self.db.messages[-1]

Categories

Resources