Return-Path: X-Original-To: apmail-hadoop-common-user-archive@www.apache.org Delivered-To: apmail-hadoop-common-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 817E0105F6 for ; Thu, 19 Dec 2013 04:16:07 +0000 (UTC) Received: (qmail 19434 invoked by uid 500); 19 Dec 2013 04:15:55 -0000 Delivered-To: apmail-hadoop-common-user-archive@hadoop.apache.org Received: (qmail 19316 invoked by uid 500); 19 Dec 2013 04:15:55 -0000 Mailing-List: contact user-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@hadoop.apache.org Delivered-To: mailing list user@hadoop.apache.org Received: (qmail 19309 invoked by uid 99); 19 Dec 2013 04:15:54 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 19 Dec 2013 04:15:54 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of unmeshabiju@gmail.com designates 209.85.212.44 as permitted sender) Received: from [209.85.212.44] (HELO mail-vb0-f44.google.com) (209.85.212.44) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 19 Dec 2013 04:15:48 +0000 Received: by mail-vb0-f44.google.com with SMTP id x8so342159vbf.17 for ; Wed, 18 Dec 2013 20:15:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=mqB2oOvf21zzXFJmEN7jzcm/QCT12u5Ml/gAmra0b64=; b=OaCTry6XMH3EvN7gmvbmyKoA1xfGRx8TxzQeiAfzKYmC93cXquZWM+4ObmPUj5Y4Ny s3ezcUyR/FTqviEPu7JUiAbqi3vYtdxCKqWC4L1+NZX+OjJU3GzZfr/arrMOiqMWHKM+ 4xLYq/N3JPJBomMVSFtsATm5nZa3TOOJdxl3w4RT0r5bS27d91q0K2Iufu7HFCw3t73L 7A+QmKRODngesy69sZGtege5JAsprG4NrMF+Cr3pM2ZtwxfKy5gQsJCkNiLYVN6dJp72 oqZXfBMbTLpct3X1hiyQeTH/ktLCR31pf0pBeQ+yHqZ4obIvXZqu3t4qPjWCLUTro+bs o2Sg== MIME-Version: 1.0 X-Received: by 10.220.53.135 with SMTP id m7mr12883059vcg.12.1387426527234; Wed, 18 Dec 2013 20:15:27 -0800 (PST) Received: by 10.59.8.2 with HTTP; Wed, 18 Dec 2013 20:15:27 -0800 (PST) In-Reply-To: References: Date: Thu, 19 Dec 2013 09:45:27 +0530 Message-ID: Subject: Re: Get dynamic values in a user defined class from reducer. From: unmesha sreeveni To: User Hadoop , rdyer@iastate.edu Content-Type: multipart/alternative; boundary=001a1133f8a44eceab04eddb6905 X-Virus-Checked: Checked by ClamAV on apache.org --001a1133f8a44eceab04eddb6905 Content-Type: text/plain; charset=ISO-8859-1 Thanks for ur reply Robert Dyer. Thanks for spending ur valuable time for clearing my doubts. I need to pass some other values too Reducer ------------ public class Reduce extends MapReduceBase implements Reducer { static int cnt =0; ArrayList ar = new ArrayList(); String data = null; public void reduce(Text key, Iterator values, OutputCollector output, Reporter reporter) throws IOException { System.out.println("In reducer"); int sum = 0; String line = key.toString(); StringTokenizer itr = new StringTokenizer(line); while (values.hasNext()) { sum += values.next().get(); } output.collect(key, new IntWritable(sum)); String data = key+" "+sum; ar.add(data); writeToFile(ar); ar.add("\n"); int index=Integer.parseInt(itr.nextToken()); String value=itr.nextToken(); String classLabel=itr.nextToken(); int count=sum; } public static void writeToFile(ArrayList text) { System.out.println("In reduce write to file "); try { cnt++; C45 id=new C45(); System.out.println("count "+cnt); Path input = new Path("C45/intermediate"+id.current_index+".txt"); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(fs.create(input, true))); System.out.println("Text from Reducer: "+ "C45/intermediate"+id.current_index+".txt"+ text); System.out.println("file exists:C45/intermediate"+id.current_index+".txt"+fs.exists(input)); for(String str: text) { bw.write(str); } bw.newLine(); bw.close(); } catch (Exception e) { System.out.println("File is not creating in reduce"); } } } Driver -------- GainRatio gainObj = new GainRatio(); gainObj.getcount(); GainRatio -------------- public void getcount() { System.out.println("In gainratio --- getcount "); C45 id=new C45(); FileInputStream fstream; try { Configuration config = new Configuration(); FileSystem dfs = FileSystem.get(config); Path path = new Path("C45/intermediate"+id.current_index+".txt"); System.out.println("file exists:C45/intermediate"+id.current_index+".txt"+dfs.exists(path)); System.out.println(path); BufferedReader br=new BufferedReader(new InputStreamReader(dfs.open(path))); String line; StringTokenizer itr; while ((line = br.readLine()) != null) { System.out.println("In gain Ratio : "+line); itr= new StringTokenizer(line); count[linenumber][0]=itr.nextToken(); count[linenumber][1]=itr.nextToken(); count[linenumber][2]=itr.nextToken(); count[linenumber][3]=itr.nextToken(); int i=linenumber; linenumber++; } System.out.println("outsidec while"); count[linenumber][0]=null; count[linenumber][1]=null; count[linenumber][2]=null; count[linenumber][3]=null; br.close(); emptyfile(id.current_index); } catch (Exception e) { e.printStackTrace(); } } Here from reducer many files are created eg: intermediate0.txt .....upto intermediate7.txt according to my input dataset. But here only intermediate0.txt is created all other creation of file get failed. Is that the same reason you explained before. For this also should i go for couters? On Wed, Dec 18, 2013 at 11:09 PM, Robert Dyer wrote: > Generally speaking, static fields are not useful in Hadoop. > > The issue you are seeing is that the reducer is running in a separate VM > (possibly on a different node!) and thus the static value you are reading > inside of Mid is actually a separate instantiation of that class and field. > > If you have an integer you need to set and read from the driver/mid, you > can use a counter. E.g.: > > > http://www.philippeadjiman.com/blog/2010/01/07/hadoop-tutorial-series-issue-3-counters-in-action/ > > - Robert > > On Wed, Dec 18, 2013 at 4:02 AM, unmesha sreeveni wrote: > >> Can any one pls suggest a good way >> My scenario: >> i hav >> 1.Driver class >> 2.Mapper class >> 3.reducer class >> 4.Mid class >> After completing mapper it goes to reduce. From reducer it will be going >> to Driver and from driver to Mid class >> >> so i need to get a data from reducer class to Mid class >> So for that i declared a static variable in reduce and tried to access >> that variable in Mid class.But while executing i am only getting the 0 >> value. How to overcome this. >> >> >> Reducer.java >> -------------------- >> public class Reduce extends MapReduceBase >> implements Reducer { >> int ig; >> public void reduce(Text key, Iterator values, >> OutputCollector output, >> Reporter reporter) throws IOException { >> ig = 9; >> } >> } >> Driver.java >> gainObj.getcount(); >> >> >> Mid.java >> public class Mid{ >> public void getcount(String args) throws IOException >> { >> //WANT TO GET THE IG VALUE IE "9" HERE So i did like >> this >> Reduce r = new Reduce(); >> System.out.println("red "+ r.ig); >> } >> } >> But my result is "0". Not 9. >> How to solve this? >> >> >> >> >> -- >> *Thanks & Regards* >> >> Unmesha Sreeveni U.B >> >> *Junior Developer* >> >> >> > -- *Thanks & Regards* Unmesha Sreeveni U.B *Junior Developer* --001a1133f8a44eceab04eddb6905 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Thanks for ur reply Robert Dyer.
Thanks for spending ur valuabl= e time for clearing my doubts.

<= /div>
= I need to pass some other values too


Reducer
------------
public =A0= class Reduce extends MapReduceBase
implements Reducer<Text, IntWritable= , Text, IntWritable> {

=
static int cnt =3D0;
ArrayList<String> ar =3D new ArrayList<String>();
<= span class=3D"" style=3D"white-space:pre"> String data =3D null;
public void reduce(Text key, Iterat= or<IntWritable> values,
= OutputCollector<Text, IntWritable> output,
Reporter reporter) throws IOExcep= tion {
System.out.prin= tln("In reducer");
int sum =3D 0;
String line =3D key.toString();
<= span class=3D"" style=3D"white-space:pre"> StringTokenizer itr =3D = new StringTokenizer(line);
while (values.hasNext()) {<= /div>
sum +=3D values.next().get()= ;
}
output.collect(key, new IntWritable(sum));

=
String data =3D key+" &= quot;+sum;
ar.add(data);
=A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0writeToFile(ar);
ar.add("\n");
int index=3DInteger.parseI= nt(itr.nextToken());
String value=3Ditr.nextToken();
<= span class=3D"" style=3D"white-space:pre"> String classLabel=3Ditr.= nextToken();
int count=3Dsum;

}

publ= ic static void writeToFile(ArrayList<String> =A0text) {
System.out.println("In reduce= write to file ");
try {

cnt++;
C= 45 id=3Dnew C45();
System.out.println("count &q= uot;+cnt);

Path input =3D new P= ath("C45/intermediate"+id.current_index+".txt");=
Configuration conf =3D new Configuration();
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 FileSystem fs =3D FileSyst= em.get(conf);
Buf= feredWriter bw =3D new BufferedWriter(
new OutputStreamWriter(fs.creat= e(input, true)));
System.out.println("Text fro= m Reducer: "+ "C45/intermediate"+id.current_index+".txt= "+ text);
System.out.println("file exi= sts:C45/intermediate"+id.current_index+".txt"+fs.exists(inpu= t));

=
for(String str: text) {
bw.write(str);
}

=
bw.newLine();
<= div class=3D"gmail_default"> bw.close();
} catch (Exception e) {
System.out.println("File i= s not creating in reduce");
}
}

=
}

Driver
----= ----
GainRa= tio =A0gainObj =3D new GainRatio();<= /div>
gainObj.getcount();
<= font face=3D"arial, sans-serif">


GainRatio<= /span>
--------------=

public void getcount()
{=A0
System.out.println("In gainratio --- getcount ");
C45 id=3Dnew C45();
FileInputStream fs= tream;
try {<= /div>

Configuration config =3D new Configuration();
FileSystem dfs = =3D FileSystem.get(config);
Path path =3D new Path("C45/intermediate"+id.= current_index+".txt");
System.out.= println("file exists:C45/intermediate"+id.current_index+".tx= t"+dfs.exists(path));
System.out.println(path);
BufferedReader br= =3Dnew BufferedReader(new InputStreamReader(dfs.open(path)));
String line;
StringTokenizer itr;
while ((lin= e =3D br.readLine()) !=3D null) =A0 {
System.out.println("In gain Ratio : &q= uot;+line);
itr=3D new Strin= gTokenizer(line);
= count[linenumber][0]=3Ditr.nextToken();
count[linenumber][1]=3Ditr.nextToken();=
count[linenumber= ][2]=3Ditr.nextToken();
count[linenumber][3]=3Ditr.nextToken();
int i=3Dlinenumber;
linenumber++;
}
System.out.println(&= quot;outsidec while");
count[linenumber]= [0]=3Dnull;
= count[linenumber][1]=3Dnull;
count[linenumber][2]=3Dnull;
count[linenumber]= [3]=3Dnull;
= br.close();
= emptyfile(id.current_index);

} c= atch (Exception e) {

e.printStackTrace();

}
}


Here from reducer many files are = created
eg: intermediate0.txt
=A0 =A0 =A0.....upto inte= rmediate7.txt according to my input dataset.

But here only intermediate0.txt is created all other cr= eation of file get failed.
Is that the same reason you explained = before.

For this also should i go for couters?


On Wed, Dec 18, 2013 at 11:09 PM, Robert D= yer <psybers@gmail.com> wrote:
Generally speaking, static = fields are not useful in Hadoop.

The issue you are seein= g is that the reducer is running in a separate VM (possibly on a different = node!) and thus the static value you are reading inside of Mid is actually = a separate instantiation of that class and field.

If you have an integer you need to set and read from th= e driver/mid, you can use a counter. =A0E.g.:

- Robert

On Wed, Dec 18, 2013 at 4:0= 2 AM, unmesha sreeveni <unmeshabiju@gmail.com> wrote:
Can any one pls suggest a good way=A0
My scenario:
i hav
1.Driver class
2.Mapper class
3.reducer class
4.Mid cl= ass
After compl= eting mapper it goes to reduce. From reducer it will be going to Driver and= from driver to Mid class

so i need to get a data from reducer class to Mi= d class
=A0So for that i declared a static variable in reduce and tried to access t= hat variable in Mid class.But while executing i am only getting the 0 value= . How to overcome this.


Reducer.java
--------------------
public =A0class Reduce extends MapR= educeBase
implements Re= ducer<Text, IntWritable, Text, IntWritable> {
int ig;
public vo= id reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output,
Reporter reporter) throws IOException {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ig =3D 9;
}
}
Driver.java
= =A0 =A0 =A0 =A0gainObj.getcount();


= Mid.java
public class Mid{
public void getcount(String ar= gs) throws IOException
{=A0
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0//WANT TO GET THE IG VALUE IE &= quot;9" HERE So i did like this
=A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 Reduce r =3D new Reduce();
System.out.println("red= "+ r.ig);
}
}
But my result is "0&= quot;. Not 9.
How to solve this?




--
Thanks & Regards

Unmesha Sreeveni U.B
Junior Developer





--
Thanks & Regards

Unmesha Sreeveni U.B
Junior Developer

--001a1133f8a44eceab04eddb6905--