映射器函数中的静态变量值没有变化

时间:2016-12-22 10:00:04

标签: java hadoop mapreduce iteration

我正在尝试为BFS图算法编写迭代mapreduce 为了指定何时停止迭代,我试图使用计数器

代码是

Option Explicit

Private Sub Findcheck_Click()

Dim Rngfound1 As Range
Dim Rngfound2 As Range
Dim Rngfound3 As Range
Dim FindRng As Range

Dim LastRow As Long, i As Long

' modify "Sheet1" to your sheet's name (where you want to search for the 3 words)
With Worksheets("Sheet1")
    ' init row number
    i = 1

    ' find last row with data in Column A >> you might need to modify to another column
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

msearch:

    Set FindRng = .Range(Cells(i, 1), Cells(LastRow, 5))
    Debug.Print FindRng.Address

    If i > LastRow Then GoTo mexit

    ' loop through all rows from row 2 till last row with data
    Set Rngfound1 = FindRng.Find(What:=TextBox1.Value)

    If Rngfound1 Is Nothing Then
        GoTo mexit
    Else
        ' successfult find of TextBox1 >> save the row number
        i = Rngfound1.Row
    End If

    Set Rngfound2 = .Range("A" & i).EntireRow.Find(TextBox2.Value)
    If Rngfound2 Is Nothing Then
        Set Rngfound1 = Nothing
        i = i + 1
        GoTo msearch
    End If

    Set Rngfound3 = .Range("A" & i).EntireRow.Find(TextBox3.Value)

    If Rngfound3 Is Nothing Then
        Set Rngfound1 = Nothing
        Set Rngfound2 = Nothing
        i = i + 1
        GoTo msearch
    Else
        MsgBox "Found at row" & i
        Exit Sub
    End If

End With

mexit:
MsgBox "Not Found"

End Sub

这个想法是当计数器保持为0时,表示所有节点都已遍历(全部已变为黑色)并且BFS已完成

public class GraphSearch extends Configured implements Tool {

static int counter=0;


public static class MapClass extends MapReduceBase implements
Mapper<LongWritable, Text, IntWritable, Text> {

    public void map(LongWritable key, Text value, OutputCollector<IntWritable, Text> output,
            Reporter reporter) throws IOException {

        Node node = new Node(value.toString()) ;




        if (node.getColor() == Node.Color.GRAY) {
            if(node.getDistance()==Integer.MAX_VALUE)
                node.setDistance(0);


            for (int v : node.getEdges()) {

                Node vnode = new Node(v);


                    vnode.setDistance(node.getDistance() + 1);
                    vnode.setColor(Node.Color.GRAY);
                    output.collect(new IntWritable(vnode.getId()), vnode.getLine());

            }

            node.setColor(Node.Color.BLACK);
            setCounter(1);



        }


        output.collect(new IntWritable(node.getId()), node.getLine());

    }

    static void setCounter(int i) {
        if(i==1)
        counter=counter+1;
        else
        counter=0;

    }

但是setCounter没有显示计数器值的任何变化。 该怎么办? 有没有其他方法在Java中使用全局变量(比如在c ++中,它非常简单 - 只需在所有内容之外声明它)??

1 个答案:

答案 0 :(得分:0)

目前还不清楚你在做什么,但无论如何:你在-(void)createFolder{ AWSS3PutObjectRequest *awsS3PutObjectRequest = [AWSS3PutObjectRequest new]; awsS3PutObjectRequest.key = [NSString stringWithFormat:@"%@/", @"FolderName"]; awsS3PutObjectRequest.bucket = @"Bucket_Name"; AWSS3 *awsS3 = [AWSS3 defaultS3]; [awsS3 putObject:awsS3PutObjectRequest completionHandler:^(AWSS3PutObjectOutput * _Nullable response, NSError * _Nullable error) { if (error) { NSLog(@"error Creating folder"); }else{ NSLog(@"Folder Creating Sucessful"); } }]; } 类中声明static计数器变量,这是一个hadoop工作类。 Hadoop作业在其自己的JVM中的hadoop节点上运行。 hadoop中的映射器在单独的JVM中运行,并拥有自己的变量实例。这就是你的代码不起作用的原因。

我不知道你的算法应该如何工作所以我不建议任何改变。从Hadoop tutorials开始,了解map reduce算法的想法。