You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-user@hadoop.apache.org by Jason Grey <ja...@gmail.com> on 2007/11/21 17:27:23 UTC
Text and/or ImmutableBytesWritable issue?
Can anyone explain why "testTextToBytes" doesn't assert and
"testStringToBytes" does?
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.io.Text;
import junit.framework.TestCase;
public class TestImmutableBytesWritable extends TestCase {
public void testTextToBytes(){
Text input = new Text("this is a test.");
ImmutableBytesWritable bytes =
new ImmutableBytesWritable( input.getBytes() );
Text output = new Text( bytes.get() );
assertEquals(input, output);
}
public void testStringToBytes(){
String input = "this is a test.";
ImmutableBytesWritable bytes =
new ImmutableBytesWritable( input.getBytes() );
String output = new String( bytes.get() );
assertEquals(input, output);
}
}
If I inspect the objects during debugging at the point of the assert I
see the following:
* input
bytes = [116, 104, 105, 115, 32, 105
, 115, 32, 97, 32, 116, 101
, 115, 116, 46, 0]
length = 15
* bytes = [116, 104, 105, 115, 32, 105
, 115, 32, 97, 32, 116, 101
, 115, 116, 46, 0]
* output
bytes = [116, 104, 105, 115, 32, 105
, 115, 32, 97, 32, 116, 101
, 115, 116, 46, 0]
length = 16
The length property appears to be off between the two Text objects, but
all the data is correct... any help would be greatly appreciated.
Thanks
-jg-
RE: Text and/or ImmutableBytesWritable issue?
Posted by Jim Kellerman <ji...@powerset.com>.
Text objects typically contain more bytes than are actually in use.
If you were to use the alternate constructor for ImmutableBytesWritable:
new ImmutableBytesWritable(input.getBytes(), 0, input.getLength());
the test will pass.
One more note: Relying on the default encoding being the same for
Strings may work on any single machine but if one machine has a
default encoding of EN_US and another's is UTF-8, passing an
ImmutableBytesWritable from one machine to another will result in
the String decoding failing. For this reason, we always specify
an encoding for String.getBytes and in the String constructor:
new ImmutableBytesWritable("this is a string".getBytes("UTF-8"))
and
new String(ibw.getBytes(), "UTF-8")
---
Jim Kellerman, Senior Engineer; Powerset
> -----Original Message-----
> From: Jason Grey [mailto:jason.grey.work@gmail.com]
> Sent: Wednesday, November 21, 2007 8:27 AM
> To: hadoop-user@lucene.apache.org
> Subject: Text and/or ImmutableBytesWritable issue?
>
> Can anyone explain why "testTextToBytes" doesn't assert and
> "testStringToBytes" does?
>
>
> import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
> import org.apache.hadoop.io.Text;
>
> import junit.framework.TestCase;
>
> public class TestImmutableBytesWritable extends TestCase {
>
> public void testTextToBytes(){
>
> Text input = new Text("this is a test.");
>
> ImmutableBytesWritable bytes =
> new ImmutableBytesWritable(
> input.getBytes() );
>
> Text output = new Text( bytes.get() );
>
> assertEquals(input, output);
>
> }
>
> public void testStringToBytes(){
>
> String input = "this is a test.";
>
> ImmutableBytesWritable bytes =
> new ImmutableBytesWritable(
> input.getBytes() );
>
> String output = new String( bytes.get() );
>
> assertEquals(input, output);
>
> }
> }
>
>
> If I inspect the objects during debugging at the point of the
> assert I see the following:
>
> * input
> bytes = [116, 104, 105, 115, 32, 105
> , 115, 32, 97, 32, 116, 101
> , 115, 116, 46, 0]
> length = 15
>
> * bytes = [116, 104, 105, 115, 32, 105
> , 115, 32, 97, 32, 116, 101
> , 115, 116, 46, 0]
>
> * output
> bytes = [116, 104, 105, 115, 32, 105
> , 115, 32, 97, 32, 116, 101
> , 115, 116, 46, 0]
> length = 16
>
> The length property appears to be off between the two Text
> objects, but all the data is correct... any help would be
> greatly appreciated.
>
> Thanks
>
> -jg-
>
>
RE: Text and/or ImmutableBytesWritable issue?
Posted by Jim Kellerman <ji...@powerset.com>.
Well, it depends on how the Text object is initialized. If it is initialized
with a String, it sets its internal length to the length of the string.
---
Jim Kellerman, Senior Engineer; Powerset
> -----Original Message-----
> From: stack [mailto:stack@duboce.net]
> Sent: Wednesday, November 21, 2007 9:04 AM
> To: hadoop-user@lucene.apache.org
> Subject: Re: Text and/or ImmutableBytesWritable issue?
>
> What Jim just said, but it looks to me like Text is doing the
> wrong thing. When you ask it its length, it returns the byte
> buffer capacity rather than how many bytes are in use. It
> says length is 16 but there are only 15 characters in your
> test string, UTF-8'd or not.
>
> St.Ack
>
>
> Jason Grey wrote:
> > Can anyone explain why "testTextToBytes" doesn't assert and
> > "testStringToBytes" does?
> >
> >
> > import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
> > import org.apache.hadoop.io.Text;
> >
> > import junit.framework.TestCase;
> >
> > public class TestImmutableBytesWritable extends TestCase {
> >
> > public void testTextToBytes(){
> >
> > Text input = new Text("this is a test.");
> >
> > ImmutableBytesWritable bytes =
> > new ImmutableBytesWritable(
> input.getBytes() );
> >
> > Text output = new Text( bytes.get() );
> >
> > assertEquals(input, output);
> >
> > }
> >
> > public void testStringToBytes(){
> >
> > String input = "this is a test.";
> >
> > ImmutableBytesWritable bytes =
> > new ImmutableBytesWritable(
> input.getBytes() );
> >
> > String output = new String( bytes.get() );
> >
> > assertEquals(input, output);
> >
> > }
> > }
> >
> >
> > If I inspect the objects during debugging at the point of
> the assert I
> > see the following:
> >
> > * input
> > bytes = [116, 104, 105, 115, 32, 105
> > , 115, 32, 97, 32, 116, 101
> > , 115, 116, 46, 0]
> > length = 15
> >
> > * bytes = [116, 104, 105, 115, 32, 105
> > , 115, 32, 97, 32, 116, 101
> > , 115, 116, 46, 0]
> >
> > * output
> > bytes = [116, 104, 105, 115, 32, 105
> > , 115, 32, 97, 32, 116, 101
> > , 115, 116, 46, 0]
> > length = 16
> >
> > The length property appears to be off between the two Text objects,
> > but all the data is correct... any help would be greatly
> appreciated.
> >
> > Thanks
> >
> > -jg-
> >
> >
>
>
Re: Text and/or ImmutableBytesWritable issue?
Posted by stack <st...@duboce.net>.
On review, after a kick from JK, I'm mistaken in the below. I wasn't
using Text.getLength, I was doing Text.getBytes().length. Please ignore
below.
St.Ack
stack wrote:
> What Jim just said, but it looks to me like Text is doing the wrong
> thing. When you ask it its length, it returns the byte buffer
> capacity rather than how many bytes are in use. It says length is 16
> but there are only 15 characters in your test string, UTF-8'd or not.
>
> St.Ack
>
>
> Jason Grey wrote:
>> Can anyone explain why "testTextToBytes" doesn't assert and
>> "testStringToBytes" does?
>>
>>
>> import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
>> import org.apache.hadoop.io.Text;
>>
>> import junit.framework.TestCase;
>>
>> public class TestImmutableBytesWritable extends TestCase {
>>
>> public void testTextToBytes(){
>>
>> Text input = new Text("this is a test.");
>>
>> ImmutableBytesWritable bytes =
>> new ImmutableBytesWritable( input.getBytes() );
>>
>> Text output = new Text( bytes.get() );
>>
>> assertEquals(input, output);
>>
>> }
>>
>> public void testStringToBytes(){
>>
>> String input = "this is a test.";
>>
>> ImmutableBytesWritable bytes =
>> new ImmutableBytesWritable( input.getBytes() );
>>
>> String output = new String( bytes.get() );
>>
>> assertEquals(input, output);
>>
>> }
>> }
>>
>>
>> If I inspect the objects during debugging at the point of the assert I
>> see the following:
>>
>> * input
>> bytes = [116, 104, 105, 115, 32, 105
>> , 115, 32, 97, 32, 116, 101
>> , 115, 116, 46, 0]
>> length = 15
>>
>> * bytes = [116, 104, 105, 115, 32, 105
>> , 115, 32, 97, 32, 116, 101
>> , 115, 116, 46, 0]
>>
>> * output
>> bytes = [116, 104, 105, 115, 32, 105
>> , 115, 32, 97, 32, 116, 101
>> , 115, 116, 46, 0]
>> length = 16
>>
>> The length property appears to be off between the two Text objects, but
>> all the data is correct... any help would be greatly appreciated.
>>
>> Thanks
>>
>> -jg-
>>
>>
>
Re: Text and/or ImmutableBytesWritable issue?
Posted by stack <st...@duboce.net>.
What Jim just said, but it looks to me like Text is doing the wrong
thing. When you ask it its length, it returns the byte buffer capacity
rather than how many bytes are in use. It says length is 16 but there
are only 15 characters in your test string, UTF-8'd or not.
St.Ack
Jason Grey wrote:
> Can anyone explain why "testTextToBytes" doesn't assert and
> "testStringToBytes" does?
>
>
> import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
> import org.apache.hadoop.io.Text;
>
> import junit.framework.TestCase;
>
> public class TestImmutableBytesWritable extends TestCase {
>
> public void testTextToBytes(){
>
> Text input = new Text("this is a test.");
>
> ImmutableBytesWritable bytes =
> new ImmutableBytesWritable( input.getBytes() );
>
> Text output = new Text( bytes.get() );
>
> assertEquals(input, output);
>
> }
>
> public void testStringToBytes(){
>
> String input = "this is a test.";
>
> ImmutableBytesWritable bytes =
> new ImmutableBytesWritable( input.getBytes() );
>
> String output = new String( bytes.get() );
>
> assertEquals(input, output);
>
> }
> }
>
>
> If I inspect the objects during debugging at the point of the assert I
> see the following:
>
> * input
> bytes = [116, 104, 105, 115, 32, 105
> , 115, 32, 97, 32, 116, 101
> , 115, 116, 46, 0]
> length = 15
>
> * bytes = [116, 104, 105, 115, 32, 105
> , 115, 32, 97, 32, 116, 101
> , 115, 116, 46, 0]
>
> * output
> bytes = [116, 104, 105, 115, 32, 105
> , 115, 32, 97, 32, 116, 101
> , 115, 116, 46, 0]
> length = 16
>
> The length property appears to be off between the two Text objects, but
> all the data is correct... any help would be greatly appreciated.
>
> Thanks
>
> -jg-
>
>