9
9
import org .apache .hadoop .hbase .client .ResultScanner ;
10
10
import org .apache .hadoop .hbase .client .Scan ;
11
11
import org .apache .hadoop .hbase .client .Table ;
12
+ import org .apache .hadoop .hbase .filter .BinaryComparator ;
13
+ import org .apache .hadoop .hbase .filter .CompareFilter ;
14
+ import org .apache .hadoop .hbase .filter .Filter ;
15
+ import org .apache .hadoop .hbase .filter .FilterList ;
12
16
import org .apache .hadoop .hbase .filter .PageFilter ;
17
+ import org .apache .hadoop .hbase .filter .RowFilter ;
13
18
import org .apache .hadoop .hbase .util .Bytes ;
14
19
20
+ import javax .annotation .Nullable ;
15
21
import java .io .IOException ;
16
22
import java .time .Instant ;
17
23
import java .util .ArrayList ;
22
28
import java .util .Objects ;
23
29
import java .util .Optional ;
24
30
import java .util .Set ;
31
+ import java .util .SortedMap ;
32
+ import java .util .TreeMap ;
25
33
import java .util .function .Function ;
26
34
import java .util .function .Supplier ;
27
35
import java .util .stream .Collectors ;
@@ -137,8 +145,10 @@ public <K extends Key<T>> Map<K, T> getAll(final Set<K> keys) throws IOException
137
145
}
138
146
139
147
/**
148
+ * Utility method for running scan without a provided constant.
149
+ *
140
150
* Runs a paging table scan from the provided starting key to the provided ending key,
141
- * and returns a list of entities in the order returned from BigTable.
151
+ * and returns a list of paired Key/Value entities in the order returned from BigTable.
142
152
*
143
153
* Use the last returned entity to construct a new starting key for subsequent paging requests until no values are returned.
144
154
*
@@ -151,31 +161,63 @@ public <K extends Key<T>> Map<K, T> getAll(final Set<K> keys) throws IOException
151
161
* @throws IOException when an error occurs while communicating with BigTable
152
162
*/
153
163
@ Override
154
- public <K extends Key <T >> List <T > scan (final K startKey ,
155
- final boolean startKeyInclusive ,
156
- final K endKey ,
157
- final boolean endKeyInclusive ,
158
- final int numRows ) throws IOException {
164
+ public <K extends Key <T >> SortedMap <Key <T >, T > scan (final K startKey ,
165
+ final boolean startKeyInclusive ,
166
+ final K endKey ,
167
+ final boolean endKeyInclusive ,
168
+ final int numRows ) throws IOException {
169
+ return scan (startKey , startKeyInclusive , endKey , endKeyInclusive , numRows , null );
170
+ }
171
+
172
+ /**
173
+ * Runs a paging table scan from the provided starting key to the provided ending key,
174
+ * and returns a list of paired Key/Value entities in the order returned from BigTable.
175
+ *
176
+ * Use the last returned entity to construct a new starting key for subsequent paging requests until no values are returned.
177
+ *
178
+ * @param startKey key to start scanning from (does not have to have an existing record at the location)
179
+ * @param startKeyInclusive whether to include result from startKey
180
+ * @param endKey key to end scanning on (does not have to have an existing record at the location)
181
+ * @param endKeyInclusive whether to include result from endKey
182
+ * @param numRows max number of entries to return
183
+ * @param constant optional field to be used to be included, should be the constant provided to KeyComponent if it exists
184
+ * @return A list of entities in the order that they are stored in BigTable
185
+ * @throws IOException when an error occurs while communicating with BigTable
186
+ */
187
+ @ Override
188
+ public <K extends Key <T >> SortedMap <Key <T >, T > scan (final K startKey ,
189
+ final boolean startKeyInclusive ,
190
+ final K endKey ,
191
+ final boolean endKeyInclusive ,
192
+ final int numRows ,
193
+ @ Nullable final String constant ) throws IOException {
159
194
Objects .requireNonNull (startKey );
160
195
Objects .requireNonNull (endKey );
161
196
197
+ List <Filter > filters = new ArrayList <>();
198
+ filters .add (new PageFilter (numRows ));
199
+ if (Objects .nonNull (constant ) && !"" .equals (constant )) {
200
+ filters .add (new RowFilter (CompareFilter .CompareOp .EQUAL , new BinaryComparator (constant .getBytes ())));
201
+ }
202
+ FilterList filterList = new FilterList (FilterList .Operator .MUST_PASS_ALL , filters );
203
+
162
204
final Scan scan = new Scan ();
163
- scan .setFilter (new PageFilter ( numRows ) );
205
+ scan .setFilter (filterList );
164
206
scan .withStartRow (startKey .toBytes (), startKeyInclusive );
165
207
scan .withStopRow (endKey .toBytes (), endKeyInclusive );
166
208
167
209
final ResultScanner scanner = table .getScanner (scan );
168
- final List < T > results = new ArrayList <>();
210
+ final SortedMap < Key < T >, T > results = new TreeMap <>();
169
211
170
212
Result result ;
171
213
while ((result = scanner .next ()) != null ) {
172
214
if (!result .isEmpty ()) {
173
215
final T entity = delegateToEntity (result );
174
- results .add ( entity );
216
+ results .put ( new RawKey < T >( result . getRow ()), entity );
175
217
}
176
218
}
177
219
178
- return Collections .unmodifiableList (results );
220
+ return Collections .unmodifiableSortedMap (results );
179
221
}
180
222
181
223
/**
0 commit comments