Java Code Examples for com.csvreader.CsvReader#close()

The following examples show how to use com.csvreader.CsvReader#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RulesDbClientImpl.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Collection<RuleId> listRules(String workflowId, RuleAttributeSet attributeSet) {
    Objects.requireNonNull(workflowId);

    String path = RULES_PREFIX + workflowId + "/data.csv";
    Map<String, String> query = ImmutableMap.of("start", "0",
                                                "count", "-1",
                                                "headers", "true",
                                                "cols", "algoType,contingencyId,indexType");

    try {
        CsvReader csvReader = new CsvReader(httpClient.getHttpRequest(new HistoDbUrl(config, path, query)), ',', StandardCharsets.UTF_8);
        try {
            csvReader.setSafetySwitch(false);
            csvReader.readHeaders();

            List<RuleId> ruleIds = new ArrayList<>();

            while (csvReader.readRecord()) {
                String[] values = csvReader.getValues();
                ruleIds.add(new RuleId(RuleAttributeSet.valueOf(values[0]),
                            new SecurityIndexId(values[1], SecurityIndexType.fromLabel(values[2]))));
            }

            return ruleIds;
        } finally {
            csvReader.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: CSPARQLAarhusTrafficStream.java    From Benchmark with GNU General Public License v3.0 5 votes vote down vote up
public CSPARQLAarhusTrafficStream(String uri, String txtFile, EventDeclaration ed, Date start, Date end)
		throws IOException {
	super(uri);
	logger.info("IRI: " + this.getIRI().split("#")[1] + ed.getInternalQos());
	this.startDate = start;
	this.endDate = end;
	messageCnt = 0;
	byteCnt = 0;
	this.txtFile = txtFile;
	this.ed = ed;
	streamData = new CsvReader(String.valueOf(txtFile));
	streamData.setTrimWhitespace(false);
	streamData.setDelimiter(',');
	streamData.readHeaders();
	metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
	metaData.readHeaders();
	streamData.readRecord();
	while (metaData.readRecord()) {
		if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
			// p1Street = metaData.get("POINT_1_STREET");
			// p1City = metaData.get("POINT_1_CITY");
			// p1Lat = metaData.get("POINT_1_LAT");
			// p1Lon = metaData.get("POINT_1_LNG");
			// p1Country = metaData.get("POINT_2_COUNTRY");
			// p2Street = metaData.get("POINT_2_STREET");
			// p2City = metaData.get("POINT_2_CITY");
			// p2Lat = metaData.get("POINT_2_LAT");
			// p2Lon = metaData.get("POINT_2_LNG");
			// p2Country = metaData.get("POINT_2_COUNTRY");
			distance = metaData.get("DISTANCE_IN_METERS");
			if (ed instanceof TrafficReportService)
				((TrafficReportService) ed).setDistance(Integer.parseInt(distance));

			// timestamp = metaData.get("TIMESTAMP");
			// id = metaData.get("extID");
			metaData.close();
			break;
		}
	}
}
 
Example 3
Source File: CQELSAarhusTrafficStream.java    From Benchmark with GNU General Public License v3.0 5 votes vote down vote up
public CQELSAarhusTrafficStream(ExecContext context, String uri, String txtFile, EventDeclaration ed, Date start,
		Date end) throws IOException {
	super(context, uri);
	this.startDate = start;
	this.endDate = end;
	messageCnt = 0;
	byteCnt = 0;
	this.txtFile = txtFile;
	this.ed = ed;
	streamData = new CsvReader(String.valueOf(txtFile));
	streamData.setTrimWhitespace(false);
	streamData.setDelimiter(',');
	streamData.readHeaders();
	metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
	metaData.readHeaders();
	streamData.readRecord();
	while (metaData.readRecord()) {
		if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
			// p1Street = metaData.get("POINT_1_STREET");
			// p1City = metaData.get("POINT_1_CITY");
			// p1Lat = metaData.get("POINT_1_LAT");
			// p1Lon = metaData.get("POINT_1_LNG");
			// p1Country = metaData.get("POINT_2_COUNTRY");
			// p2Street = metaData.get("POINT_2_STREET");
			// p2City = metaData.get("POINT_2_CITY");
			// p2Lat = metaData.get("POINT_2_LAT");
			// p2Lon = metaData.get("POINT_2_LNG");
			// p2Country = metaData.get("POINT_2_COUNTRY");
			distance = metaData.get("DISTANCE_IN_METERS");
			// timestamp = metaData.get("TIMESTAMP");
			// id = metaData.get("extID");
			metaData.close();
			break;
		}
	}
}
 
Example 4
Source File: ReadDocument.java    From Benchmark with GNU General Public License v3.0 5 votes vote down vote up
public static AarhusTrafficObservation getStreamData(String dataFile) throws NumberFormatException, IOException {
	CsvReader streamData = new CsvReader(String.valueOf(dataFile));
	streamData.readHeaders();
	// Reads csv document for traffic metadata
	try {
		CsvReader metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
		metaData.readHeaders();
		AarhusTrafficObservation data = new AarhusTrafficObservation();
		while (streamData.readRecord()) {

			while (metaData.readRecord()) {
				if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
					data = new AarhusTrafficObservation(Double.parseDouble(streamData.get("REPORT_ID")),
							Double.parseDouble(streamData.get("avgSpeed")), Double.parseDouble(streamData
									.get("vehicleCount")), Double.parseDouble(streamData.get("avgMeasuredTime")),
							0, 0, metaData.get("POINT_1_STREET"), metaData.get("POINT_1_CITY"),
							Double.parseDouble(metaData.get("POINT_1_LAT")), Double.parseDouble(metaData
									.get("POINT_1_LNG")), metaData.get("POINT_2_STREET"),
							metaData.get("POINT_2_CITY"), Double.parseDouble(metaData.get("POINT_2_LAT")),
							Double.parseDouble(metaData.get("POINT_2_LNG")), metaData.get("POINT_1_COUNTRY"),
							metaData.get("POINT_2_COUNTRY"), metaData.get("TIMESTAMP"));
					Double distance = Double.parseDouble(metaData.get("DISTANCE_IN_METERS"));
					data.setEstimatedTime(distance / data.getAverageSpeed());
					data.setCongestionLevel(data.getVehicle_count() / distance);
				}
			}

		}
		streamData.close();
		metaData.close();
		return data;
	} catch (Exception e) {

		e.printStackTrace();
	}
	return null;
}
 
Example 5
Source File: FileSpec.java    From bdt with Apache License 2.0 5 votes vote down vote up
/**
 * Read csv file and store result in list of maps
 *
 * @param csvFile
 */
@When("^I read info from csv file '(.+?)' with separator '(.+?)'$")
public void readFromCSV(String csvFile, String separator) throws Exception {
    //By default separator is a coma
    char sep = ',';
    if (separator.length() > 1) {
        switch (separator) {
            case "\\t":
                sep = '\t';
                break;
            default:
                sep = ',';
                break;
        }
    } else {
        sep = separator.charAt(0);
    }

    CsvReader rows = new CsvReader(csvFile, sep);

    String[] columns = null;
    if (rows.readRecord()) {
        columns = rows.getValues();
        rows.setHeaders(columns);
    }

    List<Map<String, String>> results = new ArrayList<Map<String, String>>();
    while (rows.readRecord()) {
        Map<String, String> row = new HashMap<String, String>();
        for (String column : columns) {
            row.put(column, rows.get(rows.getIndex(column)));
        }
        results.add(row);
    }

    rows.close();

    commonspec.setResultsType("csv");
    commonspec.setCSVResults(results);
}
 
Example 6
Source File: Utils.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static long determineStartSecFromParsedFile(String parsedFileName) throws IOException
{
	CsvReader parsedReader = new CsvReader(parsedFileName);

	parsedReader.setSafetySwitch(false);

	parsedReader.readHeaders();

	parsedReader.readRecord();

	long startSec = Long.parseLong(parsedReader.getValues()[0]);

	parsedReader.close();

	return startSec;
}
 
Example 7
Source File: Utils.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static void generateInputFile(String folderName, long startTimeInSec, long finishTimeInSec, String generatingFile) throws IOException
{
	File folder = new File(folderName);

	if ( folder.exists() )
	{
		File[] files = folder.listFiles();

           Arrays.sort(files, new FileSpecialComparator(true, false));

           if(files.length == 0) {
               return;
           }

           int index = files.length - 1;
		for ( ; index >= 0; --index )
		{
			long startInterval = determineStartInterval(files[index].getName());

               if(startInterval < startTimeInSec * 1000000) {
                   break;
               }
		}

           if(index < 0) {
               index = 0;
           }



		BufferedWriter writer = new BufferedWriter(new FileWriter(generatingFile));

		for ( int i = index; i < files.length; ++i )
		{
			if ( isDebugged )
			{
				logger.debug(files[i].getName());
			}

			CsvReader reader = new CsvReader(folderName + File.separator + files[i].getName());
			reader.setSafetySwitch(false);

			reader.readRecord();

			if ( i == index )
			{
				writer.write(reader.getRawRecord());
				writer.newLine();
			}

			while ( reader.readRecord() )
			{
				long timestamp = Long.parseLong(reader.getValues()[0]);

                   if(timestamp >= startTimeInSec * 1000000 && (timestamp < finishTimeInSec * 1000000 || finishTimeInSec == Long.MAX_VALUE)) {
                       writer.write(reader.getRawRecord());
                       writer.newLine();
                   } else if(timestamp >= startTimeInSec * 1000000) {
                       break;
                   }
               }

			reader.close();
		}

		writer.close();

       } else {
           throw new EPSCommonException("Could not find [" + folderName + "] folder with files");
       }
}
 
Example 8
Source File: Utils.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static void generateMDLatFile(String folderName, long startTimeInSec, long finishTimeInSec, String generatingFile, SimpleDateFormat format, String analyzedField, long startSec) throws IOException
{
	File folder = new File(folderName);

	if ( folder.exists() )
	{
		File[] files = folder.listFiles();

           Arrays.sort(files, new FileSpecialComparator(true, false));

           if(files.length == 0) {
               return;
           }

           int index = files.length - 1;
		for ( ; index >= 0; --index )
		{
			long startInterval = determineStartInterval(files[index].getName());

               if(startInterval < startTimeInSec * 1000000) {
                   break;
               }
		}

           if(index < 0) {
               index = 0;
           }


           try(BufferedWriter writer = new BufferedWriter(new FileWriter(generatingFile))) {
               int fieldIndex = -1;

               for(int i = index; i < files.length; ++i) {
                   if(isDebugged) {
                       logger.debug(files[i].getName());
                   }

                   CsvReader reader = new CsvReader(folderName + File.separator + files[i].getName());
                   reader.setSafetySwitch(false);

                   reader.readRecord();

                   if(i == index) {

                       String[] headers = reader.getValues();

                       for(int j = 0; j < headers.length; ++j) {
                           if(headers[j].equals(analyzedField)) {
                               fieldIndex = j;
                               break;
                           }
                       }

                       writer.write("Timestamp,Latency");

                       for(int j = 3; j < headers.length; ++j) {
                           writer.write("," + headers[j]);
                       }

                       writer.newLine();

                       if(fieldIndex == -1) {
                           throw new IllegalArgumentException("Could not find fieldName = [" + analyzedField + "] in inputFile = [" + files[i].getName() + "]");
                       }
                   }

                   while(reader.readRecord()) {
                       String[] values = reader.getValues();

                       long timestamp = Long.parseLong(values[0]);

                       if(timestamp >= startTimeInSec * 1000000 && (timestamp < finishTimeInSec * 1000000 || finishTimeInSec == Long.MAX_VALUE)) {
                           try {
                               String sendingTime = values[fieldIndex];

                               long sendingTimestamp = format.parse(sendingTime + " +0000").getTime();

                               sendingTimestamp = (sendingTimestamp - startSec * 1000) * 1000;

                               writer.write(values[0] + ',' + (timestamp - sendingTimestamp));

                               for(int j = 3; j < values.length; ++j) {
                                   writer.write("," + values[j]);
                               }

                               writer.newLine();
                           } catch(ParseException e) {
                           }
                       } else if(timestamp >= startTimeInSec * 1000000) {
                           break;
                       }
                   }

                   reader.close();
               }
		}
       } else {
           throw new EPSCommonException("Could not find [" + folderName + "] folder with files");
       }
}
 
Example 9
Source File: Utils.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static void generateFakeInputFile(String folderName, long startTimeInSec, long finishTimeInSec, String generatingFile) throws IOException
{
	File folder = new File(folderName);

	if ( folder.exists() )
	{
		File[] files = folder.listFiles();

           Arrays.sort(files, new FileSpecialComparator(true, false));

           if(files.length == 0) {
               return;
           }

           int index = files.length - 1;
		for ( ; index >= 0; --index )
		{
			long startInterval = determineStartInterval(files[index].getName());

               if(startInterval < startTimeInSec * 1000000) {
                   break;
               }
		}

           if(index < 0) {
               index = 0;
           }

		BufferedWriter writer = new BufferedWriter(new FileWriter(generatingFile));


		boolean firstRecordWritten = false;

		for ( int i = index; i < files.length; ++i )
		{
			if ( isDebugged )
			{
				logger.debug(files[i].getName());
			}

			CsvReader reader = new CsvReader(folderName + File.separator + files[i].getName());
			reader.setSafetySwitch(false);

			reader.readRecord();

			if ( i == index )
			{
				writer.write(reader.getRawRecord());
				writer.newLine();
			}


			if ( !firstRecordWritten || i == files.length - 1)
			{
				String lastRecord = "";

				while ( reader.readRecord() )
				{
					long timestamp = Long.parseLong(reader.getValues()[0]);

					lastRecord = reader.getRawRecord();

					if ( !firstRecordWritten )
					{
                           if(timestamp >= startTimeInSec * 1000000 && (timestamp < finishTimeInSec * 1000000 || finishTimeInSec == Long.MAX_VALUE)) {
                               writer.write(reader.getRawRecord());
                               writer.newLine();
                               firstRecordWritten = true;
                           } else if(timestamp >= startTimeInSec * 1000000) {
                               break;
                           }
                       }
				}

				writer.write(lastRecord);
			}

			reader.close();
		}

		writer.close();

       } else {
           throw new EPSCommonException("Could not find [" + folderName + "] folder with files");
       }
}
 
Example 10
Source File: Utils.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static void mergeFiles(String folderName, String generatingFile) throws IOException
{
	File folder = new File(folderName);

	if ( folder.exists() )
	{
		File[] files = folder.listFiles();

           Arrays.sort(files, new FileComparator(CompType.TIME, "", false));

           if(files.length == 0) {
               return;
           }

		BufferedWriter writer = new BufferedWriter(new FileWriter(generatingFile));

		for ( int i = 0; i < files.length; ++i )
		{
			CsvReader reader = new CsvReader(folderName + File.separator + files[i].getName());
			reader.setSafetySwitch(false);

			reader.readRecord();

			if ( i == 0 )
			{
				writer.write(reader.getRawRecord());
				writer.newLine();
			}

			while ( reader.readRecord() )
			{
				writer.write(reader.getRawRecord());
				writer.newLine();
			}

			reader.close();
		}

		writer.close();

       } else {
           throw new EPSCommonException("Could not find [" + folderName + "] folder with files");
       }
}
 
Example 11
Source File: CSPARQLAarhusTrafficStream.java    From Benchmark with GNU General Public License v3.0 4 votes vote down vote up
public CSPARQLAarhusTrafficStream(String uri, String txtFile, EventDeclaration ed) throws IOException {
	super(uri);
	String fileName = "";
	if (this.getIRI().split("#").length > 1)
		fileName = this.getIRI().split("#")[1];
	else
		fileName = this.getIRI();
	// logFile = new File("resultlog/" + fileName + ".csv");
	// fw = new FileWriter(logFile);
	// cw = new CsvWriter(fw, ',');
	// cw.write("time");
	// cw.write("message_cnt");
	// cw.write("byte_cnt");
	// cw.endRecord();heh
	// cw.flush();
	messageCnt = 0;
	byteCnt = 0;
	this.txtFile = txtFile;
	this.ed = ed;
	// time1 = time.getTime();
	streamData = new CsvReader(String.valueOf(txtFile));
	streamData.setTrimWhitespace(false);
	streamData.setDelimiter(',');
	streamData.readHeaders();
	// streamData.skipRecord();
	metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
	metaData.readHeaders();
	streamData.readRecord();
	while (metaData.readRecord()) {
		if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
			// p1Street = metaData.get("POINT_1_STREET");
			// p1City = metaData.get("POINT_1_CITY");
			// p1Lat = metaData.get("POINT_1_LAT");
			// p1Lon = metaData.get("POINT_1_LNG");
			// p1Country = metaData.get("POINT_2_COUNTRY");
			// p2Street = metaData.get("POINT_2_STREET");
			// p2City = metaData.get("POINT_2_CITY");
			// p2Lat = metaData.get("POINT_2_LAT");
			// p2Lon = metaData.get("POINT_2_LNG");
			// p2Country = metaData.get("POINT_2_COUNTRY");
			distance = metaData.get("DISTANCE_IN_METERS");
			if (ed instanceof TrafficReportService)
				((TrafficReportService) ed).setDistance(Integer.parseInt(distance));

			// timestamp = metaData.get("TIMESTAMP");
			// id = metaData.get("extID");
			// stream(n(RDFFileManager.defaultPrefix + streamData.get("REPORT_ID")),
			// n(RDFFileManager.ctPrefix + "hasETA"), n(data.getEstimatedTime() + ""));
			// System.out.println("metadata: " + metaData.toString());
			metaData.close();
			break;
		}
	}
}
 
Example 12
Source File: CQELSAarhusTrafficStream.java    From Benchmark with GNU General Public License v3.0 4 votes vote down vote up
public CQELSAarhusTrafficStream(ExecContext context, String uri, String txtFile, EventDeclaration ed)
		throws IOException {
	super(context, uri);
	String fileName = "";
	if (this.getURI().split("#").length > 1)
		fileName = this.getURI().split("#")[1];
	else
		fileName = this.getURI();
	// logFile = new File("resultlog/" + fileName + ".csv");
	// fw = new FileWriter(logFile);
	// cw = new CsvWriter(fw, ',');
	// cw.write("time");
	// cw.write("message_cnt");
	// cw.write("byte_cnt");
	// cw.endRecord();
	// cw.flush();
	messageCnt = 0;
	byteCnt = 0;
	this.txtFile = txtFile;
	this.ed = ed;
	// time1 = time.getTime();
	streamData = new CsvReader(String.valueOf(txtFile));
	streamData.setTrimWhitespace(false);
	streamData.setDelimiter(',');
	streamData.readHeaders();
	// streamData.skipRecord();
	metaData = new CsvReader("dataset/MetaData/trafficMetaData.csv");
	metaData.readHeaders();
	streamData.readRecord();
	while (metaData.readRecord()) {
		if (streamData.get("REPORT_ID").equals(metaData.get("REPORT_ID"))) {
			// p1Street = metaData.get("POINT_1_STREET");
			// p1City = metaData.get("POINT_1_CITY");
			// p1Lat = metaData.get("POINT_1_LAT");
			// p1Lon = metaData.get("POINT_1_LNG");
			// p1Country = metaData.get("POINT_2_COUNTRY");
			// p2Street = metaData.get("POINT_2_STREET");
			// p2City = metaData.get("POINT_2_CITY");
			// p2Lat = metaData.get("POINT_2_LAT");
			// p2Lon = metaData.get("POINT_2_LNG");
			// p2Country = metaData.get("POINT_2_COUNTRY");
			distance = metaData.get("DISTANCE_IN_METERS");
			// timestamp = metaData.get("TIMESTAMP");
			// id = metaData.get("extID");
			// stream(n(RDFFileManager.defaultPrefix + streamData.get("REPORT_ID")),
			// n(RDFFileManager.ctPrefix + "hasETA"), n(data.getEstimatedTime() + ""));
			// System.out.println("metadata: " + metaData.toString());
			metaData.close();
			break;
		}
	}
}