java - 在 Hadoop 中设置可写?

标签 java hadoop mapreduce writable

我正在尝试在 Hadoop 中创建 SetWritable。这是我的实现。我刚刚开始使用 MapReduce,我不知道我到底应该如何做到这一点。我写了下面的代码,但它不起作用。

自定义可写(需要是一组):

public class TextPair implements Writable {

    private Text first;
    public HashSet<String> valueSet = new HashSet<String>();
    public TextPair() {

    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(valueSet.size());
        Iterator<String> it = valueSet.iterator();
        while (it.hasNext()) {
            this.first = new Text(it.next());
            first.write(out);
        }
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        Iterator<String> it = valueSet.iterator();
        while (it.hasNext()) {
            this.first = new Text(it.next());
            first.readFields(in);
        }
    }

}

映射器代码:

public class TokenizerMapper extends Mapper<Object, Text, Text, TextPair> {

    ArrayList<String> al = new ArrayList<String>();
    TextPair tp = new TextPair();

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

        String [] val = value.toString().substring(2,value.toString().length()).split(" ");

        for(String v: val) {
            tp.valueSet.add(v);
        }
        String [] vals = value.toString().split(" ");

        for(int i=0; i<vals.length-1; i++) {
            setKey(vals[0],vals[i+1]);
            System.out.println(getKey());
            context.write(new Text(getKey()), tp); 
        }
    }

    public void setKey(String first,String second) {

        al.clear();
        al.add(first);
        al.add(second);

        java.util.Collections.sort(al);
    }

    public String getKey() {

        String tp = al.get(0)+al.get(1);
        return tp;
    }
 }

我基本上试图从映射器中发出 SetWritable 作为值。请建议我需要做出哪些改变。谢谢!

最佳答案

我想说你的阅读和写作方式有问题。您需要知道 Set 有多大,并使用它来读取正确数量的 Text 对象。

我将您的版本更改为一组文本对象,因为它们可以轻松读取和写入。

public class TextWritable implements Writable {

    private Set<Text> values;

    public TextPair() {
        values = new HashSet<Text>();
    }

    @Override
    public void write(DataOutput out) throws IOException {

        // Write out the size of the Set
        out.writeInt(valueSet.size());

        // Write out each Text object
        for(Text t : values) {
            t.write(out);
        }
    }

    @Override
    public void readFields(DataInput in) throws IOException {

        // Make sure we have a HashSet to fill up
        values = new HashSet<Text>();

        // Get the number of elements in the set
        int size = in.readInt();

        // Read the correct number of Text objects
        for(int i=0; i<size; i++) {
            Text t = new Text();
            t.readFields(in);
            values.add(t);
        }
    }
}

您应该向其中添加一些辅助类,以便向 Set 中添加元素。

我也看不到您在 map 方法中清除 Set 的位置。如果不清除它,每次调用 map 方法时它可能会变得越来越大。

请参阅Hadoop ArrayWritable供引用。

关于java - 在 Hadoop 中设置可写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37704049/

相关文章:

java - Mapreduce Hbase 文件未找到异常

java.lang.ClassCastException : class org. json.JSONObject 在 MapReduce 程序中

java - 按下android中的图像按钮并按顺序获取数组中的随机图像

java - 如何在java swing中垂直对齐按钮

hadoop - 如何将 Postgres "Text"数据类型加载到 HIVE 中

hadoop - 无法使用 YARN 运行 MapReduce 作业

java - 与java协调变化

java - IncationTargetException 尝试创建服务类的新实例

java - 与 MapClass 相关的 Hadoop ClassNotFoundException

hadoop - 打开透视图 org apache hadoop eclipse 透视图时出错