【问题标题】:MapReduce with Avro in Java: String vs CharSequence vs Utf8 data typesJava 中使用 Avro 的 MapReduce:String、CharSequence 和 Utf8 数据类型
【发布时间】:2013-12-26 20:14:11
【问题描述】:

我是使用 Avro 编写 Hadoop MapReduce 的初学者,我不清楚通过 String、CharSequence 或 Utf8 将 Pair 传递给 map/reduce 方法之间有什么区别?

如果字符串只是“hello world”之类的东西怎么办?

这里有一个简单的 map 方法例如,在这种情况下使用 CharSequence 作为输出键类型:

public void map(Pair<CharSequence, Integer> datum, AvroCollector<Pair<CharSequence, Integer>> collector, Reporter reporter) throws IOException {
            Integer number_one = new Integer(1);
            String output_key = "hello world";
            collector.collect(new Pair<CharSequence, Integer>(output_key, one));
        }

感谢任何帮助!

【问题讨论】:

    标签: java hadoop mapreduce avro


    【解决方案1】:

    CharSequence is an interface that "bundles" 大多数基于字符的实现,如StringBuilderStringBufferCharBufferString 和 Avro 中的Utf8

    String 是不可变的,这意味着您不能修改内部数据 - 您所做的每一次修改都会导致创建一个新的 String 对象。

    Utf8 on the other hand will allow you to modify its internal buffer ("mutable"),与使用String 实例相比,这将产生更少的垃圾。

    所以您可以说使用CharSequence 是最灵活的解决方案,因为它允许您传递比专门的实现更多的字符串表示,您可以根据需要从可用的实现中进行选择。

    【讨论】:

    • 与此问题相关的警告。您可能希望坚持使用 CharSequence 的一种特定实现,例如 String,而不是使用通用接口。不同 CharSequence 实现的哈希码不一定匹配,这可能会导致问题。参见例如stackoverflow.com/questions/19728853/…
    • @AlexA。好点,它会破坏 map 和 reduce 之间的分区。
    猜你喜欢
    • 2014-09-26
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多