Enabling Upload Parameters for Apps and Java Scripts - Teradata AppCenter

Teradata® AppCenter User Guide

Product
Teradata AppCenter
Release Number
2.0
Published
September 2020
Language
English (United States)
Last Update
2020-09-28
dita:mapPath
zzv1586527506119.ditamap
dita:ditavalPath
ft:empty
dita:id
B035-1111
Product Category
Analytical Ecosystem
"" User, Administrator

To enable upload of parameters for an app or Java scripts, do the following:

  1. In the app or script code, read the parameter file into memory.
    For example, if the parameter name is ‘param’, the file would be here: /data/parameters/job_id/param
  2. Convert the file to reflect the desired query.
    For example, if you have the SQL query select * from table where column1 in (${param}), replace ${param} with a list of values in the uploaded file. The uploaded file would be in the following format:

    value1

    value2

    value3

    The final SQL query would be: select * from table where column1 in (‘value1’, ‘value2’, ‘value3’)

    The following is sample JAVA code for enabling parameter upload:

    public class SampleApp
    {
        private static final String PARAMETER_VOLUME_MOUNT_PATH = "/data/parameters/";
    
        public static void main( String[] args ) throws Exception
        {
            String query1 = "select * from cfilter_demo where col1_item1 in (${param});";
    
            Try (DefaultAppCenterClient client = new DefaultAppCenterClient()) {
                client.log(LogLevel.INFO, "Replacing query parameters...");
                String query = replaceWithConfig(client, query1, "param");
                    
                client.log(LogLevel.INFO, "executing query:" + query);
                InputStream resultStream = client.streamResultsFromQuery(query);
                
                client.log(LogLevel.INFO, "Caching result...");
                client.cacheResult(resultStream, "application/json");
                
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    
        private static String getParamValueFromFile(DefaultAppCenterClient client, String configParamName) throws IOException {
            String paramFile = PARAMETER_VOLUME_MOUNT_PATH + client.getAppContext().getJobId() + "/" +
                    configParamName;
            StringBuilder configParamValue = new StringBuilder();
            boolean isFirst = true;
            try (BufferedReader br = new BufferedReader(new FileReader(paramFile))) {
                for (String line; (line = br.readLine()) != null; ) {
                    if (!isFirst) {
                        configParamValue.append(",");
                    } else {
                       isFirst = false;
                    }
                    configParamValue.append("'").append(line).append("'");
                }
            }
            return configParamValue.toString();
        }
        
        private static String replaceWithConfig(DefaultAppCenterClient client, String query, String paramName) throws IOException {
            String paramValue = getParamValueFromFile(client, paramName);
            query = query.replace("${" + paramName + "}", paramValue);
            return query;
        }