用户、管理员
要为应用程序或 Java 脚本启用参数上载,请执行以下操作:
- 在应用程序或脚本代码中,将参数文件读入内存中。例如,如果参数名称为 ‘param’,则文件将位于此目录:/data/parameters/job_id/param
- 转换文件以反映所需的查询。例如,如果您有 SQL 查询 select * from table where column1 in (${param}),则将 ${param} 替换为上载的文件中的值列表。上载的文件将具有以下格式:
value1
value2
value3
最终 SQL 查询将是:select * from table where column1 in (‘value1’, ‘value2’, ‘value3’)
以下是用于启用参数上载的示例 JAVA 代码:
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; }