[LATEST UPDATE] Thanks to Jack's enormous help!!! I managed to connect to the Cloud SQL postgres DB and read/write my dataframes to the database. However, I am still experiencing the same error that I experienced previously, which is...
struct.error: 'h' format requires -32768 <= number <= 32767
This error doesnt happen when the dataframes are small, compact and columns do not have too many NaN values in them. However, when there are many NaN values in the columns, the program throws the following error.
Separately I have tried using df = df.fillna(0) to fill the NaN values with 0. But it did not work as well, and the same error surfaced. Please help!
Traceback (most recent call last):
File "...\falcon_vbackup\STEP5_SavetoDB_and_SendEmail.py", line 81, in <module>
main_SavetoDB_and_SendEmail(
File "...\falcon_vbackup\STEP5_SavetoDB_and_SendEmail.py", line 37, in main_SavetoDB_and_SendEmail
Write_Dataframe_to_SQLTable(
File "...\falcon_vbackup\APPENDIX_Database_ReadWrite_v2.py", line 143, in Write_Dataframe_to_SQLTable
df_Output.to_sql(sql_tablename, con=conn, schema='public', index=False, if_exists=if_exists, method='multi', chunksize=1000)
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\pandas\core\generic.py", line 2963, in to_sql
return sql.to_sql(
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\pandas\io\sql.py", line 697, in to_sql
return pandas_sql.to_sql(
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\pandas\io\sql.py", line 1739, in to_sql
total_inserted = sql_engine.insert_records(
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\pandas\io\sql.py", line 1322, in insert_records
return table.insert(chunksize=chunksize, method=method)
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\pandas\io\sql.py", line 950, in insert
num_inserted = exec_insert(conn, keys, chunk_iter)
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\pandas\io\sql.py", line 873, in _execute_insert_multi
result = conn.execute(stmt)
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\sqlalchemy\engine\base.py", line 1289, in execute
return meth(self, multiparams, params, _EMPTY_EXECUTION_OPTS)
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\sqlalchemy\sql\elements.py", line 325, in _execute_on_connection
return connection._execute_clauseelement(
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\sqlalchemy\engine\base.py", line 1481, in _execute_clauseelement
ret = self._execute_context(
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\sqlalchemy\engine\base.py", line 1845, in _execute_context
self._handle_dbapi_exception(
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\sqlalchemy\engine\base.py", line 2030, in _handle_dbapi_exception
util.raise_(exc_info[1], with_traceback=exc_info[2])
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\sqlalchemy\engine\base.py", line 1802, in _execute_context
self.dialect.do_execute(
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\sqlalchemy\engine\default.py", line 732, in do_execute
cursor.execute(statement, parameters)
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\pg8000\dbapi.py", line 455, in execute
self._context = self._c.execute_unnamed(
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\pg8000\core.py", line 627, in execute_unnamed
self.send_PARSE(NULL_BYTE, statement, oids)
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\pg8000\core.py", line 601, in send_PARSE
val.extend(h_pack(len(oids)))
struct.error: 'h' format requires -32768 <= number <= 32767
Exception ignored in: <function Connector.__del__ at 0x00000213190D8700>
Traceback (most recent call last):
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\site-packages\google\cloud\sql\connector\connector.py", line 167, in __del__
File "c:\Users\ng_yj\.conda\envs\venv_falcon\lib\concurrent\futures\_base.py", line 447, in result
concurrent.futures._base.TimeoutError:
I have setup a postgresql in GCP's Cloud SQL. I am trying to connect to it using
google.cloud.sql.connector. I have created a Service Account from the GCP Console, and downloaded the json keys.
I want to use a service account , credentials/ keys (in the format of reading a .json file placed in the same directory as my main.py code) to authenticate access to cloud_sql.
I am trying to authenticate, but I keep getting an error that says that the service account json file was not found.
Can anyone help to figure out how to fix this error? Thank you!
import pandas as pd
from google.cloud.sql.connector import connector
import os
import pandas as pd
import pandas as pd
import sqlalchemy
import os
# configure Cloud SQL Python Connector properties
def getconn():
conn = connector.connect(
os.environ['LL_DB_INSTANCE_CONNECTION_NAME'],
"pg8000",
user=os.environ['LL_DB_USER'],
password=os.environ['LL_DB_PASSWORD'],
db=os.environ['LL_DB_NAME'])
return conn
# Show existing SQLTables within database
def Show_SQLTables_in_Database(conn):
if conn!=None:
# Show what tables remain in database
results = conn.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""").fetchall()
for table in results:
print(table)
if __name__=="__main__":
# Set the Google Application Credentials as environment variable
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(os.getcwd(),"Google-Credentials-LL-tech2.json")
# create connection pool to re-use connections
pool = sqlalchemy.create_engine("postgresql+pg8000://", creator=getconn)
with pool.connect() as db_conn:
# Show what tables remain in database
results = db_conn.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""").fetchall()
for table in results:
print(table)
The error you are seeing means that the .json file is not being found. This is most likely being caused by os.getcwd() which gets the path of the current working directory from where main.py is being called. This leads to errors if you are calling the file from anywhere other than the parent directory.
Working case: python main.py
Error case: python folder/main.py
Change the line where you set credentials to the following:
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(os.path.dirname(os.path.abspath(__file__)),"Google-Credentials-LL-tech2.json")
This will allow the credentials path to be properly set for all cases of where your main.py is called from.
Responding to your latest update of the error.
First, make sure that your service account has the Cloud SQL Client role applied to it.
Secondly, try executing the following basic script prior to your custom configuration, this will help isolate the error to the Python Connector or the service account/implementation.
The following should just connect to your database and print the time.
from google.cloud.sql.connector import connector
import sqlalchemy
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(os.path.dirname(os.path.abspath(__file__)),"GSheet-Credentials-LL-tech2.json")
# build connection for db using Python Connector
def getconn():
conn = connector.connect(
os.environ['LL_DB_INSTANCE_CONNECTION_NAME'],
"pg8000",
user=os.environ['LL_DB_USER'],
password=os.environ['LL_DB_PASSWORD'],
db=os.environ['LL_DB_NAME'],
)
return conn
# create connection pool
pool = sqlalchemy.create_engine("postgresql+pg8000://", creator=getconn)
def db_connect():
with pool.connect() as conn:
current_time = conn.execute(
"SELECT NOW()").fetchone()
print(f"Time: {str(current_time[0])}")
db_connect()
If that still gives the error, please provide the full stacktrace of the error so that I can try and debug it further with more info.
Related
I created a mongodb connection successfully, my connection tests successfully and was able to use a Crawler to create metadata in the Glue Data Catalog. However, when i use below where i am adding my mongodb database name and collection name in additional_options parameter i get an error:
data_catalog_database = 'tinkerbell'data_catalog_table = 'tinkerbell_funds'glueContext.create_dynamic_frame_from_catalog(database = data_catalog_database,table_name = data_catalog_table,additional_options = {"database":"tinkerbell","collection":"funds"})
following is the error: An error was encountered: An error occurred while calling o177.getDynamicFrame. : java.lang.NoSuchMethodError: com.mongodb.internal.connection.DefaultClusterableServerFactory.<init>(Lcom/mongodb/connection/ClusterId;Lcom/mongodb/connection/ClusterSettings;Lcom/mongodb/connection/ServerSettings;Lcom/mongodb/connection/ConnectionPoolSettings;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/MongoCredential;Lcom/mongodb/event/CommandListener;Ljava/lang/String;Lcom/mongodb/MongoDriverInformation;Ljava/util/List;)V
When I use it without additional parameters
glueContext.create_dynamic_frame_from_catalog(database = data_catalog_database,table_name = data_catalog_table)
I get following error:
An error was encountered: Missing collection name. Set via the 'spark.mongodb.input.uri' or 'spark.mongodb.input.collection' property Traceback (most recent call last): File "/home/glue_user/aws-glue-libs/PyGlue.zip/awsglue/context.py", line 179, in create_dynamic_frame_from_catalog return source.getFrame(**kwargs) File "/home/glue_user/aws-glue-libs/PyGlue.zip/awsglue/data_source.py", line 36, in getFrame jframe = self._jsource.getDynamicFrame() File "/home/glue_user/spark/python/lib/py4j-0.10.9-src.zip/py4j/java_gateway.py", line 1305, in call answer, self.gateway_client, self.target_id, self.name) File "/home/glue_user/spark/python/pyspark/sql/utils.py", line 117, in deco raise converted from None pyspark.sql.utils.IllegalArgumentException: Missing collection name. Set via the 'spark.mongodb.input.uri' or 'spark.mongodb.input.collection' property
Can someone please help me pass these parameters correctly?
Have explained above on what I tried but what I was expecting the dynamic frame to be created using the catalog table.
You are getting that error as mongo is expecting a connection with spark and need the input and output property.
Please refer to below link-
https://www.mongodb.com/docs/spark-connector/master/python-api/#std-label-pyspark-shell
Admittedly, I am fairly new to postgresql and pgadmin, but after spending a day learning it, with pgadmin4.4.1 I was able to backup tables and restore them in various different formats, currently, the format of choice is plain text, so that I can actually read the backup file to see what it is doing and also my database is tiny, but large enough to not want to type in every entry by hand.
Since updating to pgadmin4.4.2, the restore function does not work. I provides a green box saying restore job created, yet no data is added to the database. This was not the case yesterday, it would just restore the data.
Does anyone know what defaults might possibly have changed in the update that will be preventing my restore job from actually running? I am assuming that it is created and waiting in a queue somewhere that I can't find in order to actually run the backup.
Here is the last error from the log file. Apologies for the delay in replying.
019-02-08 17:59:06,942: ERROR pgadmin: 'NoneType' object has no attribute 'replace'
Traceback (most recent call last):
File "/Applications/pgAdmin 4.app/Contents/Resources/venv/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Applications/pgAdmin 4.app/Contents/Resources/venv/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Applications/pgAdmin 4.app/Contents/Resources/venv/lib/python3.6/site-packages/flask_login.py", line 792, in decorated_view
return func(*args, **kwargs)
File "/Applications/pgAdmin 4.app/Contents/Resources/web/pgadmin/misc/bgprocess/__init__.py", line 61, in index
return make_response(response=BatchProcess.list())
File "/Applications/pgAdmin 4.app/Contents/Resources/web/pgadmin/misc/bgprocess/processes.py", line 610, in list
desc = desc.message
File "/Applications/pgAdmin 4.app/Contents/Resources/web/pgadmin/tools/backup/__init__.py", line 145, in message
port = html.safe_str(port)
File "/Applications/pgAdmin 4.app/Contents/Resources/web/pgadmin/utils/html.py", line 35, in safe_str
return cgi.escape(x)
File "/Applications/pgAdmin 4.app/Contents/Resources/venv/lib/python3.6/cgi.py", line 1056, in escape
s = s.replace("&", "&") # Must be done first!
AttributeError: 'NoneType' object has no attribute 'replace'
I'm trying to use mongoDB with cuckoo but i get this error message :
2016-12-16 06:58:01,632 [lib.cuckoo.core.plugins] ERROR: Failed to run the reporting module "MongoDB":
Traceback (most recent call last):
File "/home/ziv/Documents/cuckoo/lib/cuckoo/core/plugins.py", line 533, in process
current.run(self.results)
File "/home/ziv/Documents/cuckoo/modules/reporting/mongodb.py", line 89, in run
if "cuckoo_schema" in self.db.collection_names():
File "/usr/lib/python2.7/dist-packages/pymongo/database.py", line 520, in collection_names
results = self._list_collections(sock_info, slave_okay)
File "/usr/lib/python2.7/dist-packages/pymongo/database.py", line 492, in _list_collections
cursor = self._command(sock_info, cmd, slave_okay)["cursor"]
File "/usr/lib/python2.7/dist-packages/pymongo/database.py", line 393, in _command
allowable_errors)
File "/usr/lib/python2.7/dist-packages/pymongo/pool.py", line 211, in command
read_concern)
File "/usr/lib/python2.7/dist-packages/pymongo/network.py", line 100, in command
helpers._check_command_response(response_doc, msg, allowable_errors)
File "/usr/lib/python2.7/dist-packages/pymongo/helpers.py", line 196, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
OperationFailure: command SON([('listCollections', 1), ('cursor', {})]) on namespace cuckoo.$cmd failed: not authorized on cuckoo to execute command { listCollections: 1, cursor: {} }
this is the DB's i have:
show dbs
admin 0.078GB
cuckoo 0.078GB
local 0.078GB
i used this guide to install mongo db https://www.howtoforge.com/tutorial/install-mongodb-on-ubuntu-16.04/
i used this guide to install cuckoo
http://mostlyaboutsecurity.com/?p=15&i=1
update
ithink i dont have permissions but i dont know how to set up what i need,
this is the cuckoo code that uses mongo DB:
(on this line "self.db.collection_names():")
def run(self, results):
"""Writes report.
#param results: analysis results dictionary.
#raise CuckooReportError: if fails to connect or write to MongoDB.
"""
if not HAVE_MONGO:
raise CuckooDependencyError(
"Unable to import pymongo (install with "
"`pip install pymongo`)"
)
self.connect()
# Set mongo schema version.
# TODO: This is not optimal becuase it run each analysis. Need to run
# only one time at startup.
if "cuckoo_schema" in self.db.collection_names():
if self.db.cuckoo_schema.find_one()["version"] != self.SCHEMA_VERSION:
CuckooReportError("Mongo schema version not expected, check data migration tool")
else:
self.db.cuckoo_schema.save({"version": self.SCHEMA_VERSION})
def connect(self):
"""Connects to Mongo database, loads options and set connectors.
#raise CuckooReportError: if unable to connect.
"""
host = self.options.get("host", "127.0.0.1")
port = int(self.options.get("port", 27017))
db = self.options.get("db", "cuckoo")
try:
self.conn = MongoClient(host, port)
self.db = self.conn[db]
self.fs = GridFS(self.db)
except TypeError:
raise CuckooReportError("Mongo connection port must be integer")
except ConnectionFailure:
raise CuckooReportError("Cannot connect to MongoDB")
I don't want to edit this code (add connection string to it)
I have a clean installation of MongoDB, how do i create a DB named cuckoo
that this code can acsess and use?
I couldn't find any reference in all the guides I read. its like it should work automaticly but it doesn't
The error message is:
OperationFailure: command SON([('listCollections', 1), ('cursor', {})]) on namespace cuckoo.$cmd failed: not authorized on cuckoo to execute command { listCollections: 1, cursor: {} }
This indicates that your app is attempting to execute a command in the MongoDB database for which it does not have permissions.
Does your connection string to the database include the authentication credentials (username/password)?
Does this user have necessary permissions to execute this command?
Good day,
I'm trying to use MongoDB with web2py, and for that I started with authentication, but this appeared some errors that I do not understand.
In a relational database, the web2py creates the authentication tables, MongoDB in the collections are not created automatically.
Below is the code and the error when trying to log me:
db.py
db = DAL("mongodb://localhost/primer", check_reserved=["mongodb_nonreserved",], adapter_args={"safe":False})
from gluon.tools import Auth, Service, PluginManager
auth = Auth(db)
service = Service()
plugins = PluginManager()
auth.settings.remember_me_form = False
auth.settings.actions_disabled=['register','change_password','request_reset_password','retrieve_username','profile']
auth.define_tables(username=True)
from gluon.contrib.login_methods.ldap_auth import ldap_auth
auth.settings.login_methods = [ldap_auth(server='localhost', port='10389', base_dn='ou=people,o=empresa,dc=com,dc=br')]
The authentication is by LDAP, and works perfectly in a relational database, which has the AUTH_USER table.
However, the loging using MongoDB, this appearing the following error:
Traceback (most recent call last):
File "C:\Users\Rafa\Desktop\web2py-10-06-2015p4\applications\contrato\controllers/appadmin.py", line 249, in select
nrows = db(query, ignore_common_filters=True).count()
File "C:\Users\Rafa\Desktop\web2py-10-06-2015p4\gluon\packages\dal\pydal\objects.py", line 2016, in count
return db._adapter.count(self.query,distinct)
File "C:\Users\Rafa\Desktop\web2py-10-06-2015p4\gluon\packages\dal\pydal\adapters\mongo.py", line 200, in count
count=True,snapshot=snapshot)['count'])
File "C:\Users\Rafa\Desktop\web2py-10-06-2015p4\gluon\packages\dal\pydal\adapters\mongo.py", line 319, in select
sort=mongosort_list, snapshot=snapshot).count()}
File "C:\Python27\lib\site-packages\pymongo\collection.py", line 929, in find
return Cursor(self, *args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'snapshot'
The database "primer" is created and only has two collections "posts" and "system.indexes"
Could someone help me with this error to be able to use MongoDB with the web2py?
Thank You!
Found it.
From pymongo's changelog There are a lot of breaking changes in pymongo 3.0 compared to 2.8
The following find/find_one options have been removed:
snapshot (use the new modifiers option instead)
So uninstall pymongo and try the latest before 3.0:
pip install pymongo==2.8.1
Here's my attempt:
>>> from pydal import *
No handlers could be found for logger "web2py"
>>> db = DAL('mongodb://localhost/connect_test')
>>> db.define_table('some',Field('key'),Field('value'))
<Table some (id,key,value)>
>>> db.define_table('some2',Field('ref','reference some'),Field('value'))
<Table some2 (id,ref,value)>
>>> db(db.some).select()
<Rows (1)>
>>> db(db.some).select().first()
<Row {'value': 'pir', 'key': 'bla', 'id': 26563964102769618087622556519L}>
>>>
[edit]
There's more to it. This worked at least with pydal 15.03. Googling some code i found the following in the mongo.py adapter :
from pymongo import version
if 'fake_version' in driver_args:
version = driver_args['fake_version']
if int(version.split('.')[0]) < 3:
raise Exception(
"pydal requires pymongo version >= 3.0, found '%s'"
% version)
Which was like good soil for a big frown...
After updating pydal to 15.07 it apears to brake indeed:
RuntimeError: Failure to connect, tried 5 times:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\pydal\base.py", line 437, in __init__
self._adapter = ADAPTERS[self._dbname](**kwargs)
File "C:\Python27\lib\site-packages\pydal\adapters\base.py", line 57, in __call__
obj = super(AdapterMeta, cls).__call__(*args, **kwargs)
File "C:\Python27\lib\site-packages\pydal\adapters\mongo.py", line 82, in __init__
% version)
Exception: pydal requires pymongo version >= 3.0, found '2.8.1'
So it's back to upgrading pymongo :)
With pymongo at 3.0.3 and pydal at 15.07 it works like a charm again.
I'm using MongoDB for my database.
db = DAL('mongodb://localhost:27017/app')
When I attempt to register a new user, I get the following error:
Traceback (most recent call last):
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/gluon/restricted.py", line 209, in restricted
exec ccode in environment
File "/home/ecosystem/web2py/applications/app/controllers/default.py", line 146, in <module>
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/gluon/globals.py", line 187, in <lambda>
self._caller = lambda f: f()
File "/home/ecosystem/web2py/applications/app/controllers/default.py", line 108, in user
return dict(form=auth())
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/gluon/tools.py", line 1205, in __call__
return getattr(self,args[0])()
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/gluon/tools.py", line 2180, in register
self.add_membership(group_id, form.vars.id)
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/gluon/tools.py", line 2963, in add_membership
record = membership(user_id = user_id,group_id = group_id)
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/gluon/dal.py", line 7656, in __call__
else:
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/gluon/dal.py", line 8787, in select
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/gluon/dal.py", line 5092, in select
row=[]
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/pymongo/cursor.py", line 1058, in next
if len(self.__data) or self._refresh():
File "/home/ecosystem/anaconda/lib/python2.7/site-packages/pymongo/cursor.py", line 1002, in _refresh
self.__uuid_subtype))
OverflowError: MongoDB can only handle up to 8-byte ints
If I look in my database tables, everything seems okay except a couple fields.
In auth_user (not sure about this one):
registration_id:""
In auth_event (I'm assuming this should not be null):
user_id:null
What is the best way to resolve this? If possible, I'd like to use MongoDB for all database transactions (including access control / authentication).
UPDATE: You can avoid this by upgrading to the latest version of Web2py. This is now fully supported so you do not need to use two different databases.
I wasn't able to solve this problem directly, but I was able to create a work-around. In db.py, I define two separate database connections:
db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all'])
db1 = DAL('mongodb://localhost:27017/collection')
I call db (eventually a mysql database) on all auth functions (registration, login, is logged in checks), while handling everything else (including detailed member profiles) in mongo. I often bypass db1 DAL entirely by using the pymongo API.