View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.filter;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.hbase.Cell;
33  import org.apache.hadoop.hbase.testclassification.MediumTests;
34  import org.apache.hadoop.hbase.client.Put;
35  import org.apache.hadoop.hbase.client.Result;
36  import org.apache.hadoop.hbase.client.ResultScanner;
37  import org.apache.hadoop.hbase.client.Scan;
38  import org.apache.hadoop.hbase.client.Table;
39  import org.apache.hadoop.hbase.util.Bytes;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  /**
45   * Test if Filter is incompatible with scan-limits
46   */
47  @Category(MediumTests.class)
48  public class TestFilterWithScanLimits extends FilterTestingCluster {
49    private static final Log LOG = LogFactory
50        .getLog(TestFilterWithScanLimits.class);
51  
52    private static final String tableName = "scanWithLimit";
53    private static final String columnFamily = "f1";
54  
55    @Test
56    public void testScanWithLimit() {
57      int kv_number = 0;
58      try {
59        Scan scan = new Scan();
60        // set batch number as 2, which means each Result should contain 2 KVs at most
61        scan.setBatch(2);
62        SingleColumnValueFilter filter = new SingleColumnValueFilter(
63            Bytes.toBytes(columnFamily), Bytes.toBytes("c5"),
64            CompareFilter.CompareOp.EQUAL, new SubstringComparator("2_c5"));
65  
66        // add filter after batch defined
67        scan.setFilter(filter);
68        Table table = openTable(tableName);
69        ResultScanner scanner = table.getScanner(scan);
70        // Expect to get following row
71        // row2 => <f1:c1, 2_c1>, <f1:c2, 2_c2>,
72        // row2 => <f1:c3, 2_c3>, <f1:c4, 2_c4>,
73        // row2 => <f1:c5, 2_c5>
74  
75        for (Result result : scanner) {
76          for (Cell kv : result.listCells()) {
77            kv_number++;
78            LOG.debug(kv_number + ". kv: " + kv);
79          }
80        }
81  
82        scanner.close();
83        table.close();
84      } catch (Exception e) {
85        // no correct result is expected
86        assertNotNull("No IncompatibleFilterException catched", e);
87      }
88      LOG.debug("check the fetched kv number");
89      assertEquals("We should not get result(s) returned.", 0, kv_number);
90    }
91  
92    @BeforeClass
93    public static void prepareData() {
94      try {
95        createTable(tableName, columnFamily);
96        Table table = openTable(tableName);
97        List<Put> puts = new ArrayList<Put>();
98  
99        // row1 => <f1:c1, 1_c1>, <f1:c2, 1_c2>, <f1:c3, 1_c3>, <f1:c4,1_c4>,
100       // <f1:c5, 1_c5>
101       // row2 => <f1:c1, 2_c1>, <f1:c2, 2_c2>, <f1:c3, 2_c3>, <f1:c4,2_c4>,
102       // <f1:c5, 2_c5>
103       for (int i = 1; i < 4; i++) {
104         Put put = new Put(Bytes.toBytes("row" + i));
105         for (int j = 1; j < 6; j++) {
106           put.add(Bytes.toBytes("f1"), Bytes.toBytes("c" + j),
107               Bytes.toBytes(i + "_c" + j));
108         }
109         puts.add(put);
110       }
111 
112       table.put(puts);
113       table.close();
114     } catch (IOException e) {
115       assertNull("Exception found while putting data into table", e);
116     }
117   }
118 
119 }