grouping By Multiple columns.
ex:
select dno, sex, sum(sal) from emp
group by dno, sex;
DnoSexSalMap.java
--------------------
package mr.analytics;
import java.io.IOException;
import
org.apache.hadoop.io.IntWritable;
import
org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import
org.apache.hadoop.mapreduce.Mapper;
public class DnoSexSalMap extends
Mapper
<LongWritable,Text,Text,IntWritable>
{
// file : emp
// schema : id,name,sal,sex,dno
// delimiter : "," (comma)
// sample row : 101,amar,20000,m,11
// sex as key, sal as value.
public void map(LongWritable
k,Text v,
Context con)
throws IOException,
InterruptedException
{
String line =
v.toString();
String[] w = line.split(",");
String sex = w[3];
String dno = w[4];
String myKey = dno+"\t"+sex;
int sal =Integer.parseInt(w[2]);
con.write(new Text(myKey),new
IntWritable(sal));
}
}
----------------
Driver8.java
----------------
package mr.analytics;
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.lib.input.F
ileInputFormat;
import
org.apache.hadoop.mapreduce.lib.output.
FileOutputFormat;
public class Driver8
{
public static void main(String
[] args)
throws Exception
{
Configuration c = new
Configuration();
Job j = new Job
(c,"d8");
j.setJarByClass
(Driver8.class);
j.setMapperClass
(DnoSexSalMap.class);
j.setReducerClass
(RedForSum.class);
j.setOutputKeyClass
(Text.class);
j.setOutputValueClass
(IntWritable.class);
Path p1 = new Path
(args[0]); //input
Path p2 = new Path
(args[1]); //output
FileInputFormat.addInputPath(j,p1);
FileOutputFormat.setOutputPath(j, p2);
System.exit(j.waitForCompletion(true) ?
0:1);
}
}
--------------------------
submit:
[training@localhost ~]$ hadoop fs -cat
mrlab/r8/part-r-00000
11 f 25000
11 m 26000
12 f 18000
13 m 19000
______________________________
ex:
select dno, sex, sum(sal) from emp
group by dno, sex;
DnoSexSalMap.java
--------------------
package mr.analytics;
import java.io.IOException;
import
org.apache.hadoop.io.IntWritable;
import
org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import
org.apache.hadoop.mapreduce.Mapper;
public class DnoSexSalMap extends
Mapper
<LongWritable,Text,Text,IntWritable>
{
// file : emp
// schema : id,name,sal,sex,dno
// delimiter : "," (comma)
// sample row : 101,amar,20000,m,11
// sex as key, sal as value.
public void map(LongWritable
k,Text v,
Context con)
throws IOException,
InterruptedException
{
String line =
v.toString();
String[] w = line.split(",");
String sex = w[3];
String dno = w[4];
String myKey = dno+"\t"+sex;
int sal =Integer.parseInt(w[2]);
con.write(new Text(myKey),new
IntWritable(sal));
}
}
----------------
Driver8.java
----------------
package mr.analytics;
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.lib.input.F
ileInputFormat;
import
org.apache.hadoop.mapreduce.lib.output.
FileOutputFormat;
public class Driver8
{
public static void main(String
[] args)
throws Exception
{
Configuration c = new
Configuration();
Job j = new Job
(c,"d8");
j.setJarByClass
(Driver8.class);
j.setMapperClass
(DnoSexSalMap.class);
j.setReducerClass
(RedForSum.class);
j.setOutputKeyClass
(Text.class);
j.setOutputValueClass
(IntWritable.class);
Path p1 = new Path
(args[0]); //input
Path p2 = new Path
(args[1]); //output
FileInputFormat.addInputPath(j,p1);
FileOutputFormat.setOutputPath(j, p2);
System.exit(j.waitForCompletion(true) ?
0:1);
}
}
--------------------------
submit:
[training@localhost ~]$ hadoop fs -cat
mrlab/r8/part-r-00000
11 f 25000
11 m 26000
12 f 18000
13 m 19000
______________________________
The info was very much useful http://www.traininginsholinganallur.in/hadoop-training-in-chennai.html
ReplyDeletethank you for Giving such a valuable information.very helpful blog and useful to professionals and students.one of the recommanded blog.very apriciated
ReplyDeleteHadoop Online Training In Hyderabad!
Excellent and very cool idea and the subject at the top of magnificence and I am happy to this post..Interesting post! Thanks for writing it. What's wrong with this kind of post exactly? It follows your previous guideline for post length as well as clarity..
ReplyDeleteHadoop Training in Chennai
thank you giving your valuable information about the hadoop
ReplyDeletehadoop online training
Webtrackker technology is the best IT training institute in NCR. Webtrackker provide training on all latest technology such as hadoop training. Webtrackker is not only training institute but also it also provide best IT solution to his client. Webtrackker provide training by experienced and working in the industry on same technology.Webtrackker Technology C-67 Sector-63 Noida 8802820025
ReplyDeleteHadoop Training institute in indirapuram
Hadoop Training institute in Noida
Hadoop Training institute in Ghaziabad
Hadoop Training institute in Vaishali
Hadoop Training institute in Vasundhara
Hadoop Training institute in Delhi South Ex
nice blog. thank you for sharing hadoop tutorials. Really helpful for me. Keep sharing on hadoop tutorials
ReplyDeletethank you for offering such a nice content.we are very delighted to read .one of the recommended blog.very use full content for fresher and professionalsdata science training in hyderabad
ReplyDeletedata science training in ameerpet
best data science training institute in hyderabad
Thanks for giving this much useful hadoop information
ReplyDeleteHadoop Training in Hyderabad
Thank you for providing useful information and this is the best article blog for the students.learn Python programming training course.
ReplyDeletePython Training in Hyderabad
Thank you for sharing such a valuable article with good information in this blog.learn Data science course with advanced technology.
ReplyDeleteData Science Training in Hyderabad
Thanks for sharing valuable article having good information and also gain worth-full knowledge.
ReplyDeleteOracle ICS Online Training
Thank you for sharing your awesome and valuable article this is the best blog for the students they can also learn.
ReplyDeleteWorkday Online Training