简体   繁体   English

Java根据csv文件中的列数据提取计数

[英]Java Extract count based on column data from csv file

I have my below Java code and TestData.csv (Input file) and my expected output is like below.我有下面的 Java 代码和 TestData.csv(输入文件),我的预期输出如下所示。 But it show actual count I tried lot.但它显示了我尝试过的实际数量。 Any one hava any idea on this.任何人对此都有任何想法。 Any help is valuable one.任何帮助都是有价值的。 Based on column data I want the count for the particular value.基于列数据,我想要特定值的计数。

在此处输入图片说明 在此处输入图片说明

package com;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import com.opencsv.CSVWriter;
import com.opencsv.CSVReader;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;

public class TestDataProcess {
    public static void main(String args[]) throws IOException {
        processData();
    }

    public static void processData() {
        String[] trafficDetails;
        int locColumnPosition, subCcolumnPosition, j, i, msgTypePosition, k, m, trafficLevelPosition;
        String masterCSVFile, dayFolderPath;
        String[] countryID = { "LOC1" };
        String[] subID = { "S1" };
        String[] mType = { "MSG1" };
        String[] trafficLevel = { "1", "2", "3" };
        String columnNameLocation = "CountryID";
        String columnNameSubsystem = "SubID";
        String columnNameMsgType = "Type";
        String columnNameAlrmLevel = "TrafficLevel";
        masterCSVFile = "D:\\TestData.csv";
        dayFolderPath = "D:\\output\\";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd_MM_yyyy");
        LocalDateTime now = LocalDateTime.now();
        System.out.println(dtf.format(now));
        int count = 0;
        for (i = 0; i < countryID.length; i++) {
            count = 0;
            for (j = 0; j < subID.length; j++) {
                count = 0;
                String locaIdSubsysId = dtf.format(now) + "_" + countryID[i] + "_" + subID[j] + ".csv";
                try (CSVWriter csvWriter = new CSVWriter(new FileWriter(dayFolderPath + locaIdSubsysId, true));
                        CSVReader csvReader = new CSVReader(new FileReader(masterCSVFile));) {
                    trafficDetails = csvReader.readNext();
                    csvWriter.writeNext(trafficDetails);
                    locColumnPosition = getHeaderLocation(trafficDetails, columnNameLocation);
                    subCcolumnPosition = getHeaderLocation(trafficDetails, columnNameSubsystem);
                    msgTypePosition = getHeaderLocation(trafficDetails, columnNameMsgType);
                    trafficLevelPosition = getHeaderLocation(trafficDetails, columnNameAlrmLevel);
                    while ((trafficDetails = csvReader.readNext()) != null && locColumnPosition > -1
                            && subCcolumnPosition > -1) {
                        for (k = 0; k < mType.length; k++) {
                            for (m = 0; m < trafficLevel.length; m++) {
                                if (trafficDetails[locColumnPosition].matches(countryID[i])
                                        & trafficDetails[subCcolumnPosition].matches(subID[j])
                                        & trafficDetails[trafficLevelPosition].matches(trafficLevel[m])
                                        & trafficDetails[msgTypePosition].matches(mType[k]))

                                {
                                    count = count + 1;
                                    csvWriter.writeNext(trafficDetails);

                                }
                            }
                        }
                    }
                } catch (Exception ee) {

                    ee.printStackTrace();
                }
            }
        }
    }

    public static int getHeaderLocation(String[] headers, String columnName) {
        return Arrays.asList(headers).indexOf(columnName);
    }
}

You can have that using a Map to store the traffic level as a key and all the rows from your csv file in a List as its value.您可以使用Map将流量级别存储为键,并将 csv 文件中的所有行存储在List作为其值。 Then just print the size of the List .然后只需打印List的大小。

See the following example and have a look at the code comments:请参阅以下示例并查看代码注释:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class ExampleMain {

public static void main(String[] args) {
    // create a Path object from the path to your file
    Path csvFilePath = Paths.get("Y:\\our\\path\\to\\file.csv");
    // create a data structure that stores data rows per traffic level
    Map<Integer, List<DataRow>> dataRowsPerTrafficLevel = new TreeMap<Integer, List<DataRow>>();

    try {
        // read all the lines of the file
        List<String> lines = Files.readAllLines(csvFilePath);

        // iterate all the lines, skipping the header line
        for (int i = 1; i < lines.size(); i++) {
            // split the lines by the separator (WHICH MAY DIFFER FROM THE ONE USED HERE)
            String[] lineValues = lines.get(i).split(",");
            // store the value from column 6 (index 5) as the traffic level
            int trafficLevel = Integer.valueOf(lineValues[5]);
            // if the map already contains this key, just add the next data row
            if (dataRowsPerTrafficLevel.containsKey(trafficLevel)) {
                DataRow dataRow = new DataRow();
                dataRow.subId = lineValues[1];
                dataRow.countryId = lineValues[2];
                dataRow.type = lineValues[3];
                dataRowsPerTrafficLevel.get(trafficLevel).add(dataRow);
            } else {
                /* otherwise create a list, then a data row, add it to the list and put it in
                 * the map along with the new key
                 */
                List<DataRow> dataRows = new ArrayList<DataRow>();
                DataRow dataRow = new DataRow();
                dataRow.subId = lineValues[1];
                dataRow.countryId = lineValues[2];
                dataRow.type = lineValues[3];
                dataRows.add(dataRow);
                dataRowsPerTrafficLevel.put(trafficLevel, dataRows);
            }
        }

        // print the result
        dataRowsPerTrafficLevel.forEach((trafficLevel, dataRows) -> {
            System.out.println("For TrafficLevel " + trafficLevel + " there are " + dataRows.size()
                    + " data rows in the csv file");
        });

    } catch (IOException e) {
        e.printStackTrace();
    }
}

/*
 * small holder class that just holds the values of columns 3, 4 and 5.
 * If you want to have distinct values, make this one a full POJO implementing Comparable
 */
static class DataRow {
    String subId;
    String countryId;
    String type;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM