public class AddTwoIntegers {
public static void main(String[] args) {
int first = 10;
int second = 20;
int sum = first + second;
System.out.println("The sum is: " + sum);
}
}
public static void main(String[] args) {
int first = 10;
int second = 20;
int sum = first + second;
System.out.println("The sum is: " + sum);
}
}
________________________________________________________
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
System.out.print("Input the first number: ");
int num1 = input.nextInt();
System.out.print("Input the second number: ");
int num2 = input.nextInt();
int sum = num1 + num2;
System.out.println();
System.out.println("Sum: "+sum);
}
}
__________________________________________________
__________________________________________________
public class WordCount {
static int
wordcount(String string)
{
int
count=0;
char
ch[]= new char[string.length()];
for(int
i=0;i<string.length();i++)
{
ch[i]= string.charAt(i);
if(
((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!='
')&&(i==0)) )
count++;
}
return
count;
}
public static
void main(String[] args) {
String
string =" Hi How are you I am
fine";
System.out.println(wordcount(string) + " words.");
}
}
_________________________________________________
Mapreduce Addition
_________________________________________________
Mapreduce Addition
package aa;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import
org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import
org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class GlobalNumberAddition {
public static class
GlobalNumberAdditionMapper
extends Mapper <
Object, Text, Text, IntWritable > {
int sum = 0;
public void
map(Object key, Text value, Context context) throws IOException,
InterruptedException
{
StringTokenizer itr
= new StringTokenizer(value.toString());
while
(itr.hasMoreTokens()) {
String str =
itr.nextToken();
sum = sum +
Integer.parseInt(str);
}
}
public void
cleanup(Context context) throws IOException,
InterruptedException
{
context.write(new
Text("Addition of numbers is"), new IntWritable(sum));
}
}
public static void
main(String[] args) throws Exception {
Configuration conf =
new Configuration();
Job job =
Job.getInstance(conf, "Global Addition of Numbers");
job.setJarByClass(GlobalNumberAddition.class);
job.setMapperClass(GlobalNumberAdditionMapper.class);
job.setNumReduceTasks(0);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
No comments:
Post a Comment