org.apache.commons.text.StringSubstitutor Java Examples
The following examples show how to use
org.apache.commons.text.StringSubstitutor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Discord.java From Rails with GNU General Public License v2.0 | 7 votes |
public void sendMessage(String player) { setConfig(); if ( webhook == null ) { return; } Map<String, String> keys = new HashMap<>(); keys.put("game", root.getGameName()); //keys.put("gameName", StringUtils.defaultIfBlank(root.getGameData().getUsersGameName(), "[none]")); keys.put("round", gameUiManager.getCurrentRound().getRoundName()); keys.put("current", StringUtils.defaultIfBlank(playerNameMappings.get(player), player)); keys.put("previous", StringUtils.defaultIfBlank(observer.getFormerPlayer().getId(), "[none]")); String msgBody = StringSubstitutor.replace(body, keys); log.debug("Sending message '{}' to Discord for user {}", msgBody, player); HttpPost httpPost = new HttpPost(webhook); try { httpPost.setEntity(new StringEntity(msgBody)); httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); httpPost.setHeader(HttpHeaders.USER_AGENT, "18xx Rails"); CloseableHttpResponse response = httpClient.execute(httpPost); if ( response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT ) { log.debug("Unexpected Discord response: {}", response); } response.close(); } catch (IOException e) { log.error("Error sending message to Discord", e); } }
Example #2
Source File: ApiMockUpdate.java From component-runtime with Apache License 2.0 | 6 votes |
private static CompletableFuture<Void> capture(final Map<String, byte[]> files, final Executor executor, final String path, final String base, final Map<String, String> templates, final Function<WebTarget, byte[]> target) { return CompletableFuture.runAsync(() -> { final String outputPath = new StringSubstitutor(templates, "{", "}").replace(path); log.info("Trying to grab {}", outputPath); final Client client = ClientBuilder.newClient(); try { WebTarget webTarget = client.target(base).path(path); for (final Map.Entry<String, String> tpl : templates.entrySet()) { webTarget = webTarget.resolveTemplate(tpl.getKey(), tpl.getValue()); } webTarget.property("http.connection.timeout", 30000L).property("http.receive.timeout", 60000L); files.put(outputPath, target.apply(webTarget)); log.info("Grabbed to grab {}", outputPath); } catch (final ProcessingException | WebApplicationException ex) { log.error("Error on {}", outputPath, ex); throw ex; } finally { client.close(); } }, executor); }
Example #3
Source File: SettingsReader.java From elasticsearch-beyonder with Apache License 2.0 | 6 votes |
/** * Read a file content from the classpath * @param file filename * @return The file content */ public static String readFileFromClasspath(String file) { logger.trace("Reading file [{}]...", file); String content = null; try (InputStream asStream = SettingsReader.class.getClassLoader().getResourceAsStream(file)) { if (asStream == null) { logger.trace("Can not find [{}] in class loader.", file); return null; } content = IOUtils.toString(asStream, "UTF-8"); } catch (IOException e) { logger.warn("Can not read [{}].", file); } return StringSubstitutor.replace(content, System.getenv()); }
Example #4
Source File: BuildURI.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { if (args.length < 1) { throw new ValueExprEvaluationException("Incorrect number of arguments"); } if (!(args[0] instanceof Literal)) { throw new ValueExprEvaluationException("First argument must be a string"); } Literal s = (Literal) args[0]; String tmpl = s.getLabel(); Map<String, String> mappings = new HashMap<>(args.length); for (int i = 1; i < args.length; i++) { mappings.put(Integer.toString(i), args[i].stringValue()); } String newValue = StringSubstitutor.replace(tmpl, mappings, "{?", "}"); if (tmpl.charAt(0) == '<' && tmpl.charAt(tmpl.length() - 1) == '>') { return valueFactory.createURI(newValue.substring(1, newValue.length() - 1)); } throw new ValueExprEvaluationException("Invalid URI template: " + tmpl); }
Example #5
Source File: BuildString.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { if (args.length < 1) { throw new ValueExprEvaluationException("Incorrect number of arguments"); } if (!(args[0] instanceof Literal)) { throw new ValueExprEvaluationException("First argument must be a string"); } Literal s = (Literal) args[0]; String tmpl = s.getLabel(); Map<String, String> mappings = new HashMap<>(args.length); for (int i = 1; i < args.length; i++) { mappings.put(Integer.toString(i), args[i].stringValue()); } String newValue = StringSubstitutor.replace(tmpl, mappings, "{?", "}"); return valueFactory.createLiteral(newValue); }
Example #6
Source File: RelationshipTypeJoinGenerator.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static String addRelationshipWhereClause( Long relationshipTypeId, RelationshipEntity relationshipEntity ) { String sql = new StringSubstitutor( ImmutableMap.<String, Long> builder().put( "relationshipid", relationshipTypeId ).build() ) .replace( RELATIONSHIP_JOIN ); sql += " AND "; switch ( relationshipEntity ) { case TRACKED_ENTITY_INSTANCE: return sql + "tei.uid = ax.tei "; case PROGRAM_STAGE_INSTANCE: return sql + "psi.uid = ax.psi "; case PROGRAM_INSTANCE: return sql + "pi.uid = ax.pi "; default: throw new IllegalQueryException( new ErrorMessage( ErrorCode.E7227, relationshipEntity.name() ) ); } }
Example #7
Source File: BigQueryConverters.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Returns {@code String} using Key/Value style formatting. * * @param formatTemplate a String with bracketed keys to apply "I am a {key}" * @param row is a TableRow object which is used to supply key:values to the template * * <p> Extracts TableRow fields and applies values to the formatTemplate. * ie. formatStringTemplate("I am {key}"{"key": "formatted"}) -> "I am formatted" */ public static String formatStringTemplate(String formatTemplate, TableRow row) { // Key/Value Map used to replace values in template Map<String, String> values = new HashMap<>(); // Put all column/value pairs into key/value map Set<String> rowKeys = row.keySet(); for (String rowKey : rowKeys) { // Only String types can be used in comparison if(row.get(rowKey) instanceof String) { values.put(rowKey, (String) row.get(rowKey)); } } // Substitute any templated values in the template String result = StringSubstitutor.replace(formatTemplate, values, "{", "}"); return result; }
Example #8
Source File: AbstractTagGroupQueryBuilder.java From sailfish-core with Apache License 2.0 | 6 votes |
public String build() { Map<String, String> variables = new HashMap<>(); variables.put("tempTable", TAG_GROUP_TEMP_TABLE); variables.put("dimensions", dimensions); variables.put("passed", String.valueOf(StatusType.PASSED.getId())); variables.put("conditionallyPassed", String.valueOf(StatusType.CONDITIONALLY_PASSED.getId())); variables.put("failed", String.valueOf(StatusType.FAILED.getId())); variables.put("nestedDimensions", nestedDimensions); variables.put("totalTimeField", getTotalTimeField()); variables.put("nestedResultIds", joinType.nestedResultFields); variables.put("nestedJoinTables", joinType.nestedJoinTables); variables.put("nestedGroupFields", joinType.nestedGroupFields); variables.put("joinTables", joinType.resultJoinTables); variables.put("where", buildWhereClause()); variables.put("groupCount", groupCount); variables.put("groupFields", groupFields); variables.put("orderFields", orderFields); return new StringSubstitutor(variables).replace(MAIN_QUERY_PATTERN); }
Example #9
Source File: App.java From cuba with Apache License 2.0 | 6 votes |
protected void initHomeDir() { String homeDir = System.getProperty(DesktopAppContextLoader.HOME_DIR_SYS_PROP); if (StringUtils.isBlank(homeDir)) { homeDir = getDefaultHomeDir(); } homeDir = StringSubstitutor.replaceSystemProperties(homeDir); System.setProperty(DesktopAppContextLoader.HOME_DIR_SYS_PROP, homeDir); File file = new File(homeDir); if (!file.exists()) { boolean success = file.mkdirs(); if (!success) { System.out.println("Unable to create home dir: " + homeDir); System.exit(-1); } } if (!file.isDirectory()) { System.out.println("Invalid home dir: " + homeDir); System.exit(-1); } }
Example #10
Source File: BigQueryConverters.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Return a formatted String Using Key/Value Style formatting * from the TableRow applied to the Format Template. * ie. formatStringTemplate("I am {key}"{"key": "formatted"}) -> "I am formatted" */ public static String formatStringTemplate(String formatTemplate, TableRow row) { // Key/Value Map used to replace values in template Map<String, String> values = new HashMap<>(); // Put all column/value pairs into key/value map Set<String> rowKeys = row.keySet(); for (String rowKey : rowKeys) { // Only String types can be used in comparison if(row.get(rowKey) instanceof String) { values.put(rowKey, (String) row.get(rowKey)); } } // Substitute any templated values in the template String result = StringSubstitutor.replace(formatTemplate, values, "{", "}"); return result; }
Example #11
Source File: JdbcCompleteDataSetRegistrationExchangeStore.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
private static String createQuery( ExportParams params ) { IdSchemes idSchemes = params.getOutputIdSchemes() != null ? params.getOutputIdSchemes() : new IdSchemes(); ImmutableMap.Builder<String, String> namedParamsBuilder = ImmutableMap.<String, String>builder() .put( DATA_SET_SCHEME, idSchemes.getDataSetIdScheme().getIdentifiableString().toLowerCase() ) .put( ORG_UNIT_SCHEME, idSchemes.getOrgUnitIdScheme().getIdentifiableString().toLowerCase() ) .put( ATTR_OPT_COMBO_SCHEME, idSchemes.getAttributeOptionComboIdScheme().getIdentifiableString().toLowerCase() ); String sql = "SELECT ds.${dsScheme} AS dsid, pe.startdate AS pe_start, pt.name AS ptname, ou.${ouScheme} AS ouid, " + "aoc.${aocScheme} AS aocid, cdsr.storedby AS storedby, cdsr.date AS created, cdsr.completed AS completed " + "FROM completedatasetregistration cdsr " + "INNER JOIN dataset ds ON ( cdsr.datasetid=ds.datasetid ) " + "INNER JOIN period pe ON ( cdsr.periodid=pe.periodid ) " + "INNER JOIN periodtype pt ON ( pe.periodtypeid=pt.periodtypeid ) " + "INNER JOIN organisationunit ou ON ( cdsr.sourceid=ou.organisationunitid ) " + "INNER JOIN categoryoptioncombo aoc ON ( cdsr.attributeoptioncomboid = aoc.categoryoptioncomboid ) "; sql += createOrgUnitGroupJoin( params ); sql += createDataSetClause( params, namedParamsBuilder ); sql += createOrgUnitClause( params, namedParamsBuilder ); sql += createPeriodClause( params, namedParamsBuilder ); sql += createCreatedClause( params, namedParamsBuilder ); sql += createLimitClause( params, namedParamsBuilder ); sql = new StringSubstitutor( namedParamsBuilder.build(), "${", "}" ).replace( sql ); log.debug( "CompleteDataSetRegistrations query: " + sql ); return sql; }
Example #12
Source File: AppProperties.java From cuba with Apache License 2.0 | 5 votes |
private String handleInterpolation(String value) { StringSubstitutor substitutor = new StringSubstitutor(key -> { String property = getSystemOrAppProperty(key); return property != null ? property : System.getProperty(key); }); return substitutor.replace(value); }
Example #13
Source File: UrlProviderImpl.java From aem-core-cif-components with Apache License 2.0 | 5 votes |
private String toUrl(SlingHttpServletRequest request, Page page, Map<String, String> params, String template, String selectorFilter) { if (page != null && params.containsKey(selectorFilter)) { Resource pageResource = page.adaptTo(Resource.class); boolean deepLink = !WCMMode.DISABLED.equals(WCMMode.fromRequest(request)); if (deepLink) { Resource subPageResource = toSpecificPage(pageResource, params.get(selectorFilter)); if (subPageResource != null) { pageResource = subPageResource; } } params.put(PAGE_PARAM, pageResource.getPath()); } String prefix = "${", suffix = "}"; // variables have the format ${var} if (template.contains("{{")) { prefix = "{{"; suffix = "}}"; // variables have the format {{var}} } StringSubstitutor sub = new StringSubstitutor(params, prefix, suffix); String url = sub.replace(template); url = StringUtils.substringBeforeLast(url, "#" + prefix); // remove anchor if it hasn't been substituted if (url.contains(prefix)) { LOGGER.warn("Missing params for URL substitution. Resulted URL: {}", url); } return url; }
Example #14
Source File: OpenCensusPluginSink.java From ffwd with Apache License 2.0 | 5 votes |
private String getOutputMetricName(Metric metric) { StringSubstitutor sub = new StringSubstitutor((key) -> { if (key.equals("key")) { return metric.getKey(); } else { return metric.getTags().get(key); } }); return sanitizeName(sub.replace(outputMetricNamePattern)); }
Example #15
Source File: AboutDialog.java From RemoteSupportTool with Apache License 2.0 | 5 votes |
private String replaceVariables(String text) { Properties replacements = new Properties(); replacements.setProperty("app.title", settings.getString("title")); replacements.setProperty("app.version", settings.getString("version")); replacements.setProperty("openjdk.name", StringUtils.defaultIfBlank( SystemUtils.JAVA_RUNTIME_NAME, SystemUtils.JAVA_VM_NAME)); replacements.setProperty("openjdk.version", StringUtils.defaultIfBlank( SystemUtils.JAVA_RUNTIME_VERSION, SystemUtils.JAVA_VERSION)); return StringSubstitutor.replace(text, replacements); }
Example #16
Source File: ContentLoaderHandler.java From syncope with Apache License 2.0 | 5 votes |
public ContentLoaderHandler( final DataSource dataSource, final String rootElement, final boolean continueOnError, final Environment env) { this.jdbcTemplate = new JdbcTemplate(dataSource); this.rootElement = rootElement; this.continueOnError = continueOnError; this.envParamSubstitutor = new StringSubstitutor(key -> { String value = env.getProperty(key); return StringUtils.isBlank(value) ? null : value; }); }
Example #17
Source File: LocationDescriptor.java From alf.io with GNU General Public License v3.0 | 5 votes |
public static String getMapUrl(String lat, String lng, Map<ConfigurationKeys, ConfigurationManager.MaybeConfiguration> geoConf) { Map<String, String> params = new HashMap<>(); params.put("latitude", lat); params.put("longitude", lng); ConfigurationKeys.GeoInfoProvider provider = getProvider(geoConf); String mapUrl = mapUrl(provider); fillParams(provider, geoConf, params); return new StringSubstitutor(params).replace(mapUrl); }
Example #18
Source File: Slack.java From Rails with GNU General Public License v2.0 | 5 votes |
public void sendMessage(String player) { setConfig(); if ( webhook == null ) { return; } Map<String, String> keys = new HashMap<>(); keys.put("game", root.getGameName()); //keys.put("gameName", StringUtils.defaultIfBlank(root.getGameData().getUsersGameName(), "[none]")); keys.put("round", gameUiManager.getCurrentRound().getRoundName()); keys.put("current", StringUtils.defaultIfBlank(playerNameMappings.get(player), player)); keys.put("previous", StringUtils.defaultIfBlank(observer.getFormerPlayer().getId(), "[none]")); String msgBody = StringSubstitutor.replace(body, keys); log.debug("Sending message '{}' to Slack for user {}", msgBody, player); HttpPost httpPost = new HttpPost(webhook); try { httpPost.setEntity(new StringEntity(msgBody)); httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); httpPost.setHeader(HttpHeaders.USER_AGENT, "18xx Rails"); CloseableHttpResponse response = httpClient.execute(httpPost); if ( response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT ) { // TODO: verify result log.debug("Unexpected Slack response: {}", response); } response.close(); } catch (IOException e) { log.error("Error sending message to Slack", e); } }
Example #19
Source File: QueryConfigUtils.java From kayenta with Apache License 2.0 | 5 votes |
private static String expandTemplate( String templateToExpand, Map<String, String> templateBindings) { try { log.debug("Expanding template='{}' with variables={}", templateToExpand, templateBindings); StringSubstitutor substitutor = new StringSubstitutor(templateBindings); substitutor.setEnableUndefinedVariableException(true); String expandedTemplate = substitutor.replace(templateToExpand); log.debug("Expanded template='{}'", expandedTemplate); return expandedTemplate; } catch (Exception e) { throw new IllegalArgumentException( "Problem evaluating custom filter template: " + templateToExpand, e); } }
Example #20
Source File: CommonUtils.java From aem-component-generator with Apache License 2.0 | 5 votes |
/** * Method to read the content of the provided template file as string. * * @param filePath Path to the template file in the project * @param generationConfig The {@link GenerationConfig} object with all the populated values * @return String return content of the resource file as string or null when file not exists */ public static String getTemplateFileAsString(String filePath, GenerationConfig generationConfig) { try (InputStream inputStream = CommonUtils.class.getClassLoader().getResourceAsStream(filePath)) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); Map<String, String> stringsToReplaceValueMap = getStringsToReplaceValueMap(generationConfig); StringSubstitutor stringSubstitutor = new StringSubstitutor(stringsToReplaceValueMap); String content = reader.lines().collect(Collectors.joining(System.lineSeparator())); return stringSubstitutor.replace(content); } catch (IOException e) { LOG.error("Failed to read " + filePath + " from the classpath.", e); } return null; }
Example #21
Source File: PubsubMessageToTemplatedString.java From gcp-ingestion with Mozilla Public License 2.0 | 5 votes |
@Override public String apply(PubsubMessage message) { final String batchKey = StringSubstitutor.replace(template, DerivedAttributesMap.of(message.getAttributesMap())); if (batchKey.contains("$")) { throw new IllegalArgumentException("Element did not contain all the attributes needed to" + " fill out variables in the configured template: " + template); } return batchKey; }
Example #22
Source File: SchemaStore.java From gcp-ingestion with Mozilla Public License 2.0 | 5 votes |
/** * Return the parsed schema corresponding to doctype and namespace parsed from attributes. * * @throws SchemaNotFoundException if schema matching the attributes exists */ public T getSchema(Map<String, String> attributes) throws SchemaNotFoundException { ensureSchemasLoaded(); if (attributes == null) { throw new SchemaNotFoundException("No schema for message with null attributeMap"); } // This is the path provided by mozilla-pipeline-schemas final String path = StringSubstitutor.replace( "${document_namespace}/${document_type}/${document_type}.${document_version}", attributes) + schemaSuffix(); return getSchema(path); }
Example #23
Source File: MergeStatementBuilder.java From DataflowTemplates with Apache License 2.0 | 5 votes |
public String buildMergeStatement( String replicaTable, String stagingTable, List<String> primaryKeyFields, List<String> allFields) { // Key/Value Map used to replace values in template Map<String, String> mergeQueryValues = new HashMap<>(); mergeQueryValues.put("replicaTable", replicaTable); mergeQueryValues.put("replicaAlias", REPLICA_TABLE_NAME); mergeQueryValues.put("stagingAlias", STAGING_TABLE_NAME); mergeQueryValues.put("deleteColumn", configuration.deletedFieldName()); // TODO require config options mergeQueryValues.put( "stagingViewSql", buildLatestViewOfStagingTable( stagingTable, allFields, primaryKeyFields, configuration.timestampFieldName(), configuration.deletedFieldName(), configuration.partitionRetention())); mergeQueryValues.put("joinCondition", buildJoinConditions(primaryKeyFields, REPLICA_TABLE_NAME, STAGING_TABLE_NAME)); mergeQueryValues.put("timestampCompareSql", buildTimestampCheck(configuration.timestampFieldName())); mergeQueryValues.put("mergeUpdateSql", buildUpdateStatement(allFields)); mergeQueryValues.put("mergeInsertSql", buildInsertStatement(allFields)); String mergeStatement = StringSubstitutor.replace(configuration.mergeQueryTemplate(), mergeQueryValues, "{", "}"); return mergeStatement; }
Example #24
Source File: MergeStatementBuilder.java From DataflowTemplates with Apache License 2.0 | 5 votes |
public String buildMergeStatement( String replicaTable, String stagingTable, List<String> primaryKeyFields, List<String> allFields) { // Key/Value Map used to replace values in template Map<String, String> mergeQueryValues = new HashMap<>(); mergeQueryValues.put("replicaTable", replicaTable); mergeQueryValues.put("replicaAlias", REPLICA_TABLE_NAME); mergeQueryValues.put("stagingAlias", STAGING_TABLE_NAME); mergeQueryValues.put("deleteColumn", configuration.deletedFieldName()); // TODO require config options mergeQueryValues.put( "stagingViewSql", buildLatestViewOfStagingTable( stagingTable, allFields, primaryKeyFields, configuration.timestampFieldName(), configuration.deletedFieldName(), configuration.partitionRetention())); mergeQueryValues.put("joinCondition", buildJoinConditions(primaryKeyFields, REPLICA_TABLE_NAME, STAGING_TABLE_NAME)); mergeQueryValues.put("timestampCompareSql", buildTimestampCheck(configuration.timestampFieldName())); mergeQueryValues.put("mergeUpdateSql", buildUpdateStatement(allFields)); mergeQueryValues.put("mergeInsertSql", buildInsertStatement(allFields)); String mergeStatement = StringSubstitutor.replace(configuration.mergeQueryTemplate(), mergeQueryValues, "{", "}"); return mergeStatement; }
Example #25
Source File: ConvertUtil.java From gushici with GNU General Public License v3.0 | 5 votes |
public static String getSvg(String content, Double fontSize, Double spacing) { int length = content.length(); double realWidth = length * fontSize + (length-1) * spacing; double realFontSize = fontSize; Map<String, Object> templateValue = new HashMap<>(); templateValue.put("width", realWidth); templateValue.put("font-size", realFontSize); templateValue.put("spacing", spacing); templateValue.put("content", content); templateValue.put("height", realFontSize * 1.1); StringSubstitutor sub = new StringSubstitutor(templateValue); return sub.replace(svgTemplate); }
Example #26
Source File: LogFile.java From Rails with GNU General Public License v2.0 | 4 votes |
@Override public String getPropertyValue() { ConfigManager.initConfiguration(false); String logDir = Config.get(CONFIG_LOG_DIRECTORY); if ( StringUtils.isBlank(logDir) ) { logDir = System.getProperty("user.home"); switch ( SystemOS.get() ) { case MAC: case UNIX: logDir += File.separator + ".rails" + File.separator; break; case WINDOWS: // should point to the Documents directory on Windows logDir = FileSystemView.getFileSystemView().getDefaultDirectory().getPath() + File.separator + "Rails" + File.separator; break; default: logDir += File.separator + "Rails" + File.separator; // nothing to do } } String fileName = Config.get(CONFIG_LOG_FILENAME_PATTERN); if ( StringUtils.isNotBlank(fileName) ) { if ( fileName.startsWith(File.separator ) ) { // use the whole thing as is logDir = ""; } // support inject of date, etc StringSubstitutor substitutor = StringSubstitutor.createInterpolator(); substitutor.setEnableSubstitutionInVariables(true); fileName = substitutor.replace(fileName); if ( SystemOS.get() == SystemOS.MAC || SystemOS.get() == SystemOS.UNIX ) { // sanitize fileName = fileName.replace(' ', '_'); } } else { fileName = "18xx_" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".log"; } return logDir + fileName; }
Example #27
Source File: KeyByBigQueryTableDestination.java From gcp-ingestion with Mozilla Public License 2.0 | 4 votes |
/** * Return the appropriate table destination instance for the given document type and other * attributes. */ public TableDestination getTableDestination(Map<String, String> attributes) { attributes = new HashMap<>(attributes); // We coerce all docType and namespace names to be snake_case and to remove invalid // characters; these transformations MUST match with the transformations applied by the // jsonschema-transpiler and mozilla-schema-generator when creating table schemas in BigQuery. final String namespace = attributes.get(Attribute.DOCUMENT_NAMESPACE); final String docType = attributes.get(Attribute.DOCUMENT_TYPE); if (namespace != null) { attributes.put(Attribute.DOCUMENT_NAMESPACE, getAndCacheNormalizedName(namespace)); } if (docType != null) { attributes.put(Attribute.DOCUMENT_TYPE, getAndCacheNormalizedName(docType)); } // Only letters, numbers, and underscores are allowed in BigQuery dataset and table names, // but some doc types and namespaces contain '-', so we convert to '_'; we don't pass all // values through getAndCacheBqName to avoid expensive regex operations and polluting the // cache of transformed field names. attributes = Maps.transformValues(attributes, v -> v.replaceAll("-", "_")); final String tableSpec = StringSubstitutor.replace(tableSpecTemplate.get(), attributes); // Send to error collection if incomplete tableSpec; $ is not a valid char in tableSpecs. if (tableSpec.contains("$")) { throw new IllegalArgumentException("Element did not contain all the attributes needed to" + " fill out variables in the configured BigQuery output template: " + tableSpecTemplate.get()); } final TableDestination tableDestination = new TableDestination(tableSpec, null, new TimePartitioning().setField(partitioningField.get()), new Clustering().setFields(clusteringFields.get())); final TableReference ref = BigQueryHelpers.parseTableSpec(tableSpec); final DatasetReference datasetRef = new DatasetReference().setProjectId(ref.getProjectId()) .setDatasetId(ref.getDatasetId()); if (bqService == null) { bqService = BigQueryOptions.newBuilder().setProjectId(ref.getProjectId()) .setRetrySettings(RETRY_SETTINGS).build().getService(); } // Get and cache a listing of table names for this dataset. Set<String> tablesInDataset; if (tableListingCache == null) { // We need to be very careful about settings for the cache here. We have had significant // issues in the past due to exceeding limits on BigQuery API requests; see // https://bugzilla.mozilla.org/show_bug.cgi?id=1623000 tableListingCache = CacheBuilder.newBuilder().expireAfterWrite(Duration.ofMinutes(10)) .build(); } try { tablesInDataset = tableListingCache.get(datasetRef, () -> { Set<String> tableSet = new HashSet<>(); Dataset dataset = bqService.getDataset(ref.getDatasetId()); if (dataset != null) { dataset.list().iterateAll().forEach(t -> { tableSet.add(t.getTableId().getTable()); }); } return tableSet; }); } catch (ExecutionException e) { throw new UncheckedExecutionException(e.getCause()); } // Send to error collection if dataset or table doesn't exist so BigQueryIO doesn't throw a // pipeline execution exception. if (tablesInDataset.isEmpty()) { throw new IllegalArgumentException("Resolved destination dataset does not exist or has no " + " tables for tableSpec " + tableSpec); } else if (!tablesInDataset.contains(ref.getTableId())) { throw new IllegalArgumentException("Resolved destination table does not exist: " + tableSpec); } return tableDestination; }
Example #28
Source File: DynamicPathTemplate.java From gcp-ingestion with Mozilla Public License 2.0 | 4 votes |
/** Return the dynamic part of the output path with placeholders filled in. */ private String replaceDynamicPart(Map<String, String> attributes) { return StringSubstitutor.replace(dynamicPart, attributes); }
Example #29
Source File: DefaultDhisConfigurationProvider.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 4 votes |
private void substituteEnvironmentVariables( Properties properties ) { final StringSubstitutor substitutor = new StringSubstitutor( System.getenv() ); // Matches on ${...} properties.entrySet().forEach( entry -> entry.setValue( substitutor.replace( entry.getValue() ).trim() ) ); }
Example #30
Source File: RelationshipTypeJoinGeneratorTest.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 4 votes |
private String addWhere( RelationshipType relationshipType ) { return new StringSubstitutor( ImmutableMap.<String, Long> builder().put( "relationshipid", relationshipType.getId() ).build() ) .replace( RelationshipTypeJoinGenerator.RELATIONSHIP_JOIN ); }