How to connect to hive using python pyhs2? - python

I am trying to access hive using pyhs2. I tried the following code:
example.py
import pyhs2
conn = pyhs2.connect(host='localhost', port=10000,authMechanism=None, user=None, password=None,database='default')
with conn.cursor() as cur:
cur.execute("select * from table")
for i in cur.fetch():
print i
I am getting the following error:
Traceback (most recent call last):
File "example.py", line 2, in <module> conn = pyhs2.connect(host='localhost', port=10000,authMechanism=None, user=None, password=None,database='default')
File "build/bdist.linux-x86_64/egg/pyhs2/__init__.py", line 7, in connect
File "build/bdist.linux-x86_64/egg/pyhs2/connections.py", line 46, in __init__
File "build/bdist.linux-x86_64/egg/pyhs2/cloudera/thrift_sasl.py", line 55, in open
File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 101, in open
thrift.transport.TTransport.TTransportException: Could not connect to localhost:10000
I am getting the exact error when I try with hive utils. I have checked sasl installation. Do I need to make any changes to the hive-site.xml in hive? If yes where do I need to create it? Am I missing out something?

1- Figure out the IP address of the localhost using (on Linux):
hostname -I
2- Change localhost to the actual ip
I would also suggest that you double check which host Hive is on. If you are using hortonworks, on Ambari, go to Hive, then Configs and check the host there.
Edit (adding another suggestion):
Your username and password most likely aren't None. To get your username and password, check hive-site.xml and look at the values in javax.jdo.option.ConnectionUserName and javax.jdo.option.ConnectionPassword. If you can't find anything, try an empty string as the password (as opposed to None), and hive or empty string as the username i.e. try these one by one:
conn = pyhs2.connect(host='localhost', port=10000,authMechanism='PLAIN', user='hive', password='',database='default')
conn = pyhs2.connect(host='localhost', port=10000,authMechanism='PLAIN', user='', password='',database='default')
Note that I also changed authMechanism to "PLAIN"

Related

Failing to create MQSQL database in python (cursor = db.cursor() not working) - Newbie Question

Following a tutorial video to learn how to make my first database as follows:
import mysql.connector
db = mysql.connector.connect()
host="localhost"
user="root"
passwd="xxxxxxxx"
However when i use the following (>>> cursor = db.cursor(), this error msg shows up... not sure what has happened as i don't understand anything yet.)
cursor = db.cursor()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\asans\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mysql\connector\connection.py", line 809, in cursor
raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.
I'm following the 1st video in a series of tutprials by 'Tech with Tim' on youtube. Also checked https://www.datacamp.com/community/tutorials/mysql-python#CD and that website seems to use the same command with no issues :(
Any ideas?
I'm using python 3.8.4 btw
You have to put the data in the parenthesis and seperate them with commas like below
import mysql.connector
db = mysql.connector.connect(
host= 'localhost',
user='root',
passwd='xxxxxxxx')
mycursor = db.cursor()

Error in database connection: Database "dbname" does not exist

I have created a python file app.py and included the code to connect to a db I created in postgresql as follows: -
import psycopg2
conn = psycopg2.connect(
user='postgres',
password='1234',
host='localhost',
port='5432',
database='bubbleformation'
)
cursor = conn.sursor()
cursor.execute('SELECT * FROM bubbleformation')
for row in cursor: print(row)
conn.close()
This was as instructed in this medium article
However, when I try to execute this python file in the terminal, I get the below error: -
Traceback (most recent call last): File "app.py", line 8, in
port='5432' File "/usr/lib/python2.7/dist-packages/psycopg2/init.py", line 130, in
connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: FATAL: database "bubbleformation" does not exist
I have created a table named "bubbleformation" and it can be viewed in the psql mode through the terminal.
Could anyone please help me understand what should be done? I tried changing the password, and user privileges, but none of them worked for my error.
You should create both database and table with the same name "bubbleformation". You've probably created that table in postgres database.
Enter psql as postgres user and call CREATE DATABASE bubbleformation;, then connect to it with \connect bubbleformation and then create your table (something like CREATE TABLE bubbleformation (id int, name text);).
The error is about there not being a database named "bubbleformation", so when you connect to the database in the terminal, whichever database that is is the one you need to specify in the database parameter. When you connect to the database in the terminal, type:
SELECT current_database();
If it is indeed a database named "bubbleformation" then is must be a different cluster you are connecting to, and therefore a different port.
Disclosure: I am an EnterpriseDB (EDB) employee.
Its due to environment error. I was loading the credentials from the .env file. But i mistakenly gave a wrong path.
project_folder = os.path.expanduser('~/scraping') instead of
project_folder = os.path.expanduser('~/find_my_nearest_store')
load_dotenv(os.path.join(project_folder, '.env'))
Hence the Error.

Why am I unable to connect to a mysql database using python and mysql.connector?

I am trying to connect to a mysql database, but having issues. I am trying to use mysql connector (not sure if this is the most efficient library to use), but I am having an error.
Here is my basic script:
import mysql.connector
def test_sql_query():
db = mysql.connector.connect(host='hostname', database='db', user='dbuser', password='dbpass' port=000)
cur = db.cursor()
if db.is_connected():
print('Connected to MySQL database')
try:
sql_command = "select * from test where test like '%FileUploadAgent%' and status='00' order by test desc;"
cur.execute(sql_command)
db.commit()
rows = cur.fetchall()
for row in rows:
print " ", row[1][1]
except:
print "did not work"
db.close()
Error:
File "sql_test.py", line 44, in <module>
test_sql_query()
File "sql_test.py", line 6, in sterling_agent_sql_query
db = mysql.connector.connect("host='hostname.com' database='dbname' user='dbuser' password='dbpassword' port='000'")
File "/Library/Python/2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 57, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes exactly 1 argument (2 given)
I am assuming that I am doing the connection wrong.
The parameter you are passing to .connect() is wrong. You are passing a string. Instead, each of those should be their own parameter.
db = mysql.connector.connect(host='hostname',
database='db',
user='dbuser',
password='dbpass',
port=000)
Additionally, port needs to be an integer, not a string. I also assume that the 000 you have is example data. If not, that's not valid.
You have the format of the connect incorrect, putting all of the variables inside of quotes makes the method treat it as a single, string input to the method.
db = mysql.connector.connect("host='hostname' database='db' user='dbuser' password='dbpass' port='000'")
should be
db = mysql.connector.connect(
host='hostname',
database='db',
user='dbuser',
password='dbpass',
port=0)
Note that your port value also should be a valid port (not the echo port) for the database.
I got the same problem my solution was to uninstall MySQL connector.
python -m pip uninstall mysql-connector
And then reinstall it using:
python -m pip install mysql-connector
and it works for me.

Python connecting to MySQL database name error

I got the following code from this site to connect to a mysql database through python (which is much appreciated by the way). I have used it once already to connect to a database on the server, but in a different file. However I know want to connect to another database on the same server but i keep getting error message to do with the database name.
I am using exactly the same code as I was before to connect, however i have only changed the database name to the other one i want to connect to. I can look at the database through puTTY, and see that it is called the correct name and that all the data is there, but just cant seem to connect properly through the Python.
Thanks in advance for any help!
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(host="127.0.0.1", # your host, usually localhost
user="root", # your username
passwd="........", # your password
db="opt_out") # name of the data base
# you must create a Cursor object. It will let
# you execute all the query you need
cur = db.cursor()
# Use all the SQL you like
cur.execute("SELECT * FROM opted_in")
# print all the first cell of all the rows
for row in cur.fetchall() :
print row[0]
And this is the error code i get in return when i try and execute the python file through puTTY:
Traceback (most recent call last):
File "/var/www/html/vra/ConnectAdiDB.py", line 7, in <module>
db="opt_out") # name of the data base
File "/usr/lib64/python2.6/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: YES)")
The Password must be wrong (I Think so)
So Try to reset the password so:
first off turn of the mysql Service like so
Ubuntu/Debian:
sudo service mysql stop
After That Do So:
sudo service mysql start
When Done Start mysql like so:
sudo mysql
An execute:
UPDATE mysql.user SET Password=PASSWORD('NEW-PASSWORD') WHERE User='root';
FLUSH PRIVILEGES;

"Can't connect to MySQL server on 'localhost' (10061)" error in python

Just today I have decided that I wanted to learn python because I personally like the syntax and its feel. I am a pretty young developer and I really wanted to learn a new programming language since I am only fluent in JavaScript (especially javascript) and PHP so I decided on Python. Python seems quite like javascript execpt for the fact that they don't use curly braces to define a new block. And of course its far more powerful.
I've learned that you can connect to MySQL databases using MySQLdb .
But I am having an issue, when I use the following code.
db = MySQLdb.connect(host="localhost", user="methodjs_postes", passwd="********", db="methodjs_postes");
I get this large error:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
db = MySQLdb.connect(host="localhost", user="methodjs_postes", passwd="********", db="methodjs_postes");
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
The main part that I was really focusing on though was the Can't connect to MySQL server on 'localhost' (10061) part. (But it would be nice if you explain the other parts too)
I don't see why i'm getting this issue.
I use localhost in my PHP and it works just fine. And I did import the MySQLdb which I don't think is the error because then it wouldn't even attempt to connect.
I am using python 2.7, I would be using 3.3 (have it downloaded) but MySQLdb only supports up to 2.7.
When using select User,Host,Password from mysql.user where User like '%methodjs_postes%'; I get the following error:
SELECT command denied to user 'methodjs'#'localhost' for table 'user'
Just replace localhost with 127.0.0.1
https://chat.stackoverflow.com/rooms/22618/discussion-between-shawn31313-and-suku
As per out chat, the conclusion is :- "Your mysql host is a remote machine and which is not allowing remote mysql connections"

Categories