Uploaded image for project: 'Hadoop Common'
  1. Hadoop Common
  2. HADOOP-17141

Add Capability To Get Text Length

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 3.4.0
    • 3.4.0
    • common, io
    • None
    • Reviewed

    Description

      The Hadoop Text class contains an array of byte which contain a UTF-8 encoded string. However, there is no way to quickly get the length of that string. One can get the number of bytes in the byte array, but to figure out the length of the String, it needs to be decoded first. In this simple example, sorting the Text objects by String length, the String needs to be decoded from the byte array repeatedly. This was brought to my attention based on HIVE-23870.

        public static void main(String[] args) {
          List<Text> list = Arrays.asList(new Text("1"), new Text("22"), new Text("333"));
          list.sort((Text t1, Text t2) -> t1.toString().length() - t2.toString().length());
        }
      

      Also helpful if I want to check the last letter in the Text object repeatedly:

          Text t = new Text("4444");
          System.out.println(t.charAt(t.toString().length() - 1));
      

      Attachments

        Issue Links

          Activity

            People

              belugabehr David Mollitor
              belugabehr David Mollitor
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: