读取 csv 文件以根据 java 中的访问方式统计子用户数

Reading a csv file to count the number of sub users based on the access method in java

提问人:Runtime21 提问时间:10/12/2023 最后编辑:EritreanRuntime21 更新时间:10/13/2023 访问量:48

问:

我目前正在读取包含以下代码的 csv 文件:

  • 这只是读取文件代码。
  • 我已经读取了文件,并且正在获取文件数据作为输出。

我想要实现的目标:** 请点击链接查看样本数据。 ** [示例数据]:(https://i.stack.imgur.com/iXMAo.png

package testing;
import java.io.*;
`public class GarExcel` {

    public static final String delimiter = ",";
    public static void read(String csvFile) {

        try {
            File file = new File(csvFile);
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line = "";
            String[] tempArr;
            while ((line = br.readLine()) != null) {
                // use as comma seperator
                tempArr = line.split(delimiter);
                for (String tempStr: tempArr) {

                    System.out.print(tempStr + " ");
                }
                System.out.println();
            }
            br.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    public static void main(String[] args) {

           String csvFile = "C:/Users/John.Cena/Documents/ExcelTesting/gar-20230901.csv";
           GarExcel.read(csvFile);
    }
}

我想根据访问方式计算子用户的数量。

因此,例如,如果您引用图像/图像链接,我希望输出具有子用户 JL 的计数,并且 TW2JL 的计数相同。因此,根据图像,我希望输出为:

Count of JL - 11
Count of TW2JL - 15.

现在的问题是csv文件超过2GB,并且有超过200万条记录。它将拥有超过 200 个子用户。我想根据访问方法计算所有子用户的数量。 我在上面也附上了一张截图,上面有一个标签样本数据,可以帮助你们理解我的问题。 我是 java 的新手,感谢您的建议。

Java 数组 文件

评论

0赞 roediGERhard 10/12/2023
我建议使用基本的 *nix shell 程序或自定义 shellscript 来完成该任务。我认为这将比在 java 中这样做要快得多。也许这样做会有所帮助:stackoverflow.com/questions/64085303/......

答:

0赞 Eritrean 10/13/2023 #1

假设您使用的是 Java 8 或更高版本,请使用将每行映射到子用户列、按子用户 using 和计数收集器分组,将文件内容读入流中。然后,结果映射应将子组的所需内容映射到其频率。例:Files.linesFunction.identity()

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Example {

    public static final String delimiter = ",";
    public static void main (String[] args) throws IOException {

        String csvFile = "/path/to/your/csv/test.csv";

        Stream<String> fileContent = readFile(csvFile);
        Map<String, Long> subUserCount = groupBySubUser(fileContent);

        subUserCount.forEach((k,v) -> System.out.println(k + " : " + v));
    }

    private static Stream<String> readFile(String csvFile) throws IOException {
        return Files.lines(Path.of(csvFile));
    }

    private static Map<String, Long> groupBySubUser(final Stream<String> fileContent) {
        return fileContent.skip(1) //skip header
                          .map(line -> line.split(delimiter)[2]) //map each line to 3rd column
                          .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
    }
}

评论

0赞 Sagar 11/16/2023
为什么我们需要'LinkedHashmap',我们只需要计数