java—如何在mapreduce作业中查找最后一行

xwbd5t1u  于 2021-05-30  发布在  Hadoop
关注(0)|答案(0)|浏览(176)

我正在处理一个 .csv 文件。我只想打印我的第一行和最后一行。我已经用普通的java代码完成了这一点,但是,我没有找到任何 Inputformat 它将读取行并找到第一行和最后一行。我的java代码如下。

  1. public class CSVToJSON {
  2. public static void main(String[] args) throws ParseException, ClassNotFoundException, FileNotFoundException {
  3. File file = new File("/../Desktop/ABCD.txt");
  4. try {
  5. BufferedReader csvFile = new BufferedReader(new FileReader("/../Desktop/.csv"));
  6. BufferedWriter jsonFile = new BufferedWriter(new FileWriter(file));
  7. InputStream inputStream = new ByteArrayInputStream(jsonNew.getBytes(StandardCharsets.UTF_8));
  8. jsonFile.write(jsonNew);
  9. jsonFile.newLine();
  10. String next, fileContent = csvFile.readLine();
  11. for (boolean first = true, last = (fileContent == null); !last; first = false, fileContent = next)
  12. {
  13. last = ((next = csvFile.readLine()) == null);
  14. if (first)
  15. {
  16. String[] tab = fileContent.split(",");
  17. String line = "[" + tab[2] + "," + tab[3] + "," + tab[8] + "," + tab[11] + "," + tab[15] + "," + tab[16] + "],";
  18. InputStream inputStreamNew = new ByteArrayInputStream(line.getBytes(StandardCharsets.UTF_8));
  19. jsonFile.write(line);
  20. jsonFile.newLine();
  21. }else if (last) {
  22. String[] tab = fileContent.split(",");
  23. String lineLast = "[" + tab[2] + "," + tab[3] + "," + tab[8] + "," + tab[11] + "," + tab[15] + "," + tab[16] + "]" + "\n" + "];
  24. InputStream inputStreamNewLine = new ByteArrayInputStream(lineLast.getBytes(StandardCharsets.UTF_8));
  25. jsonFile.write(lineLast);
  26. jsonFile.newLine();
  27. }else {
  28. String[] tab = fileContent.split(",");
  29. String line = "[" + tab[2] + "," + tab[3] + "," + tab[8] + "," + tab[11] + "," + tab[15] + "," + tab[16] + "],";
  30. inputStream inputStreamNormalLine = new ByteArrayInputStream(line.getBytes(StandardCharsets.UTF_8));
  31. jsonFile.write(line);
  32. jsonFile.newLine();
  33. }
  34. }
  35. csvFile.close();
  36. jsonFile.close();
  37. } catch (FileNotFoundException e) {
  38. Logger.getLogger(CSVToJSON.class.getName()).log(Level.SEVERE, null, e);
  39. } catch (IOException e) {
  40. Logger.getLogger(CSVToJSON.class.getName()).log(Level.SEVERE, null, e);
  41. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题