hana_ml
) today:With the latest release 2.6 on Oct 16, 2020 it includes enhancements to leverage some more of HANA’s multi-model capabilities: spatial and graph.
hana_ml
.hana_ml
is compatible with SAP HANA Cloud and SAP HANA 2.0 SPS 5.scriptserver
, while PAL/APL ML libraries require it. Well, it is not a requirement for multimodel capabilities, so we are good here even without scriptserver
.hana_ml
2.6 I am going to use a new container in Docker with Jupyter and hana_ml
by executing on my macOS laptop with running Docker Desktop:docker run -p 8888:8888 --detach -v ~:/home/jovyan/work --name hmlsandbox01 -e GRANT_SUDO=yes -e JUPYTER_ENABLE_LAB=yes jupyter/minimal-notebook start-notebook.sh --LabApp.token=''
docker exec hmlsandbox01 pip install hana_ml==2.6.* shapely
DBAdmin
. But it is not a good practice to use it for everything else. We are going to create another user HANAML
and for that, we are going to use Python and hana_ml
already!00 Setup via hana_ml.ipynb
.import hana_ml
print(hana_ml.__version__)
hana_ml
and check (visually 😉 ) that it is at least version 2.6.hana_cloud_endpoint
.hana_cloud_endpoint="<<uuid>>.hana.trial-eu10.hanacloud.ondemand.com:443"
hana_ml
's connection context cchc
with login as a DBAdmin
user.hana_cloud_host, hana_cloud_port=hana_cloud_endpoint.split(":")
cchc=hana_ml.dataframe.ConnectionContext(port=hana_cloud_port,
address=hana_cloud_host,
user='DBAdmin',
encrypt=True
)
encrypt
is True
by default in version 2.6, but I included it to remind you about that. Another new thing in this version is that it will interactively ask you for a password if you are using user-based login and a password is not provided in the ConnectionContext
method.4.x
for the current version of SAP HANA Cloud) and the current schema of the connection (should be default user schema, i.e. DBADMIN
for now).print(cchc.hana_version())
print(cchc.get_current_schema())
HANAML
user using the cchc
connection context. Obviously, replace a string assigned to newpassword
with your own (and, no worries, I did it 😉 )newuser="HANAML"
newpassword="Super$ecr3t!"
cursor = cchc.connection.cursor()
#cursor.execute("DROP USER {} CASCADE".format(newuser))
cursor.execute('CREATE USER {name} PASSWORD "{pwd}" NO FORCE_FIRST_PASSWORD_CHANGE SET USERGROUP DEFAULT'.format(name=newuser, pwd=newpassword))
cursor.execute("ALTER USER {} DISABLE PASSWORD LIFETIME".format(newuser))
cursor.close()
DROP USER
line.hana_ml
...SELECT
we had to create a cursor using a .cursor()
method of a connection
property of our connection context.hdbcli
Python client for SAP HANA!__class__
and for the connection, it is indeed hdbcli.dbapi.Connection
.cchc.connection.__class__
SELECT
SQL statements we do not need to go to the underlying dbapi
connection, and create a cursor, and loop through the cursor reading records.hana_ml
connection context has a convenient .sql()
method.print(cchc
.sql("""SELECT USER_NAME, CREATOR, PASSWORD_CHANGE_NEEDED, IS_PASSWORD_LIFETIME_CHECK_ENABLED
FROM USERS
WHERE USER_NAME='{}'
""".format(newuser))
.collect())
.collect()
method chained after .sql()
to receive results of SELECT statement from SAP HANA. We will look closer at that method and how to include geospatial data there in next parts.hana_ml
provides as well different methods to "pythonify" queries to SAP HANA. The example above can be rewritten by chaining table
, select
'ed columns and then a filter
on the records:print(cchc.table("USERS", schema="SYS")
.select('USER_NAME', 'CREATOR', 'PASSWORD_CHANGE_NEEDED', 'IS_PASSWORD_LIFETIME_CHECK_ENABLED')
.filter("USER_NAME='{}'".format(newuser))
.collect())
But behind the scene, there is still an SQL statement constructed and executed in SAP HANA. It can be seen using .select_statement
property:
print(cchc.table("USERS", schema="SYS")
.select('USER_NAME', 'CREATOR', 'PASSWORD_CHANGE_NEEDED', 'IS_PASSWORD_LIFETIME_CHECK_ENABLED')
.filter("USER_NAME='{}'".format(newuser))
.select_statement)
print(cchc.sql(cchc
.table("USERS", schema="SYS")
.select('USER_NAME', 'CREATOR', 'PASSWORD_CHANGE_NEEDED', 'IS_PASSWORD_LIFETIME_CHECK_ENABLED')
.filter("USER_NAME='{}'".format(newuser))
.select_statement)
.collect())
hana-ml-samples
repository: https://github.com/SAP-samples/hana-ml-samples/blob/main/Python-API/usecase-examples/multimodel-anal....You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
7 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 |