0% found this document useful (0 votes)
15 views3 pages

DOMO Code Exercise

The WebTrafficAnalyzer program reads web traffic data from a CSV file and identifies the top N sequences of page visits for user sessions. It counts the number of pages visited in each session and only considers sessions with at least three pages for sequence analysis. The program outputs the top sequences along with their visit counts.

Uploaded by

aslisushant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

DOMO Code Exercise

The WebTrafficAnalyzer program reads web traffic data from a CSV file and identifies the top N sequences of page visits for user sessions. It counts the number of pages visited in each session and only considers sessions with at least three pages for sequence analysis. The program outputs the top sequences along with their visit counts.

Uploaded by

aslisushant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Code:

package [Link];

import [Link];
import [Link];
import [Link];
import [Link].*;

public class WebTrafficAnalyzer {


public static void main(String[] args) {
String filePath = "C:\\Users\\Sushanth\\Downloads\\[Link]";
int topN = 5; // Number of top sequences to find

try {
List<String> sequences = findTopSequences(filePath, topN);
for (String sequence : sequences) {
[Link](sequence);
}
} catch (IOException e) {
[Link]();
}
}

private static List<String> findTopSequences(String filePath, int topN) throws


IOException {
Map<String, Integer> sequenceCounts = new HashMap<>();

// Read the file and process each line


try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
// Skip the header row
[Link]();

while ((line = [Link]()) != null) {


String[] data = [Link](",");
String sessionId = data[1];
String pageId = data[2];

// Skip sessions that have less than 3 pages


if (![Link](sessionId)) {
[Link](sessionId, 0);
}

// Increment the count for the session


[Link](sessionId, [Link](sessionId) + 1);

// If the session has visited at least 3 pages, consider it for the


sequence
if ([Link](sessionId) >= 3) {
String sequence = getSessionSequence(sessionId, filePath);
[Link](sequence,
[Link](sequence, 0) + 1);
}
}
}

// Sort the sequences by count in descending order


List<[Link]<String, Integer>> sortedSequences = new
ArrayList<>([Link]());
[Link]([Link]([Link]()));

// Get the top N sequences


List<String> topSequences = new ArrayList<>();
for (int i = 0; i < topN && i < [Link](); i++) {
[Link]<String, Integer> entry = [Link](i);
String sequence = [Link]();
int count = [Link]();
[Link](sequence + ":" + count);
}

return topSequences;
}

private static String getSessionSequence(String sessionId, String filePath)


throws IOException {
List<String> pages = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {


String line;
// Skip the header row
[Link]();

while ((line = [Link]()) != null) {


String[] data = [Link](",");
String currentSessionId = data[1];
String pageId = data[2];

// Add the page to the sequence if it belongs to the session


if ([Link](sessionId)) {
[Link](pageId);
}

// Stop adding pages once the sequence has 3 pages


if ([Link]() == 3) {
break;
}
}
}

return [Link](",", pages);


}
}

Output:

You might also like