写一个用Ehcache读取2G的CSV文件并缓存到临时文件中,最后以List的形式传出的demo
时间: 2024-05-06 22:18:48 浏览: 118
以下是一个简单的Java程序,将2G的CSV文件读入Ehcache缓存,然后将缓存数据转换为List传出。
首先,你需要在项目中添加Ehcache库的依赖项。可以在Maven中添加以下依赖项:
```xml
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.9.0</version>
</dependency>
```
然后,你需要编写一个CSV文件读取器,将数据读入Ehcache缓存中:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.core.spi.service.StatisticsService;
import org.ehcache.impl.internal.statistics.DefaultStatisticsService;
public class CsvReader {
public static void main(String[] args) {
String filePath = "path/to/csv/file.csv";
List<String[]> dataList = readCsvFile(filePath);
List<List<String>> list = getDataList(dataList);
System.out.println(list);
}
private static List<String[]> readCsvFile(String filePath) {
String line;
List<String[]> dataList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
dataList.add(values);
}
} catch (IOException e) {
e.printStackTrace();
}
return dataList;
}
private static List<List<String>> getDataList(List<String[]> dataList) {
CacheManager cacheManager = null;
Cache<String, List<String>> cache = null;
try {
StatisticsService statisticsService = new DefaultStatisticsService();
cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.using(statisticsService)
.withCache("csvCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, List.class,
ResourcePoolsBuilder.heap(2000000L))
.build())
.build(true);
cache = cacheManager.getCache("csvCache", String.class, List.class);
for (String[] values : dataList) {
List<String> list = new ArrayList<>();
for (String value : values) {
list.add(value);
}
cache.put(values[0], list);
}
return new ArrayList<>(cache.values());
} finally {
if (cacheManager != null) {
cacheManager.close();
}
}
}
}
```
这个CSV读取器将数据读入Ehcache中,并使用put方法将每行数据作为列表存储。最后,它使用values方法获取缓存中的所有数据,并将其转换为List。
请注意,我们使用了一个名为"csvCache"的缓存,容量为2000000L。你可以根据需要更改这些值。
希望这可以帮助你!
阅读全文