问题
I have a CSV with 44 columns and I have a table with 21 columns. Need to check of the columns in CSV along with the data matches with columns and data in database table
I have created 2 class (one with main) where I have read the CSV file and stored the values in Key - Value pair. I have another class where I am executing select query to get the result. Now, got stuck with how should I be comparing these 2 things based on columns and its data
Expected: No of columns in csv along with data should match with those present in the database table
//MAIN CLASS -- where CSV is read and stored in key = value pair
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(); }}}
来源:https://stackoverflow.com/questions/56330004/compare-columns-in-csv-with-columns-in-database-table-in-java