我有一个包含44列的CSV,并且有一个包含21列的表。需要检查CSV中的列以及与数据库表中的列和数据匹配的数据
我创建了2个类(其中一个主要类),在该类中我已读取CSV文件并将值存储在“键-值”对中。 我在另一个类中执行选择查询以获取结果。 现在,我陷入了如何根据列及其数据比较这两件事的问题
预期:csv中的列数以及数据应与数据库表中存在的列匹配
// MAIN CLASS-在其中CSV读取并存储在键=值对中
public static void main(String[] args) throws JSchException {
CassandraConnection c=new CassandraConnection();
Session session = null;
try {
session = jsch.getSession(user, host, port);
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Session to connect with FTP//
InputStream stream = null;
try {
stream = sftpChannel.get("file path");
System.out.println("File read");
} catch (SftpException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
BufferedReader br = new BufferedReader(new
InputStreamReader(stream));
Iterable<CSVRecord> records =
CSVFormat.DEFAULT.withFirstRecordAsHeader()
.withIgnoreEmptyLines(true).withDelimiter(',').withTrim()
.parse(br);
System.out.println("Printing records having COLUMN 4 > 0");
java.util.List<Map<String, String>> finResult =
StreamSupport.stream(records.spliterator(),
false).filter(csvRecord ->
Integer.parseInt(csvRecord.get("COLUMN 4"))>0).map(csvRecord
->
csvRecord.toMap().entrySet().stream().collect(Collectors.toMap(e
-> e.getKey(),e -> e.getValue()))).collect(Collectors.toList());
size = finResult.size();
System.out.println(size);
for (Map<String, String> map : finResult) {
System.out.println(finResult);
}
br.close();
}
catch (IOException io) {
System.out.println("Exception occurred during reading file from SFTP
server due to " + io.getMessage());
io.getMessage();
} catch (Exception e) {
System.out.println("Exception occurred during reading file from SFTP
server due to " + e.getMessage());
e.getMessage(); }}}