Exemplo n.º 1
0
def getDatasetAsDataframe(user_id, knowage_address, dataset_name,
                          datastore_request):
    address = "http://" + knowage_address + "/knowage/restful-services/2.0/datasets/" + dataset_name + "/data?offset=0&size=-1"
    auth_token = security.buildAuthToken(user_id)
    headers = {'Authorization': auth_token}
    #rest request for dataset
    payload = datastore_request
    r = requests.post(address, headers=headers, data=payload)
    #retrieve column names from metadata
    names = r.json()["metaData"]["fields"]
    column_names = []
    column_types = {}
    for x in names:
        if type(x) is dict:
            column_names.append(x['header'])
            if x["type"] == "float":
                column_types.update({x['name']: "float64"})
            elif x["type"] == "float":
                column_types.update({x['name']: "int64"})
    #save data as dataframe
    df = pd.DataFrame(r.json()["rows"])
    #cast types
    df = df.astype(column_types)
    #drop first column (redundant)
    df.drop(columns=['id'], inplace=True)
    # assign column names
    df.columns = column_names
    return df
Exemplo n.º 2
0
def getDatasetAsDataframe(widget):
    address = widget.knowage_address + "/knowage/restful-services/2.0/datasets/" + widget.dataset_name + "/data?offset=0&size=-1"
    auth_token = security.buildAuthToken(widget.user_id)
    headers = {'Authorization': auth_token}
    #rest request for dataset
    payload = widget.datastore_request
    r = requests.post(address, headers=headers, data=payload)
    # retrieve column names from metadata
    response_body = r.json()
    if 'errors' in response_body:
        raise KnowageRestServiceException(address)
    col_names = response_body["metaData"]["fields"]
    rows = response_body["rows"]
    df = convertKnowageDatasetToDataframe(col_names, rows)
    return df
Exemplo n.º 3
0
def getDatasetAsDataframe(widget):
    address = widget.knowage_address + "/knowage/restful-services/2.0/datasets/" + widget.dataset_name + "/data?offset=0&size=-1"
    auth_token = security.buildAuthToken(widget.user_id)
    headers = {'Authorization': auth_token}
    #rest request for dataset
    payload = widget.datastore_request
    r = requests.post(address, headers=headers, data=payload)
    #retrieve column names from metadata
    names = r.json()["metaData"]["fields"]
    #get cast types map
    column_names, column_types = get_cast_types(names)
    #save data as dataframe
    df = pd.DataFrame(r.json()["rows"])
    if not df.empty:
        try:
            #cast types
            df = df.astype(column_types)
        except Exception as e:
            logging.warning("Could not cast dataframe types")
        #drop first column (redundant)
        df.drop(columns=['id'], inplace=True)
        # assign column names
        df.columns = column_names
    return df