Connect to an OLAP Cube using Python on LINUX - python

I know how to connect to a MS OLAP cube using Python on Windows - well, at least one method :-).
Usually I use the win32py package and invoke a .COM object to connect:
import win32com.client
connection = win32com.client.Dispatch(r'ADODB.Connection')
connString = 'Connection String to MSOLAP CUBE'
connection.Open(connString)
However, now I want to do the same with Linux (RHEL 7.2), and I have no idea how to do that. Can you please help me with that?
What I tried so far:
I tried using pyodbc-package or the sqlalchemy to establish a connection, but was not really successful in doing so (see Edit-1 section). Moreover, I am not even sure if I am looking at the right packages, or if it is even possible at all to connect to a MS OLAP cube on Linux :S
What I found so far:
On Windows I usually use a .COM object or the .NET framework to connect to the cube. However, if I understand it right, these are Windows specific languages/frameworks, and can't be used on Linux. Right now I am looking at Mono for Linux, to see if I can use it to establish the connection.
More information:
Python version: 3.4
Linux distribution: Red Hat Enterprise Linux 7.2
sudo privileges on the machine: yes
.
Edit-1 (see: shellter comment)
I tried three different methods till now, but as mentioned above, I am not even sure if I work in the right direction. The only thing I am confident in, is that I can't establish a connection to the Cube via a .COM object (like in the Windows case), as Linux does not support the .COM environment.
Code-1:
import pyodbc
server = 'server address'
database = 'database name'
connStr = 'Provider=MSOLAP;Data Source=' + server + ';Initial Catalog=' + database + '; Application Name=Python;'
pyodbc.connect(connStr)
Error-1:
File "C:/Users/Desktop/untitled/main.py", line 35, in <module>
File connStr(connStr)
File TypeError: 'str' object is not callable
Code-2:
import pyodbc
driver = '{MSOLAP}'
server = 'server address’
database = 'database name'
user = ‘user name’
passw = 'password'
connStr = "Provider=MSOLAP;Data Source=" + server + ";Initial Catalog=" + database + "; Application Name=Python;"
Error-2:
File "C:/Users/besechr/Desktop/untitled/main.py", line 19, in <module>
pyodbc.connect('DRIVER=' + driver + ';SERVER=' + server + ';DATABASE=' + database + ';UID=' + user + ';PWD=' + passw)
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
btw: if I try to connect to the underlying SQL server with the above-stated connection string (changing the driver parameter to {SQL Server}) it works fine.
Code-3:
import pyodbc
import sqlalchemy
engine = sqlalchemy.create_engine('mssql+pyodbc://' + server )
engine.connect()
Error-3:
sqlalchemy.exc.DBAPIError: (Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') None None

I had the same requirement.
We were running on Ubuntu 20.04.
Attempt 1
We Needed Python.Net latest Stable
This needed Mono As a Dependancy
Mono only supported .NetFW4.xx integration
We used standard installation of Python.Net (which was also .NetFW4.xx support)
Hence we HAD to use The specific lib Microsoft.AnalysisServices.AdomdClient.retail.amd64 Which is Windows compatible only
If we used microsoft.analysisservices.adomdclient.netcore.retail.amd64 it fails with no proper errors
Also All the packages had to be placed inside Monos package store. Else it failed to find them.
Finally it failed saying
EntryPointNotFoundException: GetConsoleWindow assembly:<unknown assembly> type:<unknown type> member:(null)
at (wrapper managed-to-native) Microsoft.AnalysisServices.AdomdClient.Interop.NativeMethods.GetConsoleWindow()
at Microsoft.AnalysisServices.AdomdClient.Utilities.WindowsRuntimeHelper.IsProcessWithUserInterface () [0x00018] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Configuration.ConfigurationHelper.CheckIfProcessWithUserInterface () [0x00009] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Configuration.RuntimeConfiguration..ctor (System.Collections.Generic.IDictionary`2[TKey,TValue] configuration) [0x0000a] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Configuration.ConfigurationHelper.EnsureConfigurationIsLoaded () [0x00034] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Configuration.ConfigurationHelper.get_Connectivity () [0x00000] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.GetTcpClient (Microsoft.AnalysisServices.AdomdClient.Hosting.IConnectivityOwner owner, Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo) [0x000ee] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenTcpConnection (Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo, Microsoft.AnalysisServices.AdomdClient.Sspi.SecurityMode securityMode) [0x00025] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenConnectionAndCheckIfSessionTokenNeeded (Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo) [0x0005a] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient+<>c__DisplayClass95_0.<OpenConnection>b__0 () [0x00000] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Security.TransparentUserContext.ExecuteInUserContextImpl[TResult] (System.Func`1[TResult] action) [0x00000] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Security.UserContext.ExecuteInUserContext[TResult] (System.Func`1[TResult] action) [0x00006] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenConnection (Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo, System.Boolean& isSessionTokenNeeded) [0x00020] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.Connect (Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo, System.Boolean beginSession) [0x002c6] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.AdomdConnection+XmlaClientProvider.Connect () [0x00064] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.AdomdConnection+XmlaClientProvider.Microsoft.AnalysisServices.AdomdClient.AdomdConnection.IXmlaClientProviderEx.ConnectXmla () [0x00000] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.AdomdConnection.ConnectToXMLA (System.Boolean createSession, System.Boolean isHTTP) [0x00072] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.AdomdConnection.Open () [0x000b9] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <533173d24dae460899d2b10975534bb0>:0
Cause
Folks smarter than me had explained here what is going on
C# to Mono GetConsoleWindow Exception
Attempt 2
Install .netCore Runtime https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu
Install Python.Net 3.0.0a2 (This supported .netCore so we did not need to depend on mono)
Set the runtime with clr_loader that is installed as a dependancy of pythonnet
from clr_loader import get_coreclr
from pythonnet import set_runtime
rt = get_coreclr("path_to_runtime_cong/net_core_rt_conf.json")
set_runtime(rt)
import clr
My Conf Looked like
{
"runtimeOptions": {
"tfm": "net6.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "6.0.3"
}
}
}
Extracted the microsoft.analysisservices.adomdclient.netcore.retail.amd64 nuget package in to GAC of the .netCore installation on the Nix Box (call this pkg_path)
Then Tryied to connect
import pandas as pd
from sys import path
path.append(**pkg_path**)
clr.AddReference('System.Data')
clr.AddReference('Microsoft.AnalysisServices.AdomdClient')
from Microsoft.AnalysisServices.AdomdClient import AdomdConnection, AdomdDataAdapter
conn = AdomdConnection("Data Source=<MY_SERVER>;Catalog=<MY_CUBE>;")
conn.Open()
And I was greeted with
NotSupportedException: This feature is supported for a .NET Core client only on Windows systems.
---------------------------------------------------------------------------
NotSupportedException Traceback (most recent call last)
<command-3652263429411005> in <module>
10
11 conn = AdomdConnection("Data Source=<MY_SERVER>;Catalog=<MY CUBE>;")
---> 12 conn.Open()
13 cmd = conn.CreateCommand()
14 cmd.CommandText = MDX_QUERY
NotSupportedException: This feature is supported for a .NET Core client only on Windows systems.
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenConnectionAndCheckIfSessionTokenNeeded(ConnectionInfo connectionInfo)
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.<>c__DisplayClass94_0.<OpenConnection>b__0()
at Microsoft.AnalysisServices.Security.TransparentUserContext.ExecuteInUserContextImpl[TResult](Func`1 action)
at Microsoft.AnalysisServices.Security.UserContext.ExecuteInUserContext[TResult](Func`1 action)
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenConnection(ConnectionInfo connectionInfo, Boolean& isSessionTokenNeeded)
In a nutshell as of now this seems a bit impossible. At least in a straight forward manner.

Related

Driver not loaded mySql

I'm using Pycharm with Python3.9 and PyQt 5.15.2 and QSqlDatabase I followed the instructions on https://doc.qt.io/qt-5/sql-driver.html#qmysql How to Build the QMYSQL Plugin on Windows. I used the mingw81_64 compiler. The file qsqlmysql.dll has been generated and I have copied it to PyQt5/Qt/plugins/sqldrivers in my virtual environment. I also copied the file libmydql.dll to PyQt5/Qt5/bin in my virtual environment. If I execute database.drivers() I get the following ['QSQLITE', 'QMARIADB', 'QMYSQL', 'QMYSQL3', 'QODBC', 'QODBC3', 'QPSQL', 'QPSQL7']. I got the following error "Driver not loaded" I also tried downloading the file qsqlmysql.dll from https://github.com/weldermarcosxd/pyqt5-mysql/blob/master/Lib/site-packages/PyQt5/Qt/plugins/sqldrivers/qsqlmysql.dll but I got the same error. I'm using this database https://remotemysql.com/ but I also tried with a local MySql Server and I got the same error. SQLlite works fine.
class Login(QWidget):
def __init__(self):
super().__init__()
self.init_widgets()
self.set_styles()
self.connectSQLlite()
def connect(self):
server = 'remotemysql.com'
database_name = '****'
user_name = '****'
password = '****'
self.database = QSqlDatabase.addDatabase("QMYSQL")
self.database.setHostName(server)
self.database.setDatabaseName(database_name)
self.database.setUserName(user_name)
self.database.setPassword(password)
ok = self.database.open()
if ok:
print('Success')
else:
print(self.database.lastError().text())
app = QApplication(sys.argv)
You have to compile plugin with same compiler that your Qt binaries were compiled.

Connect to Filemaker Database using JDBC, Python, and JayDeBeApi

I'm trying to write an AWS Lambda Python Package that will connect to a FileMaker database over JDBC. To test, I've launched an EC2 instance with the Lambda Linux AMI, and created a virtualenv (/venv) that I'm testing in. I've uploaded the fmjdbc.jar to the instance using WinSCP to /venv/lib/fmjdbc.jar. The code uses JayDeBeApi, following the usage example here: https://pypi.python.org/pypi/JayDeBeApi/#usage
My code so far is the following:
import jaydebeapi as jdb
driverclass = 'com.filemaker.jdbc.Driver'
jdbcURL = 'jdbc:filemaker://url:port;database'
jar = '/home/ec2-user/lambda-test-project/venv/lib/fmjdbc.jar'
print jar
conn = jdb.connect(driverclass,[jdbcURL,'username','password'],jar)
Which gives me the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ec2-user/lambda-test-project/venv/local/lib/python2.7/site-package s/jaydebeapi/__init__.py", line 359, in connect
jconn = _jdbc_connect(jclassname, jars, libs, *driver_args)
File "/home/ec2-user/lambda-test-project/venv/local/lib/python2.7/site-package s/jaydebeapi/__init__.py", line 183, in _jdbc_connect_jpype
return jpype.java.sql.DriverManager.getConnection(*driver_args)
jpype._jexception.SQLExceptionPyRaisable: java.sql.SQLException: No suitable driver found for jdbc:filemaker://<MY URL STUFF IS HERE>
How can I get the jdbc driver to be read by Python's virtual environment? I'd like to have this code work in a Lambda package eventually, so I'm hoping there's a solution that can be integrated to the Python code that will work repeatedly on newly created servers.
You can use jpype package to set driver for python. I used it for connecting Oracle DB before. There is my sample code which may be useful for you.
import jaydebeapi,jpype
classpath = "your jdbc jar driver path"
jvm_path = "/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.36.x86_64/jre/lib/amd64/server/libjvm.so" #your java vm path
jpype.startJVM(jvm_path, "-Djava.class.path=%s" % classpath) #start jvm based on the driver
conn = jaydebeapi.connect(xxxxxx)

No module named usb.core

How can I remove this error in the below mentioned program? The error iI'm getting is
ImportError: No module named usb.core
and my code is:
import usb.core
import usb.util
# find our device
dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001)
# was it found?
if dev is None:
raise ValueError('Device not found')
# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()
# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
assert ep is not None
# write the data
ep.write('test')
OS is windows 8 64-bit [...]
ValueError: No backend available
Allow me to translate: You forgot to install the correct USB driver.
USB devices need a driver to work in Windows. Look at PyUSB website for details, and use Zadig to generate and install the driver (e.g. LibUSB-Win32) for you. This program takes care of the certificate that Windows 8 wants to see for your drivers inf file.
Btw: The VID you should use for USB development is 0x4242.
For error:
C:\Users\RAHUL\Desktop\python progrms\USBsample.py, line 5, in <module>
dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001) File "C:\Python27\lib\site-
packages\usb\core.py", line 864, in find raise ValueError('No backend available')
ValueError: No backend available
Download and install libusb-win32-devel-filter-1.2.6.0.exe. It should work.
python -m pip install pyusb libusb
Fixed this for me.

pyodbc: .mdb connection error on ubuntu

I am trying to access a .mdb file which located on my system. My Code look slike this :
import csv
import pyodbc
MDB = '/home/filebug/client/my.mdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'mypassword'
conn = pyodbc.connect('DRIVER=%s;DBQ=%s;PWD=%s' % (DRV,MDB,PWD))
print conn
curs = conn.cursor()
SQL = 'SELECT * FROM InOutTable;' # insert your query here
curs.execute(SQL)
rows = curs.fetchall()
curs.close()
conn.close()
But I am facing following Error :
Traceback (most recent call last):
File "mdb.py", line 8, in <module>
conn = pyodbc.connect('DRIVER=%s;DBQ=%s;PWD=%s' % (DRV,MDB,PWD))
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
I have pyodbc-3.0.6-py2.7-linux-i686.egg installed on my system. using Ubuntu 12.04
Can any one sya me what is wrong here
I get this info from http://www.easysoft.com/developer/interfaces/odbc/linux.html
In this case unixODBC could not locate the DSN "dsn_does_not_exist" and hence could not load the ODBC driver. Common reasons for this error are:
The DSN "dsn_does_not_exist" does not exist in your USER or SYSTEM ini files.
The DSN "dsn_does_not_exist" does exist in a defined ini file but you have omitted the "Driver=xxx" attribute telling the unixODBC driver manager which ODBC driver to load.
The "Driver=/path_to_driver" in the odbcinst.ini file points to an invalid path, to a path to an executable where part of the path is not readable/searchable or to a file that is not loadable (executable).
The Driver=xxx entry points to a shared object which does not export the necessary ODBC API functions (you can test this with dltest included with unixODBC.
The ODBC driver defined by DRIVER=xxx in the odbcinst.ini file depends on other shared objects which are not on your dynamic linker search path. Run ldd on the driver shared object named by Driver= in the odbcinst.ini file and see what dependent shared objects cannot be found. If some cannot be found than you need to defined your LD_LIBRARY_PATH environment variable to define the paths to any dependent shared objects or add these paths to /etc/ld.so.conf and rerun ldconfig.

How connect to mysql with python via odbc

Im running mysql, pyodbc, python 2.7 loaded on Fedora 14 x64.
Odbcinst.ini is:
# Example driver definitions
# Driver from the postgresql-odbc package
# Setup from the unixODBC package
#[PostgreSQL]
#Description = ODBC for PostgreSQL
#Driver = /usr/lib/psqlodbc.so
#Setup = /usr/lib/libodbcpsqlS.so
#Driver64 = /usr/lib64/psqlodbc.so
#Setup64 = /usr/lib64/libodbcpsqlS.so
#FileUsage = 1
# Driver from the mysql-connector-odbc package
# Setup from the unixODBC package
[MySQL]
Description = ODBC for MySQL
#Driver = /usr/lib/libmyodbc5.so
#Setup = /usr/lib/libodbcmyS.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS.so
FileUsage = 1
Odbc.ini is :
[MySQL]
Driver = MySQL
Database = mysql
Server = localhost
Socket = /var/lib/mysql/mysql.sock
User = rooter
Password = sshh
Mysql.sock is empty?
/var/lib/mysql/mysql.sock has 0.B
python script is:
import pyodbc
#pyodbc.pooling = False
conn = pyodbc.connect('DRIVER={MySQL};SOCKET=/var/lib/mysql/mysql.sock;UID=rooter;PWD=sshh')
csr = conn.cursor()
csr.execute("SET GLOBAL event_scheduler = ON")
csr.close()
conn.close()
del csr
I cant seem to connect with above script, Using isql i get Connected!
MyERROR msg:
Traceback (most recent call last):
File "/CCX/Py/MySql Event OFF.py", line 4, in <module>
conn = pyodbc.connect('DRIVER={MySQL};SOCKET=/var/lib/mysql/mysql.sock;UID=rooter;PWD=sshh')
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnectW)')
On Win XPx64,
#cxn = MySQLdb.connect (host = "localhost",user="rooter",passwd ="sshh")
Err Msg:
File "C:\Python26\lib\site-packages\MySQLdb\__init__.py", line 19, in <module>
import _mysql
ImportError: DLL load failed: %1 is not a valid Win32 application.
Comparing odbcinst.ini and your odbc.ini show an odd value for driver in odbc.ini, you should have a dynamic library here (I think).
And why don't you use direct mysql connection (without odbc)? :
import _mysql
conn = _mysql.connect(host="localhost", user="rooter",
password="sshh", db="mysql")
conn.query("SET GLOBAL event_scheduler = ON")
conn.close()
(not tested)
File "C:\Python26\lib\site-packages\MySQLdb__init__.py", line 19, in
import _mysql
ImportError: DLL load failed: %1 is not a valid Win32 application.
if Python26 && windows
we need download 3 files : libguide40.dll, libmmd.dll, libmySQL.dll
put the 3 files to C:\Python26\Lib\site-packages
and also put the 3 files to C:\Windows\system32
then restart your system
if Python27 && windows
when you setup the module ,be careful the version
MySQL-python-1.2.3.win32-py2.7.exe
MySQL-python-1.2.4.win32-py2.7.exe
one of the 2 version is you need
source: http://mysql-python.sourceforge.net/MySQLdb.html#some-mysql-examples
code tested:
import _mysql
conn=_mysql.connect(host="localhost", user="root", passwd="***", db="test")
conn.query("select * from test_table")
result = conn.use_result()
print result.fetch_row()
The fetch_row() function has two parameters:
maxrows limits the max number of rows returned.
how: it returns a tuple when how=0 and returns a dictionary when how=1.
For example, result.fetch(maxrows=5, how=1) returns an array (maximum size: 5) of dictionaries where the key is the column name and the value is the column value.

Categories