HBASE-5428 Allow for custom filters to be registered within the Thrift interface
git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1245774 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0dd3c22706
commit
9684065bec
|
@ -23,12 +23,10 @@ import java.lang.reflect.InvocationTargetException;
|
|||
import java.lang.reflect.Method;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EmptyStackException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
import java.util.*;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.hadoop.hbase.KeyValue;
|
||||
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
|
||||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
|
@ -43,6 +41,7 @@ import org.apache.hadoop.hbase.util.Bytes;
|
|||
* Filter Language can be found at: https://issues.apache.org/jira/browse/HBASE-4176
|
||||
*/
|
||||
public class ParseFilter {
|
||||
private static final Log LOG = LogFactory.getLog(ParseFilter.class);
|
||||
|
||||
private static HashMap<ByteBuffer, Integer> operatorPrecedenceHashMap;
|
||||
private static HashMap<String, String> filterHashMap;
|
||||
|
@ -832,4 +831,26 @@ public class ParseFilter {
|
|||
public Set<String> getSupportedFilters () {
|
||||
return filterHashMap.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all known filters
|
||||
* @return an unmodifiable map of filters
|
||||
*/
|
||||
public static Map<String, String> getAllFilters() {
|
||||
return Collections.unmodifiableMap(filterHashMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new filter with the parser. If the filter is already registered,
|
||||
* an IllegalArgumentException will be thrown.
|
||||
*
|
||||
* @param name a name for the filter
|
||||
* @param filterClass fully qualified class name
|
||||
*/
|
||||
public static void registerFilter(String name, String filterClass) {
|
||||
if(LOG.isInfoEnabled())
|
||||
LOG.info("Registering new filter " + name);
|
||||
|
||||
filterHashMap.put(name, filterClass);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -365,6 +365,8 @@ public class ThriftServerRunner implements Runnable {
|
|||
User.login(conf, "hbase.thrift.keytab.file",
|
||||
"hbase.thrift.kerberos.principal", machineName);
|
||||
}
|
||||
|
||||
registerFilters(conf);
|
||||
}
|
||||
|
||||
ExecutorService createExecutor(BlockingQueue<Runnable> callQueue,
|
||||
|
@ -1340,4 +1342,18 @@ public class ThriftServerRunner implements Runnable {
|
|||
op.setAttribute(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static void registerFilters(Configuration conf) {
|
||||
String[] filters = conf.getStrings("hbase.thrift.filters");
|
||||
if(filters != null) {
|
||||
for(String filterClass: filters) {
|
||||
String[] filterPart = filterClass.split(":");
|
||||
if(filterPart.length != 2) {
|
||||
LOG.warn("Invalid filter specification " + filterClass + " - skipping");
|
||||
} else {
|
||||
ParseFilter.registerFilter(filterPart[0], filterPart[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -650,6 +650,13 @@ public class TestParseFilter {
|
|||
FirstKeyOnlyFilter firstKeyOnlyFilter =
|
||||
doTestFilter(filterString, FirstKeyOnlyFilter.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterFilter() {
|
||||
ParseFilter.registerFilter("MyFilter", "some.class");
|
||||
|
||||
assertTrue(f.getSupportedFilters().contains("MyFilter"));
|
||||
}
|
||||
|
||||
private <T extends Filter> T doTestFilter(String filterString, Class<T> clazz) throws IOException {
|
||||
byte [] filterStringAsByteArray = Bytes.toBytes(filterString);
|
||||
|
|
|
@ -30,6 +30,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.hadoop.hbase.filter.ParseFilter;
|
||||
import org.junit.experimental.categories.Category;
|
||||
import org.junit.Test;
|
||||
import org.junit.BeforeClass;
|
||||
|
@ -102,6 +103,7 @@ public class TestThriftServer {
|
|||
doTestTableTimestampsAndColumns();
|
||||
doTestTableScanners();
|
||||
doTestGetTableRegions();
|
||||
doTestFilterRegistration();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -428,6 +430,18 @@ public class TestThriftServer {
|
|||
assertEquals("non-existing table should have 0 region, " +
|
||||
"but found " + regionCount, regionCount, 0);
|
||||
}
|
||||
|
||||
public void doTestFilterRegistration() throws Exception {
|
||||
Configuration conf = UTIL.getConfiguration();
|
||||
|
||||
conf.set("hbase.thrift.filters", "MyFilter:filterclass");
|
||||
|
||||
ThriftServerRunner.registerFilters(conf);
|
||||
|
||||
Map<String, String> registeredFilters = ParseFilter.getAllFilters();
|
||||
|
||||
assertEquals("filterclass", registeredFilters.get("MyFilter"));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue