use of com.opencsv.bean.CsvToBeanBuilder in project cassandre-trading-bot by cassandre-tech.
the class StrategiesAutoConfiguration method loadImportedTickers.
/**
* Load imported tickers into database.
*/
private void loadImportedTickers() {
// Deleting everything before import.
importedTickersRepository.deleteAllInBatch();
// Getting the list of files to import and insert them in database.
logger.info("Importing tickers...");
AtomicLong counter = new AtomicLong(0);
getFilesToLoad().parallelStream().filter(resource -> resource.getFilename() != null).peek(resource -> logger.info("Importing file {}.", resource.getFilename())).forEach(resource -> {
try {
// Insert the tickers in database.
new CsvToBeanBuilder<ImportedTicker>(Files.newBufferedReader(resource.getFile().toPath())).withType(ImportedTicker.class).withIgnoreLeadingWhiteSpace(true).build().parse().forEach(importedTicker -> {
logger.debug("Importing ticker {}.", importedTicker);
importedTicker.setId(counter.incrementAndGet());
importedTickersRepository.save(importedTicker);
});
} catch (IOException e) {
logger.error("Impossible to load imported tickers: {}.", e.getMessage());
}
});
logger.info("{} tickers imported.", importedTickersRepository.count());
}
use of com.opencsv.bean.CsvToBeanBuilder in project Java-Tutorial by gpcodervn.
the class ParseCsvToBean method parseCSVtoBean.
public static void parseCSVtoBean(String filename) throws FileNotFoundException {
ColumnPositionMappingStrategy<Country> strategy = new ColumnPositionMappingStrategy<Country>();
strategy.setType(Country.class);
String[] columns = new String[] { "id", "code", "name" };
strategy.setColumnMapping(columns);
CsvToBean<Country> csvToBean = new CsvToBeanBuilder<Country>(new FileReader(filename)).withMappingStrategy(strategy).withSeparator(DEFAULT_SEPARATOR).withQuoteChar(DEFAULT_QUOTE).withSkipLines(NUM_OF_LINE_SKIP).withIgnoreLeadingWhiteSpace(true).build();
List<Country> countries = csvToBean.parse();
for (Country country : countries) {
System.out.println(country);
}
}
use of com.opencsv.bean.CsvToBeanBuilder in project di-authentication-api by alphagov.
the class DataMigrationHandler method handleRequest.
@Override
public String handleRequest(S3Event input, Context context) {
for (S3EventNotification.S3EventNotificationRecord record : input.getRecords()) {
String s3Key = record.getS3().getObject().getKey();
String s3Bucket = record.getS3().getBucket().getName();
LOG.info("New data transfer file {} detected", s3Key);
S3Object object = client.getObject(s3Bucket, s3Key);
InputStreamReader reader = new InputStreamReader(object.getObjectContent());
CsvToBean<ImportRecord> importRecords = new CsvToBeanBuilder<ImportRecord>(reader).withType(ImportRecord.class).build();
var records = importRecords.parse();
int skip = 0;
int count;
do {
var importBatch = records.stream().skip(skip).limit(BATCH_SIZE).collect(Collectors.toList());
count = importBatch.size();
skip = skip + count;
LOG.info("Read {} records starting at {}", count, skip);
var batch = buildImportBatch(importBatch, configurationService.getTermsAndConditionsVersion());
authenticationService.bulkAdd(batch.stream().map(p -> p.getLeft()).collect(Collectors.toList()), batch.stream().map(p -> p.getRight()).collect(Collectors.toList()));
} while (count == BATCH_SIZE);
LOG.info("Imported {} records", skip);
}
return "Complete";
}
use of com.opencsv.bean.CsvToBeanBuilder in project cassandre-trading-bot by cassandre-tech.
the class TickerFluxMock method marketService.
@Bean
@Primary
public MarketService marketService() {
// Removes everything from table.
backtestingCandleRepository.deleteAllInBatch();
// Creates the mock.
marketServiceBacktestingImplementation = new MarketServiceBacktestingImplementation(orderFlux, tradeFlux, orderRepository, tradeRepository, backtestingCandleRepository);
// Getting the list of files to import and insert them in database.
logger.info("Importing candles for backtesting...");
Set<CurrencyPairDTO> currencyPairUsed = new HashSet<>();
getCandlesFilesToLoad().stream().filter(resource -> resource.getFilename() != null).peek(resource -> logger.info("Importing {}...", resource.getFilename())).forEach(resource -> {
try {
// Insert the tickers in database.
AtomicLong sequence = new AtomicLong(1);
new CsvToBeanBuilder<ImportedCandle>(Files.newBufferedReader(resource.getFile().toPath())).withType(ImportedCandle.class).withIgnoreLeadingWhiteSpace(true).build().parse().forEach(importedCandle -> {
logger.debug("Importing candle {}", importedCandle);
BacktestingCandle candle = backtestingTickerMapper.mapToBacktestingCandle(importedCandle);
// Specific fields in Backtesting candle.
BacktestingCandleId id = new BacktestingCandleId();
id.setTestSessionId(marketServiceBacktestingImplementation.getTestSessionId());
id.setResponseSequenceId(sequence.getAndIncrement());
id.setCurrencyPair(importedCandle.getCurrencyPair());
candle.setId(id);
// Save in database.
backtestingCandleRepository.save(candle);
// We build a list of currency pairs listed in files.
currencyPairUsed.add(id.getCurrencyPairDTO());
});
} catch (IOException e) {
logger.error("Impossible to load candles for backtesting: {}", e.getMessage());
}
});
// Setting the flux size of each currency pair.
currencyPairUsed.forEach(currencyPairDTO -> marketServiceBacktestingImplementation.getFluxSize().put(currencyPairDTO, backtestingCandleRepository.findByIdCurrencyPair(currencyPairDTO.toString()).size()));
return marketServiceBacktestingImplementation;
}
use of com.opencsv.bean.CsvToBeanBuilder in project cassandre-trading-bot by cassandre-tech.
the class StrategiesAutoConfiguration method loadCandlesFromFiles.
/**
* Load candles in database.
*/
private void loadCandlesFromFiles() {
// Deleting everything before import.
importedCandleRepository.deleteAllInBatch();
// Getting the list of files to import and insert them in database.
logger.info("Importing candles...");
AtomicLong counter = new AtomicLong(0);
getFilesToLoad("classpath*:candles-to-import*csv").stream().filter(resource -> resource.getFilename() != null).peek(resource -> logger.info("Importing candles from {}", resource.getFilename())).forEach(resource -> {
try {
// Insert the tickers in database.
new CsvToBeanBuilder<ImportedCandle>(Files.newBufferedReader(resource.getFile().toPath())).withType(ImportedCandle.class).withIgnoreLeadingWhiteSpace(true).build().parse().forEach(importedCandle -> {
logger.debug("Importing candle {}", importedCandle);
importedCandle.setUid(counter.incrementAndGet());
importedCandleRepository.save(importedCandle);
});
} catch (IOException e) {
logger.error("Impossible to load imported candles: {}", e.getMessage());
}
});
logger.info("{} candles imported", importedCandleRepository.count());
}
Aggregations