Connecting to Kerberized hadoop cluster using python module impyla - python

I am using impyla module to connect to kerberized hadoop cluster. I want to access
hiveserver2/hive but I was getting the below error:
test_conn.py
from impala.dbapi import connect
import os
connection_string = 'hdp296m1.XXX.XXX.com'
conn = connect(host=connection_string, port=21050,auth_mechanism="GSSAPI",kerberos_service_name='testuser#Myrealm.COM',password='testuser')
cursor = conn.cursor()
cursor.execute('select count(*) form t_all_types_simple_t')
print cursor.description
results = cursor.fetchall()
Stacktrace:
[vagrant#localhost vagrant]$ python test_conn.py
Traceback (most recent call last):
File "test_conn.py", line 4, in <module>
conn = connect(host=connection_string, port=21050, auth_mechanism="GSSAPI",kerberos_service_name='testuser#Myrealm.COM',password='testuser')
File "/usr/lib/python2.7/site-packages/impala/dbapi.py", line 147, in connect
auth_mechanism=auth_mechanism)
File "/usr/lib/python2.7/site-packages/impala/hiveserver2.py", line 758, in connect
transport.open()
File "/usr/lib/python2.7/site-packages/thrift_sasl/__init__.py", line 61, in open
self._trans.open()
File "/usr/lib64/python2.7/site-packages/thrift/transport/TSocket.py", line 101, in open
message=message)
thrift.transport.TTransport.TTransportException: Could not connect to hdp296m1.XXX.XXX.com:21050
testuser is my kerberos principal which I will be using to do kinit.

Your connection appears to be incorrect.. Try,
from impala.dbapi import *
import sys, os
# set your parms
host=os.environ.get("CDH_HIVE",'x.x.x.x')
port=os.environ.get("CDH_HIVE_port",'10000')
auth_mechanism=os.environ.get("CDH_auth",'GSSAPI')
user='hive'
db='mydb'
# No password use kinit
password=''
# hive is principal with krb
kbservice='hive'
class Hive:
def __init__(self,db):
self.database=db
self.__conn = connect(host=host,
port=port,
auth_mechanism=auth_mechanism,
user=user,
password=password,
database=db,
kerberos_service_name=kbservice
)
self.__cursor = self.__conn.cursor()
h = Hive(db)

Related

How to connect fdb database using Python in Ubuntu?

I am trying to connect fdb database using python. I am remotely connected to ubuntu. I have tried this:
path = '/home/ubuntu/Firebird4.0/A.fdb'
con = fdb.connect(host=host, database=path, user=user, password=pswd2, charset='UTF8')
and many combinations for the path of ubuntu fdb file. How can I solve this problem? Is it about the wrong host, port, password, or something?
I got this error all the time
Traceback (most recent call last):
File "/home/ubuntu/test.py", line 24, in <module>
conn = connection()
File "/home/ubuntu/test.py", line 15, in connection
con = fdb.connect(host=host, database=path, user=user, password=pswd2, charset='UTF8')
File "/home/ubuntu/.local/lib/python3.10/site-packages/fdb/fbcore.py", line 869, in connect
raise exception_from_status(DatabaseError, _isc_status,
fdb.fbcore.DatabaseError: ('Error while connecting to database:\n- SQLCODE: -902\n- Unable to complete network request to host "15.206.100.218".\n- Failed to establish a connection.', -902, 335544721)

After checking the database is not connected to it

I have connect to postgreql which write in documentation Heroku:
import dj_database_url
import os
import psycopg2
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
DATABASE_URL['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
And I have connect in code:
conn = psycopg2.connect(
database = data.database,
user = data.user,
password = data.password,
host = data.host,
port = data.port,
sslmode = 'require'
)
cursor = conn.cursor()
They did some check of my database and after they finished at me nothing works and cannot be connected to a DB.
I get error:
2021-07-07T18:21:39.307157+00:00 app[worker.1]: Traceback (most recent call last):
2021-07-07T18:21:39.307173+00:00 app[worker.1]: File "/app/main.py", line 23, in <module>
2021-07-07T18:21:39.308848+00:00 app[worker.1]: conn = psycopg2.connect(
2021-07-07T18:21:39.308891+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
2021-07-07T18:21:39.309172+00:00 app[worker.1]: conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
2021-07-07T18:21:39.309299+00:00 app[worker.1]: psycopg2.OperationalError: FATAL: password authentication failed for user "cozqztdivphuut"
All information about password, user, database ect. input correctly.
Help please, because I can't understand this^^

Error when connecting to sql using Python

I am using Python library pymssql to connect to a database. I have never used it before so the error might look trivial.
from os import getenv
import pymssql
server = getenv("192.xxx.xxx.xx")
user = getenv("john.constantine")
password = getenv("xxxxxxx")
conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()
cursor.execute('SELECT * from table')
rows = cursor.fetchall()
for row in rows:
print row
conn.close()
I get the following error:
Traceback (most recent call last):
File "C:\Users\Documents\Demo_DB.py", line 8, in <module>
conn = pymssql.connect(server, user, password, "tempdb")
File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
File "_mssql.pyx", line 552, in _mssql.MSSQLConnection.__init__ (_mssql.c:5891)
TypeError: argument of type 'NoneType' is not iterable
[Finished in 0.2s with exit code 1]
I connect to DB using these credentials.

Psycopg2 could not connect to server

I'have a little problem when I want to connect to my DB with psycopg2 and python. I have this little script :
#!/usr/bin/python3
import time
import psycopg2
import sys
def main():
# Get a connection
conn = psycopg2.connect(database='my_db', host='10.10.2.1', port='5433', user='me', password='my_password_that_i_dont_show_here')
# conn.cursor will return a cursor object, to perform queries
cursor = conn.cursor()
# Execute our query
cursor.execute("select date(created_at), email, firstname, lastname, locale from users where date(created_at) = current_date;")
# retrieve the records from the database
records = cursor.fetchall()
print(records)
if __name__ == "__main__":
main()
That worked well on windows but now I'm on ubuntu and I have this error :
Traceback (most recent call last):
File "Bureau/script.py", line 29, in <module>
main()
File "Bureau/script.py", line 15, in main
conn = psycopg2.connect(database='my_db', host='10.10.2.1', port='5433', user='me', password='best_password_ever')
File "/usr/local/lib/python3.5/dist-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection timed out
Is the server running on host "10.10.2.1" and accepting
TCP/IP connections on port 5433?

Python MySQLdb error

When I run my script it won't connect to mysql database, I can't find my host name from godaddy I just tried mysq.mydomain.com and mydomain.com. the login details for the database are correct.
Traceback (most recent call last):
File "mysql.py", line 6, in <module>
conn = MySQLdb.connect(host="?",user="?",passwd="?",db="?")
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'official_autotes'#'104.156.228.189' (using password: YES)")
------------------
my code
from threading import Thread
import urllib
import re
import MySQLdb
conn = MySQLdb.connect(host="?",user="?",passwd="?",db="?")
query = "INSERT INTO tutorial (symbol) values ('AAPL')"
x = conn.cursor()
x.execute(query)
row = x.fetchal()
Grand privilage to the user and ip trying to access the MySQL table using the Python code. To do the same, execute the following commands in the mysql shell.
GRANT <privileges> ON database.* TO 'user'#'ip' IDENTIFIED BY 'password';
flush privileges;

Categories