簡體   English   中英

在CSV文件中顯示前10個字

[英]Display top 10 words in a CSV file

我有已存儲在Google雲存儲中的CSV文件。 我正在閱讀此CSV文件,並為每次出現的單詞構建樹形圖。 我可以通過這種方式對文件中的前10個單詞進行排序和顯示嗎?

這是我的代碼:

@SuppressWarnings("serial")
public class GoogleCloudStorageServlet extends HttpServlet {
   public static final String BUCKETNAME = "bigdata";
   public static final String FILENAME = "Railways.csv";

@SuppressWarnings({ "unchecked", "rawtypes" })

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    resp.getWriter().println("Hello, world from java");
    GcsService gcsService = GcsServiceFactory.createGcsService();
    GcsFilename filename = new GcsFilename(BUCKETNAME, FILENAME);
    GcsFileOptions options = new GcsFileOptions.Builder()
        .mimeType("text/html")
        .acl("public-read")
        .addUserMetadata("myfield1", "my field value")
        .build();

    GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, options);

    PrintWriter writer = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));

    GcsInputChannel readChannel = null;
    BufferedReader reader = null;

    String cvsSplitBy = ",";
    try {
      readChannel = gcsService.openReadChannel(filename, 0);
      reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
      String line;
      TreeMap<String, Integer> map = new TreeMap<String, Integer>();
        while ((line = reader.readLine()) != null) {

          String[] post = line.split(cvsSplitBy);

          String[] words = post[1].split("[ \n\t\r.,;:!?(){}]");

          for (int counter = 0; counter < words.length; counter++) {
              String key = words[counter].toLowerCase(); // remove .toLowerCase for Case Sensitive result.
              if (key.length() > 0) {
                  if (map.get(key) == null) {
                      map.put(key, 1);
                  }
                  else {
                      int value = map.get(key).intValue();
                      value++;
                      map.put(key, value);
                  }
              }
           }

          //Display only top 10 words in the file
    }


    } finally {
      if (reader != null) { reader.close(); }
    }
  }
}

計算CSV文件中前10個字的方式取決於文件的大小。

  1. 小文件(可以存儲在內存中)

    如果文件很小,則可以使用針對您的情況優化的某種集合(例如Bill Lin提到的Multiset ),也可以自己執行計算。

     Map<String, Integer> counts = new HashMap<String, Integer>(); for (String word : words) { Integer count = counts.get(word); if (count == null) { counts.put(word, 1); } else { counts.put(word, count + 1); } } 

    如果文件很小,則可以在單個請求的范圍內處理這種計算。

  2. 中/大型文件

    對於中型或大型文件,您可能會超出請求限制(60s),並且可能會耗盡可用內存。 這也不是很有效。 您需要一種不同的方法。

    嘗試查看App Engine的MapReduce

    MapReduce是用於以並行和分布式方式處理大量數據的編程模型。 這對於無法在單個請求范圍內處理的大型長時間運行的作業很有用。

顯示結果的方式取決於您的處理模式。

  1. 同步

    如果您的計算是同步的,則可以通過傳遞的響應對象直接從Servlet顯示結果。

     HttpServletResponse#getWriter() 
  2. 異步

    如果是異步計算,則需要將結果存儲在某個地方(例如,數據存儲區)並按需顯示它們

我建議您使用Multiset和Multisets.copyHighestCountFirst

https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM