比较mapreduce中的三个字段

vsnjm48y  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(349)

我有一个MapReduce程序,输入名字、姓氏和手机号码。我想将这3个字段分组为一个键。为此,我用了 WritableComparable 类的接口。
我的代码是:

private static class Multifield implements WritableComparable<Multifield> {

        Text firstname1;
        Text lastname1;
        Text mobile1;

        public Multifield(Text firstname1, Text lastname1,Text mobile1) {
            this.firstname1 = firstname1;
            this.lastname1 = lastname1;
            this.mobile1=mobile1;
        }
        public Multifield() {
            this.firstname1 = new Text();
            this.lastname1 = new Text();
            this.mobile1=new Text();

        }

        public void write(DataOutput out) throws IOException {
            this.firstname1.write(out);
            this.lastname1.write(out);
            this.mobile1.write(out);

        }

        public void readFields(DataInput in) throws IOException {

            this.firstname1.readFields(in);
            this.lastname1.readFields(in);
            this.mobile1.readFields(in);

        }

        public int compareTo(Multifield pop) {

            if(firstname1.equals(pop.firstname1))
            {
                return lastname1.compareTo(pop.lastname1);
            }
            else
            {
                return firstname1.compareTo(pop.firstname1);
            }
        }
        @Override
        public String toString() {
            return "Customer Details [firstname=" + firstname1 + ", lastname=" + lastname1
                    + ", mobile=" + mobile1 + "]";
        }

上面的代码只是比较firstname和lastname。所以,我只根据前两个字段得到输出。
如何修改compareto()方法来比较所有三个字段,以便将它们作为键分组?

8fsztsew

8fsztsew1#

我们用于比较两个字段的逻辑是:

if(field1 = field2) then Result = 0;
if(field1 > field2) then Result = 1;
if(field1 < field2) then Result = -1;

为了便于解释,让我们假设,我们正在比较两个相同类型的对象,其中包含字段a、b和c。

Object 1 contains a1, b1 and c1 (instances of a, b and c)
Object 2 contains a2, b2 and c2 (instances of a, b and c)

以下是我们需要处理的不同情况:

a) if(a1 = a2, b1 = b2, c1 = c2) then Result = 0;  // (All equal)
b) if(a1 > a2)                   then Result = 1;  // (Since a1 > a2, we short circuit at a)
c) if(a1 < a2)                   then Result = -1; // (Since a1 < a2, we short circuit at a) 
d) if(a1 = a2, b1 > b2)          then Result = 1;  // (Since b1 > b2, we short circuit at b) 
e) if(a1 = a2, b1 < b2)          then Result = -1; // (Since b1 < b2, we short circuit at b) 
f) if(a1 = a2, b1 = b2, c1 > c2) then Result = 1;  // (Since c1 > c2) 
g) if(a1 = a2, b1 = b2, c1 < c2) then Result = -1; // (Since c1 < c2)

其代码为:

public int compareTo(Person otherPerson) {
    int cmp = firstName.compareTo(otherPerson.firstName);
    if(cmp != 0) return cmp;

    cmp = lastName.compareTo(otherPerson.lastName);
    if(cmp != 0) return cmp;

     return mobile.compareTo(otherPerson.mobile);
}
n3schb8v

n3schb8v2#

与此类似的一个很好的解决方案是:

public int compareTo(Multifield pop) {

    int i = firstname1.compareTo(pop.firstname1);
    if (i != 0) return i; //stop here if first names are not equal, and return their difference

    i = lastname1.compareTo(pop.lastname1);
    if (i != 0) return i; //stop here if last names are not equal, and return their difference.

    return mobile1.compareTo(pop.mobile1); //at this point, first names and last names are equal, so return the difference of the mobiles

}

相关问题