1、前言
最近接到一个需求,定时获取数据并把数据写入ElasticSearch。
假如每次使用定时启动的时间,每次执行获取一段时间数据,假如每次使用启动成功的时间,会导致数据丢失。
这个时候就考虑对开始时间进行持久化操作。
然后就想到了Properties 对象对数据进行存储。
2、使用方式
2.1、静态读写
这里使用了静态方法对对象进行读写。
/**
* 按照key读取 Properties
* @param tempPath 文件存储目录
* @param fileName 文件名称
* @param key 键
* @return
*/
public static String readProperties(String tempPath,String fileName,String key){
Properties properties = new Properties();
try (FileInputStream inputStream = new FileInputStream(tempPath + fileName)){
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return properties.getProperty(key);
}
/**
* 写入 Properties
* @param tempPath 文件存储目录
* @param fileName 文件名称
* @param key 键
* @param value 值
*/
public static void writeProperties(String tempPath,String fileName,String key,String value){
Properties properties = new Properties();
try (FileOutputStream outputStream = new FileOutputStream(tempPath + fileName)){
properties.setProperty(key,value);
properties.store(outputStream,"some http request info");
} catch (IOException e) {
e.printStackTrace();
}
}
这里使用了try-with-resource 写法,所以不用关闭流。
对于异常信息,这里没有特殊处理,如果想在项目中使用,可以处理下,例如添加 slf4j 打印等。
2.2、写入修改
当我们业务代码中使用的时候,不可能每次写入都重新覆盖,所以必须先读取文本中所有的对象,然后对Properties进行写入操作。
示例:
public static void main(String[] args) {
String tempPath = "D:\\";
String fileName = "test.properties";
String key = "test";
String key1 = "test1";
String value = "test1231";
// 获取
Map<String, String> map = new HashMap<>();
map.put(key, value);
map.put(key1, value);
Properties properties = new Properties();
try (FileInputStream inputStream = new FileInputStream(tempPath + fileName)) {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
try (FileOutputStream outputStream = new FileOutputStream(tempPath + fileName)) {
properties.putAll(map);
properties.store(outputStream, "some http request info");
} catch (IOException e) {
e.printStackTrace();
}
}
首次写入的时候会把测试的key-value进行写入,稍后修改key值,就会发现,之前的写入内容可以读取,之后进行操作后写入。
2.3、 批量处理
由于Properties 继承 Hashtable<Object,Object>,而Hashtable 是 实现Map<K,V>,则可以与操作Map相同。
获取所有数据可以使用:
List<String> values = new ArrayList<>();
Set<Object> objects = properties.keySet();
for (Object object : objects) {
String value = properties.getProperty(String.valueOf(object));
values.add(value);
}
List<String> values = new ArrayList<>();
Enumeration<Object> keys = properties.keys();
for (Object object : objects) {
String value = properties.getProperty(String.valueOf(object));
values.add(value);
}
Collection<Object> collect = properties.values();
List<String> values = collect.stream().map(String::valueOf).collect(Collectors.toList())
当然获取部分数据也可以,这个对key 进行对比就可以。
写入的时候 :
Map<String, String> map = new HashMap<>();
properties.putAll(map);
- 其他
当然使用的使用也可以转换成HashMap进行操作等:
Properties properties = new Properties();
HashMap<Object, Object> hashMap = new HashMap<>(properties);
3、总结
对于我本身的业务来讲,其实是找一个地方进行存储,这也是注册中心,数据库等相同的原理。
拿数据库来讲,写入一个非程序本身的地方,下次直接从存储的地方获取。
注册中心是微服务进行注册,其他微服务不用直接调用本服务,达到解耦的目的。
存储文件也是这种思路,写入一个非程序本身的地方,下次直接获取,重启程序不影响已写的数据。