(This is 3rd attempt to send this mail. Sorry if this mail is a duplicate)
Hi Ted,
I've made 2 JUnit test classes.
Both have one failed test.
-
HBaseAddColumnWithColumnRangeFilterTest1.testAddColumnWithColumnRangeFilter():
Expected: 10, Actual 1
-
HBaseAddColumnWithColumnRangeFilterTest2.testAddColumnWithColumnRangeFilter():
Result is empty
Since it seems that a mail with attachment is being rejected by the mailing
server, and I'm somewhat in a hurry, I'm pasting the unit test code here.
---------------------------------------------
HBaseAddColumnWithColumnRangeFilterTest1.java
---------------------------------------------
package com.test.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnRangeFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class HBaseAddColumnWithColumnRangeFilterTest1
{
@Before
public void setUp() throws Exception
{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor tableDesc = new
HTableDescriptor(TableName.valueOf(TestTableName));
HColumnDescriptor colDesc = new HColumnDescriptor(ColumnFamilyName);
tableDesc.addFamily(colDesc);
admin.createTable(tableDesc);
try (HTable table = new HTable(conf, TestTableName))
{
byte[] content = Bytes.toBytes("content");
Put p = new Put(RowKey);
p.add(ColumnFamilyNameBytes, FirstColumnNameBytes, content);
for (byte i = 0; i < 10; i++)
{
byte[] columnNameBytes = new byte[]{i};
p.add(ColumnFamilyNameBytes, columnNameBytes, content);
}
table.put(p);
}
}
@After
public void tearDown() throws Exception
{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(TestTableName);
admin.deleteTable(TestTableName);
}
@Test
public void testAddColumn() throws IOException
{
try (HTable table = new HTable(HBaseConfiguration.create(),
TestTableName))
{
Get g = new Get(RowKey);
g.addColumn(ColumnFamilyNameBytes, FirstColumnNameBytes);
Result r = table.get(g);
Assert.assertFalse("Result should not be empty", r.isEmpty());
Assert.assertEquals("Result cell count should match", 1,
r.rawCells().length);
}
}
@Test
public void testColumnRangeFilter() throws IOException
{
try (HTable table = new HTable(HBaseConfiguration.create(),
TestTableName))
{
Get g = new Get(RowKey);
g.setFilter(new ColumnRangeFilter(new byte[]{(byte)0}, false,
Bytes.toBytes("~"), false)); // includes the first column
Result r = table.get(g);
Assert.assertFalse("Result should not be empty", r.isEmpty());
Assert.assertEquals("Result cell count should match", 10,
r.rawCells().length);
}
}
@Test
public void testAddColumnWithColumnRangeFilter() throws IOException
{
try (HTable table = new HTable(HBaseConfiguration.create(),
TestTableName))
{
Get g = new Get(RowKey);
g.addColumn(ColumnFamilyNameBytes, FirstColumnNameBytes); //
should be redundant...
g.setFilter(new ColumnRangeFilter(new byte[]{(byte)0}, false,
Bytes.toBytes("~"), false)); // ...since this includes the
first column
Result r = table.get(g);
Assert.assertFalse("Result should not be empty", r.isEmpty());
Assert.assertEquals("Result cell count should match", 10,
r.rawCells().length);
}
}
static final String TestTableName = "AddColumnWithColumnRangeFilterTest";
static final String ColumnFamilyName = "f";
static final byte[] ColumnFamilyNameBytes = Bytes.toBytes(ColumnFamilyName);
static final byte[] RowKey = Bytes.toBytes("1234");
static final byte[] FirstColumnNameBytes = Bytes.toBytes("fc");
}
---------------------------------------------
---------------------------------------------
HBaseAddColumnWithColumnRangeFilterTest2.java
---------------------------------------------
package com.test.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnRangeFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import javax.print.attribute.standard.MediaSize;
import java.io.IOException;
public class HBaseAddColumnWithColumnRangeFilterTest2
{
@Before
public void setUp() throws Exception
{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor tableDesc = new
HTableDescriptor(TableName.valueOf(TestTableName));
HColumnDescriptor colDesc = new HColumnDescriptor(ColumnFamilyName);
tableDesc.addFamily(colDesc);
admin.createTable(tableDesc);
try (HTable table = new HTable(conf, TestTableName))
{
byte[] content = Bytes.toBytes("content");
Put p = new Put(RowKey);
p.add(ColumnFamilyNameBytes, FirstColumnNameBytes, content);
for (int i = 0; i < 10; i++)
{
byte[] columnNameBytes = Bytes.toBytes(OtherColumnNamePrefix +
String.format(".%d", i));
p.add(ColumnFamilyNameBytes, columnNameBytes, content);
}
table.put(p);
}
}
@After
public void tearDown() throws Exception
{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(TestTableName);
admin.deleteTable(TestTableName);
}
@Test
public void testAddColumn() throws IOException
{
try (HTable table = new HTable(HBaseConfiguration.create(),
TestTableName))
{
Get g = new Get(RowKey);
g.addColumn(ColumnFamilyNameBytes, FirstColumnNameBytes);
Result r = table.get(g);
Assert.assertFalse("Result should not be empty", r.isEmpty());
Assert.assertEquals("Result cell count should match", 1,
r.rawCells().length);
}
}
@Test
public void testColumnRangeFilter() throws IOException
{
try (HTable table = new HTable(HBaseConfiguration.create(),
TestTableName))
{
Get g = new Get(RowKey);
// should include only the OtherColumns
g.setFilter(new
ColumnRangeFilter(Bytes.toBytes(OtherColumnNamePrefix), false,
Bytes.toBytes(OtherColumnNamePrefix + "~"), false));
Result r = table.get(g);
Assert.assertFalse("Result should not be empty", r.isEmpty());
Assert.assertEquals("Result cell count should match", 10,
r.rawCells().length);
}
}
@Test
public void testAddColumnWithColumnRangeFilter() throws IOException
{
try (HTable table = new HTable(HBaseConfiguration.create(),
TestTableName))
{
Get g = new Get(RowKey);
g.addColumn(ColumnFamilyNameBytes, FirstColumnNameBytes);
g.setFilter(new
ColumnRangeFilter(Bytes.toBytes(OtherColumnNamePrefix), false,
Bytes.toBytes(OtherColumnNamePrefix + "~"), false));
Result r = table.get(g);
Assert.assertFalse("Result should not be empty", r.isEmpty());
Assert.assertEquals("Result cell count should match", 11,
r.rawCells().length);
}
}
static final String TestTableName = "AddColumnWithColumnRangeFilterTest";
static final String ColumnFamilyName = "f";
static final byte[] ColumnFamilyNameBytes = Bytes.toBytes(ColumnFamilyName);
static final byte[] RowKey = Bytes.toBytes("1234");
static final byte[] FirstColumnNameBytes = Bytes.toBytes("fc");
static final String OtherColumnNamePrefix = "oc";
}
----------------------------------------
If the tests have problems, please let me know.
Thanks.
-----Original Message-----
From: Ted Yu [mailto:[email protected]]
Sent: Thursday, January 15, 2015 6:59 PM
To: [email protected]
Subject: Re: Get addColumn + ColumnRangeFilter
Can you write a unit test which shows this behavior?
Thanks
> On Jan 14, 2015, at 9:09 PM, Taeyun Kim <[email protected]>
> wrote:
>
> Hi,
>
>
>
> I have a situation that both Get.addColumn() and Get.setFilter(new
> ColumnRangeFilter(…)) needed to Get.
>
> The source code snippet is as follows:
>
>
>
> Get g = new Get(getRowKey(lfileId));
>
> g.addColumn(Schema.ColumnFamilyNameBytes, MetaColumnNameBytes);
>
> g.setFilter(new ColumnRangeFilter(Bytes.toBytes(name), false,
>
> Bytes.toBytes(name + "~"), false));
>
> Result r = table.get(g);
>
>
>
> if (r.isEmpty())
>
> throw new FileNotFoundException(
>
> String.format("%d:%d:%s", projectId, lfileId, name));
>
>
>
> When g.addColumn() is commented out, the Result is not empty, while
> with g.addColumn the Result is empty(FileNotFoundException is thrown).
>
> Is it illegal to use both methods?
>
>
>
> BTW, ther version of HBase used is 0.98. (Hortonworks HDP 2.1)
>
>
>
> Thanks.