-2

I have a huge CSV file which I have read each row and compared against a database table using hash of each row, now I would want to extract certain rows depending on id value from the csv file again. I cannot save the rows initially as I have around 20 columns and and > 500k rows. Thus would there exist a lookup function by column value?

 id price year comments
 1   23.5  2014  XYZ
 2   25    2016  XYZ
 3   22.5  2014  XYZ
user3483129
  • 137
  • 6
  • 18
  • 2
    Your question makes no sense. when you read a csv file with opencsv, you get a row at a time, just save the rows you want. – DwB Feb 24 '16 at 22:21

2 Answers2

0
  1. Library used: OpenCSV
  2. You can use following method to get the value from specific columns
  3. csvData: has all the data from csv using readAll() from OpenCSV lib;
  4. colName: for which column you want to extract the data.

private void getCsvColumnData(String colName) {

    int counter = 0;
    String[] col = csvData.get(0);
    for (String c1 : col) {
        if (c1.equalsIgnoreCase(colName)) {
            break;
        }
        counter++;
    }
    System.out.println("size:- " + counter);

    for (int i = 1; i < csvData.size(); i++) {
        String[] label = csvData.get(i);
        for (int j = 0; j < label.length; j++) {
            if (j == counter) {
                System.out.println(label[j]);
            }

        }
    }

}
-1

Use the CsvToBeanFilter. In the opencsv api there is an excellent example that you can use to create your own filter.

Scott Conway
  • 975
  • 7
  • 13
  • I have processed the csv file, I would now again want to index into the csv file and extract row values for column id and put inside map. – user3483129 Feb 24 '16 at 22:00
  • Okay - I am starting to lean toward DwB's point of view. Your question is a little vague to say the least. Originally I thought you only wanted to read lines with certain ID's - which the filter will do that for you. But it seems you want to do it yourself. So if for some reason you do not know what id's you want until after you read the file then as you are reading the file put the results in a map as you read them. Please send a more detailed explanation of what you want to do and why - it will help us determine a solution. – Scott Conway Feb 24 '16 at 22:26