DSBDA Manual
DSBDA Manual
DATE:- PLACE:-Belhe
Batch :B
Program:-
public class wordcount{
public static void main(String[] args) {
// Sample input string
String text = "this is a wordcount example.";
// Count words in the input string
int wordCount = countWords(text);
// Output the result
System.out.println("Word count: " + wordCount);
}
public static int countWords(String text) {
// Trim any leading or trailing spaces
text = text.trim();
// If the string is empty, return 0
if (text.isEmpty()) {
return 0;
}
// Split the text by one or more spaces
String[] words = text.split("\\s+");
// Return the number of words in the array
return words.length;
}
}
Output:-
EXPERIMENT NO:12
Design a distributed application using MapReduce which
processes a log file of a system.
Program:-
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class kalyani {
// Mapper: Splits lines into words and creates (word, 1) pairs
public static class Mapper {
public List<Map<String, Integer>> map(String input) {
List<Map<String, Integer>> wordCountList = new ArrayList<>();
Map<String, Integer> wordCountMap = new HashMap<>();
// Split the input into words (based on non-word characters)
String[] words = input.split("\\W+")
for (String word : words) {
if (!word.isEmpty()) {
word = word.toLowerCase();
wordCountMap.put(word, wordCountMap.getOrDefault(word, 0) +
1);
}
}
// Add the word counts from this line to the result
wordCountList.add(wordCountMap);
return wordCountList;
}
}
// Reducer: Aggregates word counts from all mappers
public static class Reducer {
public Map<String, Integer> reduce(List<Map<String, Integer>>
mappedResults) {
Map<String, Integer> finalCountMap = new HashMap<>();
// For each map in the list of results
for (Map<String, Integer> map : mappedResults) {
// For each word and its count in the map
for (Map.Entry<String, Integer> entry : map.entrySet()) {
finalCountMap.put(entry.getKey(),
finalCountMap.getOrDefault(entry.getKey(), 0) +
entry.getValue());
}
}
return finalCountMap;
}
}
// Main Method
public static void main(String[] args) throws InterruptedException,
ExecutionException, IOException {
String inputText = "Hello world hello mapreduce hello Java world";
// 1. Step 1: Map phase (split input into words and create word count
pairs)
Mapper mapper = new Mapper();
List<Map<String, Integer>> mappedResults = mapper.map(inputText);
// 2. Step 2: Reduce phase (aggregate word counts)
Reducer reducer = new Reducer();
Map<String, Integer> finalWordCount = reducer.reduce(mappedResults);
// 3. Output the result
System.out.println("Word Count Results:");
for (Map.Entry<String, Integer> entry : finalWordCount.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Output:-
EXPERIMENT NO:13
Locatate databases(eg.Sample_weather.txt) for Working on
Weather data which read the next input file & finds Average
for temperature ,dew point ,and wind speed using java.
Program:-
# This code does nothing, as the provided input is just log messages.
# To process these logs, you would need to parse them into a structured format.
import re
logs = """
"""
if line:
match = re.match(pattern, line) # Indented this line to be inside the 'if' block
if match:
#The following lines were not part of the code, but rather log entries.
#They have been commented out as they would cause a syntax error.
Output:-