Hadoop MapReduce for循环

时间:2012-11-15 00:15:06

标签: hadoop mapreduce

我有一个变量_b。我正在累加,但是对于k的for循环的每次迭代,变量将重置为0。相同的代码可以作为本地Java应用程序正常工作。有谁知道为什么会这样?以下是我的程序代码片段:

public void map(Object key, Text value, Context context)
{
    int m, b, _m, _b;

    for (int i = 1; i <= N; ++i)
    {
        for (int j = i; j <= N; ++j)
        {
            _m = 0;
            _b = 0;
            for (int k = i; k <= j; ++k)
            {
                int _bTemp=0;
                context.write(new Text(Integer.toString(i)+","+Integer.toString(j)),new Text("Reset_bTemp=" + _bTemp + " ShouldNotReset_b=" + _b ));
                /* Calculating smallM, _m and assigning it to global variable m */
                _m += calculateSmallM(k,queryWord,value);
                //context.write(new Text(Integer.toString(i)+","+Integer.toString(j)),new Text("_m=" + _m));
                /* Calculating smallB, _b and assigning it to global variable b */
                _bTemp = calculateSmallB(k,value);
                _b += _bTemp;
                context.write(new Text(Integer.toString(i)+","+Integer.toString(j)),new Text("_bTemp=" + _bTemp + "_b=" + _b ));
            }// end of for k

            /* Assigning _m and _b to m and b */
            m = _m;
            b = _b;

            /* Calculating Discrepancy Score */
            discrepancyScore = calculateDiscrepancyScore(m,b,M,B);

            if (discrepancyScore > dmax)
            {
                dmax = discrepancyScore;
                iMax = i;
                jMax = j;

            }

        }// end of for j

        hotspot_interval = Double.toString(dmax) + "=(" + Integer.toString(iMax) + "," + Integer.toString(jMax) + ")-(" + Integer.toString(i) + "," + Integer.toString(j) + ")";
        hotspot.set(hotspot_interval);

    }// end of for i 
}

public int calculateSmallB(int k, Text value, Context context) throws IOException, InterruptedException
{
    int colon;
    String line = null;
    String tpifreqList = null;
    String lineWord = null;
    StringTokenizer st = new StringTokenizer(value.toString(),"\n");
    line = st.nextToken();
    //while (st.hasMoreTokens()) 
    //while((line = st.nextToken()) != null)
    while(line != null)
    {
        //line = st.nextToken();
        colon = line.indexOf("::");
        lineWord = line.substring(0, colon);
        tpifreqList = line.substring(lineWord.length() + 2); 
        /* lineWord.length()+1 will give the string starting with ":" */
        context.write(new Text(lineWord), new Text("Value of _b just after while(" + k + ")=" + smallB));

        /* trimming to remove whitespaces */
        tpifreqList = tpifreqList.trim();
        String[] tpiFreq = tpifreqList.split(","); /* Splitting to get a array of tpindex-freq values */
        /* Iterating through the array to retrieve and sum up all the frequencies for the 'word' occurring at tpindex k */
        for (int n = 0; n < tpiFreq.length; n++) 
        {

            tpiFreq[n] = tpiFreq[n].trim();
            /* Checking if the tpindex is equal to k */
            if (tpiFreq[n].indexOf(Integer.toString(k)) == 0) 
            {
                /* If yes, retrieve the frequency */
                int hypen = tpiFreq[n].indexOf("-");
                int freq = Integer.parseInt(tpiFreq[n].substring(hypen + 1));
                //_b += freq;
                smallB += freq;
                context.write(new Text(lineWord), new Text("calculateSmallB(" + k + ")=" + smallB));
            }
        }
        context.write(new Text(lineWord), new Text("_b just after for(" + k + ")=" + smallB));
        line = st.nextToken();
    }
    return smallB;
}

0 个答案:

没有答案