2013-12-09 35 views
1

我是用Avro編寫Hadoop MapReduce的初學者,並且不清楚傳遞給map/reduce方法與String之間的區別是什麼,CharSequence還是Utf8?MapReduce與Java中的Avro:字符串與CharSequence與Utf8數據類型

如果字符串只是簡單的像「hello world」那麼該怎麼辦?

使用的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)); 
     } 

任何幫助表示讚賞!

回答

4

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是最靈活的解決方案,因爲它允許您通過字符串的更多的表示不是專門的實現,它是由您根據您的需要可用的實現選擇。

+4

與此問題有關的警告詞。你可能會想要堅持CharSequence的一個特定實現,比如String,而不是使用通用接口。不同CharSequence實現的hashcode不一定匹配,這可能會導致問題。請參閱http://stackoverflow.com/questions/19728853/apache-avro-map-uses-charsequence-as-key –

+0

@AlexA。好點,它會破壞map和reduce之間的分區。 –