Reputation: 7
I had written a driver code with two input arguments: args[0] and args[1].
I want to use args[0] i.e input file path, as one of my file path in mapper code such that, my buffer reader can read the file as shown bellow:
{
public void map(LongWritable key, Text value, Context context)
throws java.io.IOException, InterruptedException {
ArrayList<String> al=new ArrayList<String>();
ArrayList<String> nl =new ArrayList<String>();
String line = null;
String filePath = args[0]; String path= args[1];
try {
FileReader fileReader =
new FileReader(filePath);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null)
{
if(line.contains("result") && line.contains("Complete_IND"))
{
String pattern1 = "venk";
String pattern2 = "si";
Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2));
Matcher m = p.matcher(line);
while (m.find()) {
System.out.println(m.group(1));
String num=m.group(1);
int fin=Integer.parseInt(m.group(1));
if(fin>0)
{
System.out.println("number greater than zero"+fin);
al.add(num);
for(String obj:al)
{System.out.println("final obj"+obj);}}
}
}
}
// Always close files.
bufferedReader.close();
// for(String obj:al){
// System.out.println(obj);
//}
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
filePath + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ filePath + "'");
// Or we could just do this:
// ex.printStackTrace();
}
System.out.println("secondloop");
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(path);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
System.out.println("djdne"+path);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferReader =
new BufferedReader(fileReader );
System.out.println("djdne"+path);
while((line = bufferReader.readLine()) != null)
{
System.out.println("djdne"+line);
for(String obj:al) {
if(line.contains(obj))
{
System.out.println("77");
System.out.println("1 obj is"+obj);
String pattern1 = "|";
String pattern2 = "|";
Pattern pt = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2));
Matcher mac = pt.matcher(line);
System.out.println("22");
while (mac.find()) {
System.out.println("33");
System.out.println(mac.group(1));
nl.add(mac.group(1));
for(int i=0;i<nl.size();i++)
{
Text si= new Text(al.get(i));
Text vi=new Text(nl.get(i));
context.write(si, vi);
}
}
}
}
}
bufferReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
path + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ path + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
Upvotes: 1
Views: 1578
Reputation: 1039
in configuration Mapper class you can get file name. Then you can use it for reading. Example more detail
public void configure(JobConf job) {
inputFile = job.get("mapred.input.file");
}
Upvotes: 0
Reputation: 3228
you can get it from mapper context and current input split.
((FileSplit) context.getInputSplit()).getPath();
Upvotes: 2