cacheConfig.xml中读取配置文件信息三2010-07-07

本文介绍了一个使用XPath表达式从XML文档中解析特定元素内容的Java类。该类提供了几个方法来读取默认Keyspace、默认ColumnFamily及DataBindings映射。
/** 解析默认Keyspace.
*/
private String readDefaultKeyspace(XPath aXpath, Document aDoc)
throws XPathExpressionException
{
XPathExpression pathExpression = aXpath.compile("//defaultKeyspace");
Node node = (Node) pathExpression.evaluate(aDoc, XPathConstants.NODE);
return node.getTextContent();
}

/** 解析默认ColumnFamily
* @param aDoc 文档对象
* @return String 默认ColumnFamily
*/
private String readDefaultColumnFamily(XPath aXpath, Document aDoc)
throws XPathExpressionException
{
XPathExpression pathExpression =
aXpath.compile("//defaultColumnFamily");
Node node = (Node) pathExpression.evaluate(aDoc, XPathConstants.NODE);
return node.getTextContent();
}

/** 解析DataBindings.
* @param aDoc 文档对象
*/
private Map<String, String> readDataBindingMap(XPath aXpath, Document aDoc)
throws XPathExpressionException
{
Map<String, String> dataBindingMap = new HashMap<String, String>();
XPathExpression pathExpression =
aXpath.compile("//dataBindings/property");
NodeList nodeList = (NodeList) pathExpression.evaluate(aDoc,
XPathConstants.NODESET);
String name = null;
String value = null;
for (int i = 0; i < nodeList.getLength(); i++)
{
name = nodeList.item(i)
.getAttributes()
.getNamedItem("name")
.getNodeValue();
value = nodeList.item(i)
.getAttributes()
.getNamedItem("value")
.getNodeValue();
dataBindingMap.put(name, value);
}
return dataBindingMap;
}
}
package cn.com.chinatelecom.billing.gth.gather.job.config; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; /** * 配置加载器 - 支持JSON和YAML格式 */ @Slf4j public class ConfigLoader { private static final ObjectMapper jsonMapper = new ObjectMapper(); private static final ObjectMapper yamlMapper = new ObjectMapper(); /** * 从文件加载配置 */ public static StreamConfig loadFromFile(String filePath) { try { File configFile = new File(filePath); if (!configFile.exists()) { log.warn("Config file not found: {}", filePath); return createDefaultConfig(); } String content = FileUtils.readFileToString(configFile, StandardCharsets.UTF_8); // 根据文件扩展名选择解析器 if (filePath.endsWith(".yaml") || filePath.endsWith(".yml")) { return yamlMapper.readValue(content, StreamConfig.class); } else { return jsonMapper.readValue(content, StreamConfig.class); } } catch (IOException e) { log.error("Failed to load config from file: {}", filePath, e); return createDefaultConfig(); } } /** * 从资源文件加载配置 */ public static StreamConfig loadFromResource(String resourcePath) { try { ClassLoader classLoader = ConfigLoader.class.getClassLoader(); // 根据文件扩展名选择解析器 if (resourcePath.endsWith(".yaml") || resourcePath.endsWith(".yml")) { return yamlMapper.readValue(classLoader.getResourceAsStream(resourcePath), StreamConfig.class); } else { return jsonMapper.readValue(classLoader.getResourceAsStream(resourcePath), StreamConfig.class); } } catch (IOException e) { log.error("Failed to load config from resource: {}", resourcePath, e); return createDefaultConfig(); } } /** * 从字符串加载配置 */ public static StreamConfig loadFromString(String configContent, String format) { try { if ("yaml".equalsIgnoreCase(format) || "yml".equalsIgnoreCase(format)) { return yamlMapper.readValue(configContent, StreamConfig.class); } else { return jsonMapper.readValue(configContent, StreamConfig.class); } } catch (IOException e) { log.error("Failed to load config from string", e); return createDefaultConfig(); } } /** * 创建默认配置 */ private static StreamConfig createDefaultConfig() { log.info("Creating default stream processing configuration"); StreamConfig config = new StreamConfig(); // 默认输入配置 InputConfig inputConfig = new InputConfig(); inputConfig.setType("KAFKA"); KafkaConfig kafkaConfig = new KafkaConfig(); kafkaConfig.setBootstrapServers("localhost:9092"); kafkaConfig.setTopic("stream-events"); kafkaConfig.setGroupId("flink-stream-processor"); inputConfig.setKafka(kafkaConfig); config.setInputConfig(inputConfig); // 默认输出配置 OutputConfig outputConfig = new OutputConfig(); outputConfig.setType("FILE"); FileOutputConfig fileOutputConfig = new FileOutputConfig(); fileOutputConfig.setBasePath("/data/output"); fileOutputConfig.setFormat("json"); fileOutputConfig.setEncoding("UTF-8"); fileOutputConfig.setRolloverInterval(600L); outputConfig.setFile(fileOutputConfig); config.setOutputConfig(outputConfig); // 默认缓存配置 CacheConfig cacheConfig = new CacheConfig(); cacheConfig.setType("LOCAL"); config.setCacheConfig(cacheConfig); // 默认并行度配置 ParallelismConfig parallelismConfig = new ParallelismConfig(); parallelismConfig.setDefaultParallelism(1); config.setParallelismConfig(parallelismConfig); // 默认检查点配置 CheckpointConfig checkpointConfig = new CheckpointConfig(); checkpointConfig.setInterval(60000L); checkpointConfig.setTimeout(600000L); checkpointConfig.setMinPauseBetweenCheckpoints(500); checkpointConfig.setMaxConcurrentCheckpoints(1); config.setCheckpointConfig(checkpointConfig); return config; } /** * 验证配置 */ public static boolean validateConfig(StreamConfig config) { if (config == null) { log.error("Config is null"); return false; } if (config.getInputConfig() == null) { log.error("Input config is required"); return false; } if (config.getOutputConfig() == null) { log.error("Output config is required"); return false; } // 验证输入配置 InputConfig inputConfig = config.getInputConfig(); if (inputConfig.getType() == null) { log.error("Input type is required"); return false; } // 验证输出配置 OutputConfig outputConfig = config.getOutputConfig(); if (outputConfig.getType() == null) { log.error("Output type is required"); return false; } log.info("Config validation passed"); return true; } } 仔细阅读代码,完善配置文件加载功能,要求可以解析yaml文件
最新发布
12-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值