下面列出了怎么用org.elasticsearch.common.settings.loader.JsonSettingsLoader的API类实例代码及写法,或者点击链接到github查看源代码。
private Settings toSettings(final BytesReference ref, final String type) {
if (ref == null || ref.length() == 0) {
return null;
}
XContentParser parser = null;
try {
parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, ref, XContentType.JSON);
parser.nextToken();
parser.nextToken();
if(!type.equals((parser.currentName()))) {
return null;
}
parser.nextToken();
return Settings.builder().put(new JsonSettingsLoader(true).load(parser.binaryValue())).build();
} catch (final IOException e) {
throw ExceptionsHelper.convertToElastic(e);
} finally {
if(parser != null) {
parser.close();
}
}
}
protected Map<String,String> initializeElasticSearchIndexSettings(String injectedConfig) {
String defaultConfig = injectedConfig;
if (org.apache.commons.lang3.StringUtils.isEmpty(injectedConfig)) {
try {
StringWriter writer = new StringWriter();
IOUtils.copy(getClass().getResourceAsStream(this.defaultIndexSettingsResource), writer, "UTF-8");
defaultConfig = writer.toString();
} catch (Exception ex) {
getLog().error("Failed to load indexSettings config from ["+ this.defaultIndexSettingsResource
+ "] for index builder [" + getName() + "]", ex);
}
}
JsonSettingsLoader loader = new JsonSettingsLoader();
Map<String, String> mergedConfig = null;
try {
mergedConfig = loader.load(defaultConfig);
} catch (IOException e) {
getLog().error("Problem loading indexSettings for index builder [" + getName() + "]", e);
}
// Set these here so we don't have to do this string concatenation
// and comparison every time through the upcoming 'for' loop
final boolean IS_DEFAULT = SearchIndexBuilder.DEFAULT_INDEX_BUILDER_NAME.equals(getName());
final String DEFAULT_INDEX = ElasticSearchConstants.CONFIG_PROPERTY_PREFIX + "index.";
final String LOCAL_INDEX = String.format("%s%s%s.",ElasticSearchConstants.CONFIG_PROPERTY_PREFIX,"index_",getName(),".");
// load anything set into the ServerConfigurationService that starts with "elasticsearch.index." this will
// override anything set in the indexSettings config
for (ServerConfigurationService.ConfigItem configItem : serverConfigurationService.getConfigData().getItems()) {
String propertyName = configItem.getName();
if (IS_DEFAULT && (propertyName.startsWith(DEFAULT_INDEX))) {
propertyName = propertyName.replaceFirst(DEFAULT_INDEX, "index.");
mergedConfig.put(propertyName, (String) configItem.getValue());
} else if (propertyName.startsWith(LOCAL_INDEX)) {
propertyName = propertyName.replaceFirst(LOCAL_INDEX, "index.");
mergedConfig.put(propertyName, (String) configItem.getValue());
}
}
if (getLog().isDebugEnabled()) {
for (Map.Entry<String,String> entry : mergedConfig.entrySet()) {
getLog().debug("Index property '" + entry.getKey() + "' set to: " + entry.getValue()
+ "' for index builder '" + getName() + "'");
}
}
return mergedConfig;
}
protected Map<String,String> initializeElasticSearchIndexSettings(String injectedConfig) {
String defaultConfig = injectedConfig;
if (org.apache.commons.lang3.StringUtils.isEmpty(injectedConfig)) {
try {
StringWriter writer = new StringWriter();
IOUtils.copy(getClass().getResourceAsStream(this.defaultIndexSettingsResource), writer, "UTF-8");
defaultConfig = writer.toString();
} catch (Exception ex) {
getLog().error("Failed to load indexSettings config from ["+ this.defaultIndexSettingsResource
+ "] for index builder [" + getName() + "]", ex);
}
}
JsonSettingsLoader loader = new JsonSettingsLoader();
Map<String, String> mergedConfig = null;
try {
mergedConfig = loader.load(defaultConfig);
} catch (IOException e) {
getLog().error("Problem loading indexSettings for index builder [" + getName() + "]", e);
}
// Set these here so we don't have to do this string concatenation
// and comparison every time through the upcoming 'for' loop
final boolean IS_DEFAULT = SearchIndexBuilder.DEFAULT_INDEX_BUILDER_NAME.equals(getName());
final String DEFAULT_INDEX = ElasticSearchConstants.CONFIG_PROPERTY_PREFIX + "index.";
final String LOCAL_INDEX = String.format("%s%s%s.",ElasticSearchConstants.CONFIG_PROPERTY_PREFIX,"index_",getName(),".");
// load anything set into the ServerConfigurationService that starts with "elasticsearch.index." this will
// override anything set in the indexSettings config
for (ServerConfigurationService.ConfigItem configItem : serverConfigurationService.getConfigData().getItems()) {
String propertyName = configItem.getName();
if (IS_DEFAULT && (propertyName.startsWith(DEFAULT_INDEX))) {
propertyName = propertyName.replaceFirst(DEFAULT_INDEX, "index.");
mergedConfig.put(propertyName, (String) configItem.getValue());
} else if (propertyName.startsWith(LOCAL_INDEX)) {
propertyName = propertyName.replaceFirst(LOCAL_INDEX, "index.");
mergedConfig.put(propertyName, (String) configItem.getValue());
}
}
if (getLog().isDebugEnabled()) {
for (Map.Entry<String,String> entry : mergedConfig.entrySet()) {
getLog().debug("Index property '" + entry.getKey() + "' set to: " + entry.getValue()
+ "' for index builder '" + getName() + "'");
}
}
return mergedConfig;
}
public static Settings decodeSettings(String encodedSettings) throws IOException {
Map<String, String> loaded = new JsonSettingsLoader().load(encodedSettings);
return Settings.builder().put(loaded).build();
}