Compare columns in CSV with columns in database table in JAVA

纵饮孤独 提交于 2019-12-11 07:36:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!