cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5819] Prototyping HBase visitor, test disabled for now
Date Tue, 24 Jun 2014 11:56:50 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 727d35096 -> 35b9209c0


[CXF-5819] Prototyping HBase visitor, test disabled for now


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/35b9209c
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/35b9209c
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/35b9209c

Branch: refs/heads/master
Commit: 35b9209c04154bb00f1168cbeaa94314af070609
Parents: 727d350
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Tue Jun 24 12:56:12 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Tue Jun 24 12:56:12 2014 +0100

----------------------------------------------------------------------
 rt/rs/extensions/search/pom.xml                 |  63 ++++++++
 .../ext/search/hbase/HBaseQueryVisitor.java     | 148 +++++++++++++++++++
 .../ext/search/hbase/HBaseVisitorTest.java      |  92 ++++++++++++
 3 files changed, 303 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/35b9209c/rt/rs/extensions/search/pom.xml
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/pom.xml b/rt/rs/extensions/search/pom.xml
index 7dd664c..5fb37d6 100644
--- a/rt/rs/extensions/search/pom.xml
+++ b/rt/rs/extensions/search/pom.xml
@@ -76,6 +76,69 @@
             <optional>true</optional>
         </dependency>
         <dependency>
+           <groupId>org.apache.hbase</groupId>
+           <artifactId>hbase-client</artifactId>
+           <version>0.98.3-hadoop2</version>
+           <optional>true</optional>
+           <scope>provided</scope>
+           <exclusions>
+                <exclusion>
+            	   <groupId>io.netty</groupId>
+            	   <artifactId>netty</artifactId>            	
+            	</exclusion>
+                <exclusion>
+            	   <groupId>com.sun.jersey</groupId>
+            	   <artifactId>jersey-server</artifactId>            	
+            	</exclusion>
+                <exclusion>
+            	   <groupId>com.sun.jersey</groupId>
+            	   <artifactId>jersey-core</artifactId>            	
+            	</exclusion>
+                <exclusion>
+            	   <groupId>com.sun.jersey</groupId>
+            	   <artifactId>jersey-json</artifactId>            	
+            	</exclusion>
+                <exclusion>
+            	   <groupId>com.sun.jersey.contribs</groupId>
+            	   <artifactId>jersey-guice</artifactId>            	
+            	</exclusion>
+                <exclusion>
+            	   <groupId>com.google.inject</groupId>
+            	   <artifactId>guice</artifactId>            	
+            	</exclusion>
+                <exclusion>
+            	   <groupId>com.google.inject.extensions</groupId>
+            	   <artifactId>guice-servlet</artifactId>            	
+            	</exclusion>    
+                <exclusion>
+            	   <groupId>org.mortbay.jetty</groupId>
+            	   <artifactId>jetty</artifactId>            	
+            	</exclusion>
+                <exclusion>
+            	   <groupId>org.mortbay.jetty</groupId>
+            	   <artifactId>jetty-util</artifactId>            	
+            	</exclusion>
+                <exclusion>
+            	   <groupId>commons-httpclient</groupId>
+            	   <artifactId>commons-httpclient</artifactId>            	
+            	</exclusion>
+                <!--
+                <exclusion>
+            	   <groupId>org.codehaus.jackson</groupId>
+            	   <artifactId>jackson-mapper-asl</artifactId>            	
+            	</exclusion>
+                --> 
+           </exclusions>
+        </dependency>
+<!--
+        <dependency>
+           <groupId>io.netty</groupId>
+           <artifactId>netty</artifactId>
+           <version>3.9.1.Final</version>
+           <scope>test</scope>
+        </dependency> 
+-->
+        <dependency>
             <groupId>org.apache.tika</groupId>
             <artifactId>tika-parsers</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/cxf/blob/35b9209c/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/hbase/HBaseQueryVisitor.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/hbase/HBaseQueryVisitor.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/hbase/HBaseQueryVisitor.java
new file mode 100644
index 0000000..8d466e4
--- /dev/null
+++ b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/hbase/HBaseQueryVisitor.java
@@ -0,0 +1,148 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.ext.search.hbase;
+
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+
+import org.apache.cxf.jaxrs.ext.search.ConditionType;
+import org.apache.cxf.jaxrs.ext.search.PrimitiveStatement;
+import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+import org.apache.cxf.jaxrs.ext.search.visitor.AbstractSearchConditionVisitor;
+import org.apache.hadoop.hbase.filter.BinaryComparator;
+import org.apache.hadoop.hbase.filter.ByteArrayComparable;
+import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
+import org.apache.hadoop.hbase.filter.Filter;
+import org.apache.hadoop.hbase.filter.FilterList;
+import org.apache.hadoop.hbase.filter.RegexStringComparator;
+import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
+
+public class HBaseQueryVisitor<T> extends AbstractSearchConditionVisitor<T, Filter>
{
+
+    private Stack<List<Filter>> queryStack = new Stack<List<Filter>>();
+    private String family;
+    private Map<String, String> familyMap;
+    public HBaseQueryVisitor(String family) {
+        this(family, Collections.<String, String>emptyMap());
+    }
+    public HBaseQueryVisitor(String family, Map<String, String> fieldsMap) {
+        super(fieldsMap);
+        this.family = family;
+        queryStack.push(new ArrayList<Filter>());
+    }
+    public HBaseQueryVisitor(Map<String, String> familyMap) {
+        this(familyMap, Collections.<String, String>emptyMap());
+    }
+    public HBaseQueryVisitor(Map<String, String> familyMap,
+                             Map<String, String> fieldsMap) {
+        super(fieldsMap);
+        this.familyMap = familyMap;
+        queryStack.push(new ArrayList<Filter>());
+    }
+    
+    public void visit(SearchCondition<T> sc) {
+        PrimitiveStatement statement = sc.getStatement();
+        if (statement != null) {
+            if (statement.getProperty() != null) {
+                queryStack.peek().add(buildSimpleQuery(sc.getConditionType(), 
+                                         statement.getProperty(), 
+                                         statement.getValue()));
+            }
+        } else {
+            queryStack.push(new ArrayList<Filter>());
+            for (SearchCondition<T> condition : sc.getSearchConditions()) {
+                condition.accept(this);
+            }
+            boolean orCondition = sc.getConditionType() == ConditionType.OR;
+            List<Filter> queries = queryStack.pop();
+            queryStack.peek().add(createCompositeQuery(queries, orCondition));
+        }    
+    }
+
+    public Filter getQuery() {
+        List<Filter> queries = queryStack.peek();
+        return queries.isEmpty() ? null : queries.get(0);
+    }
+    
+    private Filter buildSimpleQuery(ConditionType ct, String name, Object value) {
+        name = super.getRealPropertyName(name);
+        validatePropertyValue(name, value);
+        Class<?> clazz = getPrimitiveFieldClass(name, value.getClass());
+        CompareOp compareOp = null;
+        boolean regexCompRequired = false;
+        switch (ct) {
+        case EQUALS:
+            compareOp = CompareOp.EQUAL;
+            regexCompRequired = String.class == clazz && value.toString().endsWith("*");
+            break;
+        case NOT_EQUALS:
+            compareOp = CompareOp.NOT_EQUAL;
+            regexCompRequired = String.class == clazz && value.toString().endsWith("*");
+            break;
+        case GREATER_THAN:
+            compareOp = CompareOp.GREATER;
+            break;
+        case GREATER_OR_EQUALS:
+            compareOp = CompareOp.GREATER_OR_EQUAL;
+            break;
+        case LESS_THAN:
+            compareOp = CompareOp.LESS;
+            break;
+        case LESS_OR_EQUALS:
+            compareOp = CompareOp.LESS_OR_EQUAL;
+            break;
+        default: 
+            break;
+        }
+        String qualifier = name;
+        String theFamily = family != null ? family : familyMap.get(qualifier);
+        ByteArrayComparable byteArrayComparable = regexCompRequired 
+            ? new RegexStringComparator(value.toString().replace("*", "."))
+            : new BinaryComparator(getBytes(value.toString()));
+        
+        Filter query = new SingleColumnValueFilter(getBytes(theFamily),
+                                                   getBytes(qualifier),
+                                                   compareOp,
+                                                   byteArrayComparable);
+        return query;
+    }
+    
+    private Filter createCompositeQuery(List<Filter> queries, boolean orCondition)
{
+        
+        FilterList.Operator oper = orCondition ? FilterList.Operator.MUST_PASS_ONE
+            : FilterList.Operator.MUST_PASS_ALL;
+        FilterList list = new FilterList(oper);
+        for (Filter query : queries) {
+            list.addFilter(query);
+        }
+        return list;
+    }
+    
+    private byte[] getBytes(String value) {
+        try {
+            return value.getBytes("UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/35b9209c/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/hbase/HBaseVisitorTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/hbase/HBaseVisitorTest.java
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/hbase/HBaseVisitorTest.java
new file mode 100644
index 0000000..ca4177d
--- /dev/null
+++ b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/hbase/HBaseVisitorTest.java
@@ -0,0 +1,92 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.ext.search.hbase;
+
+import org.apache.cxf.jaxrs.ext.search.SearchBean;
+import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+import org.apache.cxf.jaxrs.ext.search.fiql.FiqlParser;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.client.HConnection;
+import org.apache.hadoop.hbase.client.HConnectionManager;
+import org.apache.hadoop.hbase.client.HTableInterface;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.hadoop.hbase.client.ResultScanner;
+import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.filter.Filter;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class HBaseVisitorTest extends Assert {
+    public static final byte[] BOOK_FAMILY = "book".getBytes();
+    public static final byte[] NAME_QUALIFIER = "name".getBytes();
+    
+    HConnection connection;
+    HTableInterface table;
+    @Before
+    public void setUp() throws Exception {
+        try {
+            Configuration hBaseConfig =  HBaseConfiguration.create();
+            connection = HConnectionManager.createConnection(hBaseConfig);
+            table = connection.getTable("books");
+        } catch (Throwable t) {
+            t.printStackTrace();
+        }
+    }
+    
+    @Test
+    @Ignore("Enable as soon as it is understood how to run HBase tests in process")
+    public void testScanWithFilterVisitor() throws Exception {
+        Scan scan = new Scan();
+        
+        SearchCondition<SearchBean> sc = new FiqlParser<SearchBean>(SearchBean.class).parse("name==CXF");
+        HBaseQueryVisitor<SearchBean> visitor = new HBaseQueryVisitor<SearchBean>("book");
+        sc.accept(visitor);
+        Filter filter = visitor.getQuery();
+        scan.setFilter(filter);
+        ResultScanner rs = table.getScanner(scan);
+        try {
+            int count = 0;
+            for (Result r = rs.next(); r != null; r = rs.next()) {
+                assertEquals("row2", new String(r.getRow()));
+                assertEquals("CXF", new String(r.getValue(BOOK_FAMILY, NAME_QUALIFIER)));
+                count++;
+            }
+            assertEquals(1, count);
+        } finally {
+            rs.close();  
+        }
+    }
+    
+    @After
+    public void tearDown() throws Exception {
+        if (table != null) {
+            table.close();
+        }
+        if (connection != null) {
+            connection.close();
+        }
+        
+        
+    }
+}


Mime
View raw message