text.readfields()与text.readstring()之间的区别

lsmepo6l  于 2021-06-01  发布在  Hadoop
关注(0)|答案(0)|浏览(365)

我试图理解 Text.readFields()Text.readString()Text 班级。
从javadoc来看,还不是很清楚。唯一的一点提示可能是关于这个 Text.readString() 一定要执行utf-8编码,这在实际实现中很明显

  1. 467 /**Read a UTF8 encoded string with a maximum size
  2. 468 */
  3. 469 public static String readString(DataInput in, int maxLength)
  4. 470 throws IOException {
  5. 471 int length = WritableUtils.readVIntInRange(in, 0, maxLength);
  6. 472 byte [] bytes = new byte[length];
  7. 473 in.readFully(bytes, 0, length);
  8. 474 return decode(bytes);
  9. 475 }
  10. 294 public void readFields(DataInput in, int maxLength) throws IOException {
  11. 295 int newLength = WritableUtils.readVInt(in);
  12. 296 if (newLength < 0) {
  13. 297 throw new IOException("tried to deserialize " + newLength +
  14. 298 " bytes of data! newLength must be non-negative.");
  15. 299 } else if (newLength >= maxLength) {
  16. 300 throw new IOException("tried to deserialize " + newLength +
  17. 301 " bytes of data, but maxLength = " + maxLength);
  18. 302 }
  19. 303 readWithKnownLength(in, newLength);
  20. 304 }

另一个区别在于方法的签名。 readString() 是静态的,而另一个不是。
现在我有以下疑问:
我还缺什么吗?
据我所知,字符串遵循javaapi中的utf编码标准。什么情况下 readString() 以及 readFields() 有区别吗?换言之,我什么时候应该用一个而不是另一个。
为什么一个是静态的而另一个不是?它有什么区别,什么时候一个应该比另一个在这方面使用 Text 类上下文(类与对象级方法)?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题