View Javadoc

1   /*
2    * Copyright (c) 2010.  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  
21  package org.apache.hadoop.hbase.hbql.statement.select;
22  
23  import org.apache.hadoop.hbase.client.Get;
24  import org.apache.hadoop.hbase.client.HTable;
25  import org.apache.hadoop.hbase.client.Result;
26  import org.apache.hadoop.hbase.client.ResultScanner;
27  import org.apache.hadoop.hbase.hbql.client.HBqlException;
28  import org.apache.hadoop.hbase.hbql.mapping.Mapping;
29  import org.apache.hadoop.hbase.hbql.statement.args.WithArgs;
30  import org.apache.hadoop.hbase.hbql.util.Lists;
31  
32  import java.io.IOException;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  public class GetRequest implements RowRequest {
37  
38      final Get getValue;
39  
40      public GetRequest(final Get getValue) {
41          this.getValue = getValue;
42      }
43  
44      private Get getGetValue() {
45          return this.getValue;
46      }
47  
48      public ResultScanner getResultScanner(final Mapping mapping,
49                                            final WithArgs withArgs,
50                                            final HTable table) throws HBqlException {
51          try {
52              // We need to fake a ResultScanner with the Get result
53              final List<Result> resultList = Lists.newArrayList();
54              final Result result = table.get(this.getGetValue());
55  
56              if (result != null && !result.isEmpty())
57                  resultList.add(result);
58  
59              return new ResultScanner() {
60  
61                  public Result next() {
62                      return null;
63                  }
64  
65                  public Result[] next(final int nbRows) {
66                      return null;
67                  }
68  
69                  public Iterator<Result> iterator() {
70                      return resultList.iterator();
71                  }
72  
73                  public void close() {
74  
75                  }
76              };
77          }
78          catch (IOException e) {
79              throw new HBqlException(e);
80          }
81      }
82  }