Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:05:11+00:00 2026-06-02T18:05:11+00:00

I’m getting some behavior from the Text constructors that don’t really make any sense.

  • 0

I’m getting some behavior from the Text constructors that don’t really make any sense. Basically, if I construct a Text object from a String, it is not equal to another Text object that I constructed from bytes, even though getBytes() returns the same value for both objects.

So we get weird stuff like this :

//This succeeds
assertEquals(new Text("ACTACGACCA_0"), new Text("ACTACGACCA_0")); 
//This succeeds
assertEquals((new Text("ACTACGACCA_0")).getBytes(), (new Text("ACTACGACCA_0")).getBytes()); 
//This fails.  Why?
assertEquals(new Text((new Text("ACTACGACCA_0")).getBytes()), new Text("ACTACGACCA_0"));  

This manifests when I’m trying to access a hashmap. Here, I’m trying to do a lookup based on a value returned by org.apache.hadoop.hbase.KeyValue.getRow() :

//This succeeds
assertEquals((new Text("ACTACGACCA_0")).getBytes(), keyValue.getRow()); 
//This returns a value
hashMap.get(new Text("ACTACGACCA_0"));  
//This returns null.  Why?
hashMap.get(new Text(keyValue.getRow()));  

So what’s going on here, and how do I deal with it? Does this have something to do with encoding?

UPDATE : PROBLEM SOLVED

Thanks to Chris for pointing me in the right direction with this. So, a little background : the keyValue object is captured (using a Mockito ArgumentCaptor) from a call to htable.put(). Basically, I had this chunk of code :

byte[] keyBytes = matchRow.getKey().getBytes();
RowLock rowLock = hTable.lockRow(keyBytes);                         

Get get = new Get(keyBytes, rowLock);               
SetWritable<Text> toWrite = new SetWritable<Text>(Text.class);
toWrite.getValues().addAll(matchRow.getMatches(hTable, get));

Put put = new Put(keyBytes, rowLock);
put.add(Bytes.toBytes(MatchesByHaplotype.MATCHING_COLUMN_FAMILY), Bytes.toBytes(MatchesByHaplotype.UID_QUALIFIER), 
        SERIALIZATION_HELPER.serialize(toWrite));               
hTable.put(put);

where matchRow.getKey() returns a text object. You see the problem here? I was adding all the bytes, including the invalid ones. So I created a nice helper function that does this :

public byte[] getValidBytes(Text text) {
    return Arrays.copyOf(text.getBytes(), text.getLength());
}

And changed the first line of that block to this :

byte[] keyBytes = SERIALIZATION_HELPER.getValidBytes(matchRow.getKey());

Problem solved! In retrospect : wow, what a nasty bug! I think what it comes down to is that the behavior of Text.getBytes() is very n00b-unfriendly. Not only does it return something that you may not expect (non-valid bytes), the Text object doesn’t have a function to return only the valid bytes! You would think this would be a common use-case. Maybe they’ll add this in the future?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:05:15+00:00Added an answer on June 2, 2026 at 6:05 pm

    For the same reason that the following fails:

    Assert.assertEquals((new Text("ACTACGACCA_0")).getLength(), (new Text("ACTACGACCA_0")).getBytes().length);
    

    getBytes() returns the backing byte array, but according to the API, the bytes are only valid upto Text.getLength();

    • Text.getBytes()

    Yes, this does have to do with encoding – the CharsetEncoder.encode method uses a ByteBuffer whose size is initially allocated to 12 * 1.1 bytes (13) in length, but the actual valid number of bytes is still only 12 (as you are using solely ASCII characters).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.