Documentation

Working with Tables

Abstract

SummaryExamples of how to work with tables using various table functions.

Some basic examples of working with tables via alooma.py.

Create a table from a mapping object

An example of how to create a table from a mapping object:

Copy
import alooma
import requests
from copy import deepcopy

# Authenticate
api = alooma.Client(user, password, account_name=account)

# Set values
#
# Here we're using existing objects but you could create them instead
#
gmapping = api.get_mapping("PUBLIC.ATable") # use get_event_types() for list
gschema  = "PUBLIC" # can use get_schemas() to get a list of existing schemas
gtable   = "GTest2" # name for the table

def create_table_from_mapping(mapping, schema, table, dist_keys=[], sort_keys=[]):
    """ Create Table Based on Mapping Fields """
    # sort and dist keys for Redshift only
    columns = _extract_column_list(deepcopy(mapping),
                                        dist_keys=dist_keys,
                                        sort_keys=sort_keys)
    table_name = "%s/%s" % (schema, table)
    return api.create_table(table_name, columns)

def _extract_column_list(mapping, dist_keys=[], sort_keys=[]):
    """ Extract Columns Using Mapping and Desired Keys """
    # sort and dist keys for Redshift only
    fields = []
    for field in mapping["fields"]:
        if len(field["fields"]) > 0:
            fields += _extract_column_list(field)
        if field["mapping"]:
            column_data = field["mapping"]
            if column_data["isDiscarded"]:
                continue
            if "subFields" in column_data:
                del column_data["subFields"]
            if "isDiscarded" in column_data:
                del column_data["isDiscarded"]
            if "machineGenerated" in column_data:
                del column_data["machineGenerated"]

            # Add Dist Key if Desired
            if field['fieldName'] in dist_keys:
                column_data['distKey'] = True
            # Add Sort Keys if Desired
            if field['fieldName'] in sort_keys:
                column_data['sortKeyIndex'] = [i for i,v in enumerate(sort_keys)
                                               if v == field['fieldName']][0]

            fields.append(column_data)

    return fields

newtable = create_table_from_mapping(gmapping, gschema, gtable)
Add Primary Keys to Tables

An example of how to add primary keys to a table (or view).  

Copy
import json
import alooma

# Authenticate
api = alooma.Client(user, password, account_name=account)

input_label = "<input_label>"
table = "<table or view name>"
custom_primary_keys = ["columns", "to", "use"]

# Adding given table+custom keys to input
inp_cfg = api.get_inputs(input_label)[0]

raw_custom_pks = inp_cfg['configuration']['custom_pks'] or '{}'
custom_pks = json.loads(raw_custom_pks)
custom_pks[table] = custom_primary_keys
inp_cfg['configuration']['custom_pks'] = json.dumps(custom_pks)

api.edit_input(inp_cfg)

#
# Then add the table to the list of tables to replicate

Note that after you add a primary key to a table, you need to add the table to the list of tables being replicated for the input. You can do this in the UI by editing the input or you can do this via the API. See "How to edit an input (add a table)" here.

Search results

    No results found