Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. public class CsvSearch extends RowListProcessor {
  2. //value to be searched for
  3. private final String stringToMatch;
  4.  
  5. //name of column to match (if you don't have headers)
  6. private final String columnToMatch;
  7.  
  8. //position of column to match
  9. private int indexToMatch = -1;
  10.  
  11. public CsvSearch(String columnToMatch, String stringToMatch){
  12. this.columnToMatch = columnToMatch;
  13. this.stringToMatch = stringToMatch.toLowerCase(); //lower case to make the search case-insensitive
  14. }
  15.  
  16. public CsvSearch(int columnToMatch, String stringToMatch){
  17. this(stringToMatch, null);
  18. this.indexToMatch = columnToMatch;
  19. }
  20.  
  21. @Override
  22. public void rowProcessed(String[] row, ParsingContext context) {
  23. if(indexToMatch == -1) {
  24. //initializes the index to match
  25. indexToMatch = context.indexOf(columnToMatch);
  26. }
  27.  
  28. String value = row[indexToMatch];
  29. if(value != null && value.toLowerCase().contains(stringToMatch)) {
  30. super.rowProcessed(row, context); // default behavior of the RowListProcessor: add the row into a List.
  31. }
  32. // else skip the row.
  33. }
  34. }
  35.  
  36. // let's measure the time roughly
  37. long start = System.currentTimeMillis();
  38.  
  39. CsvParserSettings settings = new CsvParserSettings();
  40. settings.setHeaderExtractionEnabled(true); //extract headers from the first row
  41.  
  42. CsvSearch search = new CsvSearch("City", "Paris");
  43.  
  44. //We instruct the parser to send all rows parsed to your custom RowProcessor.
  45. settings.setProcessor(search);
  46.  
  47. //Finally, we create a parser
  48. CsvParser parser = new CsvParser(settings);
  49.  
  50. //And parse! All rows are sent to your custom RowProcessor (CsvSearch)
  51. //I'm using a 150MB CSV file with 1.3 million rows.
  52. parser.parse(new File("/tmp/data/worldcitiespop.txt"));
  53.  
  54. //get the collected rows from our processor
  55. List<String[]> results = search.getRows();
  56.  
  57. //Nothing else to do. The parser closes the input and does everything for you safely. Let's just get the results:
  58. System.out.println("Rows matched: " + results.size());
  59. System.out.println("Time taken: " + (System.currentTimeMillis() - start) + " ms");
  60.  
  61. Rows matched: 218
  62. Time taken: 997 ms
  63.  
  64. [af, parisang, Parisang, 08, null, 33.180704, 67.470836]
  65. [af, qaryeh-ye bid-e parishan, Qaryeh-ye Bid-e Parishan, 06, null, 33.242727, 63.389834]
  66. [ar, parish, Parish, 01, null, -36.518335, -59.633313]
  67. [at, parisdorf, Parisdorf, 03, null, 48.566667, 15.85]
  68. [au, paris creek, Paris Creek, 05, null, -35.216667, 138.8]
  69. [az, hayi paris, Hayi Paris, 21, null, 40.449626, 46.55542]
  70. [az, hay paris, Hay Paris, 21, null, 40.449626, 46.55542]
  71. [az, rousi paris, Rousi Paris, 21, null, 40.435789, 46.510691]
  72. [az, rrusi paris, Rrusi Paris, 21, null, 40.435789, 46.510691]
  73. [bb, parish land, Parish Land, 01, null, 13.0666667, -59.5166667]
  74. ... (and many more)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement