Loading Data from External Schemas

Objective

After completing this lesson, you will be able to describe how to setup access to external schemas.

Cross-Schema Access

Your database artifacts such as tables and procedures are defined using source files that belong to a database module within a project. When you build the database module, a run-time container is generated where all run-time database objects are stored.

When your database objects refer to other database objects, those objects must be part of the same container. To achieve this, you need to ensure all database objects that are referred to are defined using source files in the same database module. This means that when the container is generated, all run-time objects are part of the same container.

But very often, you need to refer to database objects that are outside your container. For example, you need to read data from a classic database schema, or call a procedure in another database schema. By default, this is not permitted. The same is true if you want to read from other containers. XSA enforces strict isolation of database objects. This is ideal in many scenarios, but not always.

Quite often the data that needs to be read is stored in tables that are not defined in a database module of your project. This is the case when we need to load data into the data warehouse. In this case we don't need source files to define each table. The tables exist only as run-time objects in a classic schema. The good news is that a container can access these objects with some simple setup.

To provide access to database objects that are stored in external schemas, you need to setup up two key components:

  • User-provided service - also known as a cross-schema service, this is a one-time setup typically performed by an adminstrator to provide the linkage service. A developer simply needs to know the name of the service and adds this to the project's mta.yaml descriptor file.
  • Synonyms - for each database object that is referred to, a synonym is needed. The developers can define synonyms in the database module using synonym source files, but quite often the synonyms are shared across projects so they could be maintained centrally by the administrators and the synonym files imported to each project. This helps save a lot of time. Plus if the location of the database objects change (move to a new schema), or the name of the objects change, the synonyms can be managed centrally and the soiurce files re-imported to each project.

As well as the two key components mentioned above, there are other settings to take care of, including granting privileges to the owner (technical user) of the container so that it is able to access the external database objects. This is usually a select privilege. A .hdbgrants source file stored in the database module is used to define the required privileges. Yo also need to add the name of the cross-schema service to the mta.yaml file in your project.

To provide access to database objects that are stored in other containers within your run-time space, you need to setup up only one of the components:

  • Synonyms - for each database object that is referred to, a synonym is still needed.

A user provided service is not needed in this case. In order to reach the external container you will still have to include the name of a service in your mta.yaml file. But this service is not a user-provided service, but a service that is automatically generated for each container, called quite simply, the container service. You will also need to define the privileges in the .hdbgrants file.

Note

In this course we do not go into the details of user defined services because this is usually setup by the administrator and not by a developer. Special privileges are required to perform the setup because access is being granted to various schemas. Some schemas might contain sensitive data. There are lots of on-line resources that explain how to setup the service.

Synonyms

Following our reference architecture, data provisioning to the staging layer takes place using synonyms. A synonym is created using a source file with the extension .hdbsynonym.

Synonyms are aliases for database objects, but are not real database objects by themselves. Synonyms can be created for tables (this includes virtual tables), views, procedures, table functions, scalar functions and sequences. In our example, we created the synonyms in a single source file called erp.hdbsynonym which is located in the folder HDW410/BIonHana/src/STAGE/synonyms.

Although it is possible to create an individual source files for each synonym, it is good practice to group synonyms so that each source file contains multiple synonyms. You could do this by including all synonyms for all objects that belong to an external schema, as we have done. Or create a source file that contains synonyms that map to a specific database object type, such as synonyms for views or synonyms for functions. Bear in mind that other projects might want to use the same synonyms file so try to develop the synonym file with reuse in mind.

To edit the synonyms, you can either use the Code Editor, for plain code-based programming, or the graphical Synonym Editor which enables GUI-oriented programming that does not require knowledge about JSON syntax, which is applied in the .hdbsynonymfile.

Note

In Web IDE there is no dedicated menu option to create a synonym. You choose the option NewDatabase Artifact and then use the selector to choose the .hdbsynonym. Or you can choose NewFile and type in the name of the file including the extension.