如何在Map Reduce中使用ArrayList?

时间:2016-07-19 04:22:08

标签: java hadoop arraylist mapreduce

我编写了一个Java代码,我需要在Map Reduce中编写。 我对Map Reduce概念比较陌生。

作为输入,我有一个包含多个管道分隔的列的文件。 根据我的代码if条件中给出的某些条件,我需要链接文件中的Row-Id。

for Example:
rowid|rollno|sbjctcode|subjct|dt_of_exam......|marks

2|1000|0101|PHY|02072015......|-060

9|1000|0101|PHY|02072015......|060

Desired Output:

2[9]

这样,我就可以知道文件中的错误在哪里,或者是多个条目的情况等等。

这是我的Java代码:

import java.io.*;
import java.util.*;

public class MRCode {

public static void main(String[] args) {

    BufferedReader in = null;
    BufferedWriter out = null;
    String in_line;
    String PrevRollNo = "150";


    ArrayList<Transaction> PCMList = new ArrayList<Transaction>();
    ArrayList<Transaction> AdditionalList = new ArrayList<Transaction>();
    ArrayList<Transaction> OtherList = new ArrayList<Transaction>();

    try {
        in = new BufferedReader(new FileReader(
                "C:\\Users\\Desktop\\data.txt"));
        File out_file = new File("C:\\Users\\Desktop\\op.txt");
        if (!out_file.exists()) {
            out_file.createNewFile();
        }

        FileWriter fw = new FileWriter(out_file);
        out = new BufferedWriter(fw);

        while ((in_line = in.readLine()) != null) {

            Transaction transact = new Transaction(in_line);

            if (transact.rollNo.equals(PrevRollNo)) {

                if (transact.subjctCode.equals("PHY")
                        || transact.subjctCode.equals("CHEM")
                        || transact.subjctCode.equals("MATH")
                        && transact.dt_of_exam == PrevDate) {
                    PCMList.add(transact);

                    break;
                } else {
                    OtherList.add(transact);
                }

                if (transact.subjctCode.equals("0101")) {
                    Iterator<Transaction> pcm;
                    pcm = PCMList.iterator();

                    while (pcm.hasNext()) {

                        Transaction pcmtxn = pcm.next();

                        if (pcmtxn.subjctCode.equals("0102")
                                && pcmtxn.dt_of_exam == transact.dt_of_exam
                                && pcmtxn.subjct.equals(transact.subjct)
                                && pcmtxn.marks == Math.abs(transact.marks)) {

                            pcmtxn.tgtfound = true;
                            transact.srcfound = true;
                            System.out.println(pcmtxn.row_id + "["
                                    + transact.row_id + "]");
                        }

                    }

                }

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

static class Transaction {
    public String rollNo, subjctCode, subjct, l1, l2;
    public int row_id, dt_of_exam, dt_of_chking;
    public double marks;
    public boolean srcfound, tgtfound;

    public Transaction(String in_line) {
        String[] SplitData = in_line.split("\\|");
        row_id = Integer.parseInt(SplitData[0]);
        rollNo = SplitData[1];
        subjctCode = SplitData[4];
        subjct = SplitData[5];
        l1 = SplitData[7];
        l2 = SplitData[8];
        dt_of_exam = Integer.parseInt(SplitData[2]);
        dt_of_chking = Integer.parseInt(SplitData[3]);

        marks = Double.parseDouble(SplitData[11]);
        srcfound = false;
        tgtfound = false;
    }
}

}

请帮助我解释如何在我的mapper类中使用Array List。 或者如何在Map Reduce中更好地编写此代码。 注意:这只是一个快照。

分享您的观点。

0 个答案:

没有答案