If you need to setup SAP HANA Express edition locally, I recommend checking out the amazing tutorials available at https://developers.sap.com on how to get started with SAP HANA Express edition. Personally, I set it up as a virtual machine and followed this tutorial group -> https://developers.sap.com/group.hxe-install-vm.html
To learn how to connect to SAP HANA Express edition, check out the Use Clients to Query an SAP HANA Database tutorial mission -> https://developers.sap.com/mission.hana-cloud-clients.html.
# Connect to SYSTEMDB, this is exposed in port 39013
$ hdbsql -n 192.168.1.10:39013 -u SYSTEM -p MyStrongPassw0rd1
# Check the database available to connect
hdbsql SYSTEMDB=> SELECT DATABASE_NAME, HOST, SQL_PORT FROM SYS_DATABASES.M_SERVICES where SQL_PORT != 0;
# We will get a response similar to the one below
# DATABASE_NAME,HOST,SQL_PORT
# "SYSTEMDB","hxehost",39013
# "HXE","hxehost",39015
# Enable docstore in HXE
hdbsql SYSTEMDB=> ALTER DATABASE HXE add 'docstore';
# Exit SYSTEMDB
hdbsql SYSTEMDB=> \q
# Login to HXE to create separate user. This user will be used in our script
$ hdbsql -n 192.168.1.10:39015 -u SYSTEM -p MyStrongPassw0rd1
# Create the user
hdbsql HXE=> CREATE USER DEMO PASSWORD MySecondStrongPassw0rd1 no force_first_password_change;
# Connect as our demo user and create a collection
hdbsql HXE=> CONNECT USER1 PASSWORD Password1;
hdbsql HXE=> CREATE COLLECTION SourcingProjectSourcingSystemView;
# Exit HXE
hdbsql HXE=> \q
# We are setting the port to 39015 to connect to HXE
$ hdbuserstore Set DEMOUserKey 192.168.1.10:39015 DEMO MySecondStrongPassw0rd1
# Check that the key has been created
$ hdbuserstore List
# Connect to HANA
hdbsql -attemptencrypt -U DEMOUserKey
import json
from hdbcli import dbapi
import requests
# SAP Ariba Analytical Reporting API details
OAUTH_URL = 'https://api.ariba.com/v2/oauth/token'
AR_URL = 'https://openapi.ariba.com/api/analytics-reporting-view/v1/prod/views/SourcingProjectSourcingSystemView?realm=[YOUR_REALM]&filters=%7B"createdDateFrom":"2020-06-01T00:00:00Z","createdDateTo":"2020-11-22T00:00:00Z"%7D'
BASE64_AUTHSTRING = '[YOUR_BASE64_AUTHSTRING]'
API_KEY = '[YOUR_API_KEY]'
# HANA Express details
HDB_USER_STORE_KEY = 'DemoUserKey'
COLLECTION_NAME = 'SourcingProjectSourcingSystemView'
def get_access_token():
"""Get an acces_token from the SAP Ariba OAuth Server"""
payload='grant_type=client_credentials'
headers = {
'Authorization': f'Basic {BASE64_AUTHSTRING}',
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request('POST', OAUTH_URL, headers=headers, data=payload)
return response.json()['access_token']
def documents_in_collection(cursor, coll):
"""Finds out the number of documents in the collection.
Parameters
----------
cursor : Cursor
SAP HANA database cursor
coll : str
The name of the collection to query
Returns
-------
int
The count of documents in the collection
"""
cursor.execute(f'SELECT COUNT(1) AS COUNT FROM {coll}')
# print(rows)
for row in cursor:
for col in row:
return col
#Initialize your connection
conn = dbapi.connect(
key=HDB_USER_STORE_KEY, # address, port, user and password are retreived from the hdbuserstore
encrypt=True, # must be set to True when connecting to HANA Cloud
sslValidateCertificate=False # True for HANA Cloud, False for HANA Express.
)
cursor = conn.cursor()
#If no errors, print connected
print('Connected to HANA')
payload={}
headers = {
'apiKey': API_KEY,
'Authorization': f'Bearer {get_access_token()}',
}
response = requests.request("GET", AR_URL, headers=headers, data=payload)
records = response.json()['Records']
print(f'Documents in collection before inserting data: {documents_in_collection(cursor, COLLECTION_NAME)}')
for record in records:
# Converting dict to str before replacing the key single quote with double quotes
# The conversion needs to take place because of the format that HANA expects
# JSON documents to be inserted -> {"key": 'value', "key2": 'value2'}
record_str = str(record).replace("{'", "{\"") \
.replace("':", "\":") \
.replace(", '", ", \"") \
.replace('None', 'null')
sql = f'INSERT INTO {COLLECTION_NAME} values({record_str});'
cursor.execute(sql)
print(f'Documents in collection after insert: {documents_in_collection(cursor, COLLECTION_NAME)}')
cursor.close()
conn.close()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
16 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |