HBASE-23846 Removed deprecated setMaxVersions(int) from Scan
[hbase.git] / hbase-thrift / src / main / java / org / apache / hadoop / hbase / thrift2 / ThriftUtilities.java
blob1ac29f3933a94fab3cd40eda3465dbc3e91e716a
1 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 package org.apache.hadoop.hbase.thrift2;
21 import static org.apache.hadoop.hbase.util.Bytes.getBytes;
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.NavigableSet;
30 import java.util.Set;
31 import java.util.stream.Collectors;
33 import org.apache.hadoop.hbase.Cell;
34 import org.apache.hadoop.hbase.CellBuilderFactory;
35 import org.apache.hadoop.hbase.CellBuilderType;
36 import org.apache.hadoop.hbase.CellUtil;
37 import org.apache.hadoop.hbase.CompareOperator;
38 import org.apache.hadoop.hbase.ExtendedCellBuilder;
39 import org.apache.hadoop.hbase.ExtendedCellBuilderFactory;
40 import org.apache.hadoop.hbase.HConstants;
41 import org.apache.hadoop.hbase.HRegionLocation;
42 import org.apache.hadoop.hbase.HTableDescriptor;
43 import org.apache.hadoop.hbase.KeepDeletedCells;
44 import org.apache.hadoop.hbase.NamespaceDescriptor;
45 import org.apache.hadoop.hbase.PrivateCellUtil;
46 import org.apache.hadoop.hbase.ServerName;
47 import org.apache.hadoop.hbase.TableName;
48 import org.apache.hadoop.hbase.client.Append;
49 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
50 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
51 import org.apache.hadoop.hbase.client.Consistency;
52 import org.apache.hadoop.hbase.client.Delete;
53 import org.apache.hadoop.hbase.client.Durability;
54 import org.apache.hadoop.hbase.client.Get;
55 import org.apache.hadoop.hbase.client.Increment;
56 import org.apache.hadoop.hbase.client.Mutation;
57 import org.apache.hadoop.hbase.client.OperationWithAttributes;
58 import org.apache.hadoop.hbase.client.Put;
59 import org.apache.hadoop.hbase.client.RegionInfo;
60 import org.apache.hadoop.hbase.client.Result;
61 import org.apache.hadoop.hbase.client.RowMutations;
62 import org.apache.hadoop.hbase.client.Scan;
63 import org.apache.hadoop.hbase.client.Scan.ReadType;
64 import org.apache.hadoop.hbase.client.SlowLogQueryFilter;
65 import org.apache.hadoop.hbase.client.SlowLogRecord;
66 import org.apache.hadoop.hbase.client.TableDescriptor;
67 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
68 import org.apache.hadoop.hbase.exceptions.DeserializationException;
69 import org.apache.hadoop.hbase.filter.Filter;
70 import org.apache.hadoop.hbase.filter.ParseFilter;
71 import org.apache.hadoop.hbase.io.TimeRange;
72 import org.apache.hadoop.hbase.io.compress.Compression;
73 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
74 import org.apache.hadoop.hbase.regionserver.BloomType;
75 import org.apache.hadoop.hbase.security.visibility.Authorizations;
76 import org.apache.hadoop.hbase.security.visibility.CellVisibility;
77 import org.apache.hadoop.hbase.thrift2.generated.TAppend;
78 import org.apache.hadoop.hbase.thrift2.generated.TAuthorization;
79 import org.apache.hadoop.hbase.thrift2.generated.TBloomFilterType;
80 import org.apache.hadoop.hbase.thrift2.generated.TCellVisibility;
81 import org.apache.hadoop.hbase.thrift2.generated.TColumn;
82 import org.apache.hadoop.hbase.thrift2.generated.TColumnFamilyDescriptor;
83 import org.apache.hadoop.hbase.thrift2.generated.TColumnIncrement;
84 import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
85 import org.apache.hadoop.hbase.thrift2.generated.TCompareOperator;
86 import org.apache.hadoop.hbase.thrift2.generated.TCompressionAlgorithm;
87 import org.apache.hadoop.hbase.thrift2.generated.TConsistency;
88 import org.apache.hadoop.hbase.thrift2.generated.TDataBlockEncoding;
89 import org.apache.hadoop.hbase.thrift2.generated.TDelete;
90 import org.apache.hadoop.hbase.thrift2.generated.TDeleteType;
91 import org.apache.hadoop.hbase.thrift2.generated.TDurability;
92 import org.apache.hadoop.hbase.thrift2.generated.TGet;
93 import org.apache.hadoop.hbase.thrift2.generated.THRegionInfo;
94 import org.apache.hadoop.hbase.thrift2.generated.THRegionLocation;
95 import org.apache.hadoop.hbase.thrift2.generated.TIncrement;
96 import org.apache.hadoop.hbase.thrift2.generated.TKeepDeletedCells;
97 import org.apache.hadoop.hbase.thrift2.generated.TMutation;
98 import org.apache.hadoop.hbase.thrift2.generated.TNamespaceDescriptor;
99 import org.apache.hadoop.hbase.thrift2.generated.TPut;
100 import org.apache.hadoop.hbase.thrift2.generated.TReadType;
101 import org.apache.hadoop.hbase.thrift2.generated.TResult;
102 import org.apache.hadoop.hbase.thrift2.generated.TRowMutations;
103 import org.apache.hadoop.hbase.thrift2.generated.TScan;
104 import org.apache.hadoop.hbase.thrift2.generated.TServerName;
105 import org.apache.hadoop.hbase.thrift2.generated.TSlowLogQueryFilter;
106 import org.apache.hadoop.hbase.thrift2.generated.TSlowLogRecord;
107 import org.apache.hadoop.hbase.thrift2.generated.TTableDescriptor;
108 import org.apache.hadoop.hbase.thrift2.generated.TTableName;
109 import org.apache.hadoop.hbase.thrift2.generated.TTimeRange;
110 import org.apache.hadoop.hbase.util.Bytes;
111 import org.apache.hbase.thirdparty.org.apache.commons.collections4.CollectionUtils;
112 import org.apache.yetus.audience.InterfaceAudience;
114 import org.apache.hbase.thirdparty.org.apache.commons.collections4.MapUtils;
116 import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
117 import org.apache.hadoop.hbase.shaded.protobuf.generated.FilterProtos;
119 @InterfaceAudience.Private
120 public final class ThriftUtilities {
122 private final static Cell[] EMPTY_CELL_ARRAY = new Cell[]{};
123 private final static Result EMPTY_RESULT = Result.create(EMPTY_CELL_ARRAY);
124 private final static Result EMPTY_RESULT_STALE = Result.create(EMPTY_CELL_ARRAY, null, true);
128 private ThriftUtilities() {
129 throw new UnsupportedOperationException("Can't initialize class");
133 * Creates a {@link Get} (HBase) from a {@link TGet} (Thrift).
135 * This ignores any timestamps set on {@link TColumn} objects.
137 * @param in the <code>TGet</code> to convert
139 * @return <code>Get</code> object
141 * @throws IOException if an invalid time range or max version parameter is given
143 public static Get getFromThrift(TGet in) throws IOException {
144 Get out = new Get(in.getRow());
146 // Timestamp overwrites time range if both are set
147 if (in.isSetTimestamp()) {
148 out.setTimestamp(in.getTimestamp());
149 } else if (in.isSetTimeRange()) {
150 out.setTimeRange(in.getTimeRange().getMinStamp(), in.getTimeRange().getMaxStamp());
153 if (in.isSetMaxVersions()) {
154 out.readVersions(in.getMaxVersions());
157 if (in.isSetFilterString()) {
158 ParseFilter parseFilter = new ParseFilter();
159 out.setFilter(parseFilter.parseFilterString(in.getFilterString()));
162 if (in.isSetAttributes()) {
163 addAttributes(out,in.getAttributes());
166 if (in.isSetAuthorizations()) {
167 out.setAuthorizations(new Authorizations(in.getAuthorizations().getLabels()));
170 if (in.isSetConsistency()) {
171 out.setConsistency(consistencyFromThrift(in.getConsistency()));
174 if (in.isSetTargetReplicaId()) {
175 out.setReplicaId(in.getTargetReplicaId());
178 if (in.isSetCacheBlocks()) {
179 out.setCacheBlocks(in.isCacheBlocks());
181 if (in.isSetStoreLimit()) {
182 out.setMaxResultsPerColumnFamily(in.getStoreLimit());
184 if (in.isSetStoreOffset()) {
185 out.setRowOffsetPerColumnFamily(in.getStoreOffset());
187 if (in.isSetExistence_only()) {
188 out.setCheckExistenceOnly(in.isExistence_only());
191 if (in.isSetColumns()) {
192 for (TColumn column : in.getColumns()) {
193 if (column.isSetQualifier()) {
194 out.addColumn(column.getFamily(), column.getQualifier());
195 } else {
196 out.addFamily(column.getFamily());
201 if (in.isSetFilterBytes()) {
202 out.setFilter(filterFromThrift(in.getFilterBytes()));
204 return out;
208 * Converts multiple {@link TGet}s (Thrift) into a list of {@link Get}s (HBase).
210 * @param in list of <code>TGet</code>s to convert
212 * @return list of <code>Get</code> objects
214 * @throws IOException if an invalid time range or max version parameter is given
215 * @see #getFromThrift(TGet)
217 public static List<Get> getsFromThrift(List<TGet> in) throws IOException {
218 List<Get> out = new ArrayList<>(in.size());
219 for (TGet get : in) {
220 out.add(getFromThrift(get));
222 return out;
226 * Creates a {@link TResult} (Thrift) from a {@link Result} (HBase).
228 * @param in the <code>Result</code> to convert
230 * @return converted result, returns an empty result if the input is <code>null</code>
232 public static TResult resultFromHBase(Result in) {
233 Cell[] raw = in.rawCells();
234 TResult out = new TResult();
235 byte[] row = in.getRow();
236 if (row != null) {
237 out.setRow(in.getRow());
239 List<TColumnValue> columnValues = new ArrayList<>(raw.length);
240 for (Cell kv : raw) {
241 TColumnValue col = new TColumnValue();
242 col.setFamily(CellUtil.cloneFamily(kv));
243 col.setQualifier(CellUtil.cloneQualifier(kv));
244 col.setTimestamp(kv.getTimestamp());
245 col.setValue(CellUtil.cloneValue(kv));
246 col.setType(kv.getType().getCode());
247 if (kv.getTagsLength() > 0) {
248 col.setTags(PrivateCellUtil.cloneTags(kv));
250 columnValues.add(col);
252 out.setColumnValues(columnValues);
254 out.setStale(in.isStale());
256 out.setPartial(in.mayHaveMoreCellsInRow());
257 return out;
261 * Converts multiple {@link Result}s (HBase) into a list of {@link TResult}s (Thrift).
263 * @param in array of <code>Result</code>s to convert
265 * @return list of converted <code>TResult</code>s
267 * @see #resultFromHBase(Result)
269 public static List<TResult> resultsFromHBase(Result[] in) {
270 List<TResult> out = new ArrayList<>(in.length);
271 for (Result result : in) {
272 out.add(resultFromHBase(result));
274 return out;
278 * Creates a {@link Put} (HBase) from a {@link TPut} (Thrift)
280 * @param in the <code>TPut</code> to convert
282 * @return converted <code>Put</code>
284 public static Put putFromThrift(TPut in) {
285 Put out;
287 if (in.isSetTimestamp()) {
288 out = new Put(in.getRow(), in.getTimestamp());
289 } else {
290 out = new Put(in.getRow());
293 if (in.isSetDurability()) {
294 out.setDurability(durabilityFromThrift(in.getDurability()));
297 for (TColumnValue columnValue : in.getColumnValues()) {
298 try {
299 if (columnValue.isSetTimestamp()) {
300 out.add(CellBuilderFactory.create(CellBuilderType.DEEP_COPY)
301 .setRow(out.getRow())
302 .setFamily(columnValue.getFamily())
303 .setQualifier(columnValue.getQualifier())
304 .setTimestamp(columnValue.getTimestamp())
305 .setType(Cell.Type.Put)
306 .setValue(columnValue.getValue())
307 .build());
308 } else {
309 out.add(CellBuilderFactory.create(CellBuilderType.DEEP_COPY)
310 .setRow(out.getRow())
311 .setFamily(columnValue.getFamily())
312 .setQualifier(columnValue.getQualifier())
313 .setTimestamp(out.getTimestamp())
314 .setType(Cell.Type.Put)
315 .setValue(columnValue.getValue())
316 .build());
318 } catch (IOException e) {
319 throw new IllegalArgumentException((e));
323 if (in.isSetAttributes()) {
324 addAttributes(out,in.getAttributes());
327 if (in.getCellVisibility() != null) {
328 out.setCellVisibility(new CellVisibility(in.getCellVisibility().getExpression()));
331 return out;
335 * Converts multiple {@link TPut}s (Thrift) into a list of {@link Put}s (HBase).
337 * @param in list of <code>TPut</code>s to convert
339 * @return list of converted <code>Put</code>s
341 * @see #putFromThrift(TPut)
343 public static List<Put> putsFromThrift(List<TPut> in) {
344 List<Put> out = new ArrayList<>(in.size());
345 for (TPut put : in) {
346 out.add(putFromThrift(put));
348 return out;
352 * Creates a {@link Delete} (HBase) from a {@link TDelete} (Thrift).
354 * @param in the <code>TDelete</code> to convert
356 * @return converted <code>Delete</code>
358 public static Delete deleteFromThrift(TDelete in) {
359 Delete out;
361 if (in.isSetColumns()) {
362 out = new Delete(in.getRow());
363 for (TColumn column : in.getColumns()) {
364 if (in.isSetDeleteType()) {
365 switch (in.getDeleteType()) {
366 case DELETE_COLUMN:
367 if (column.isSetTimestamp()) {
368 out.addColumn(column.getFamily(), column.getQualifier(), column.getTimestamp());
369 } else {
370 out.addColumn(column.getFamily(), column.getQualifier());
372 break;
373 case DELETE_COLUMNS:
374 if (column.isSetTimestamp()) {
375 out.addColumns(column.getFamily(), column.getQualifier(), column.getTimestamp());
376 } else {
377 out.addColumns(column.getFamily(), column.getQualifier());
379 break;
380 case DELETE_FAMILY:
381 if (column.isSetTimestamp()) {
382 out.addFamily(column.getFamily(), column.getTimestamp());
383 } else {
384 out.addFamily(column.getFamily());
386 break;
387 case DELETE_FAMILY_VERSION:
388 if (column.isSetTimestamp()) {
389 out.addFamilyVersion(column.getFamily(), column.getTimestamp());
390 } else {
391 throw new IllegalArgumentException(
392 "Timestamp is required for TDelete with DeleteFamilyVersion type");
394 break;
395 default:
396 throw new IllegalArgumentException("DeleteType is required for TDelete");
398 } else {
399 throw new IllegalArgumentException("DeleteType is required for TDelete");
402 } else {
403 if (in.isSetTimestamp()) {
404 out = new Delete(in.getRow(), in.getTimestamp());
405 } else {
406 out = new Delete(in.getRow());
410 if (in.isSetAttributes()) {
411 addAttributes(out,in.getAttributes());
414 if (in.isSetDurability()) {
415 out.setDurability(durabilityFromThrift(in.getDurability()));
418 return out;
422 * Converts multiple {@link TDelete}s (Thrift) into a list of {@link Delete}s (HBase).
424 * @param in list of <code>TDelete</code>s to convert
426 * @return list of converted <code>Delete</code>s
428 * @see #deleteFromThrift(TDelete)
431 public static List<Delete> deletesFromThrift(List<TDelete> in) {
432 List<Delete> out = new ArrayList<>(in.size());
433 for (TDelete delete : in) {
434 out.add(deleteFromThrift(delete));
436 return out;
439 public static TDeleteType deleteTypeFromHBase(Cell.Type type) {
440 switch (type) {
441 case Delete: return TDeleteType.DELETE_COLUMN;
442 case DeleteColumn: return TDeleteType.DELETE_COLUMNS;
443 case DeleteFamily: return TDeleteType.DELETE_FAMILY;
444 case DeleteFamilyVersion: return TDeleteType.DELETE_FAMILY_VERSION;
445 default: throw new IllegalArgumentException("Unknow delete type " + type);
448 public static TDelete deleteFromHBase(Delete in) {
449 TDelete out = new TDelete(ByteBuffer.wrap(in.getRow()));
451 List<TColumn> columns = new ArrayList<>(in.getFamilyCellMap().entrySet().size());
452 long rowTimestamp = in.getTimestamp();
453 if (rowTimestamp != HConstants.LATEST_TIMESTAMP) {
454 out.setTimestamp(rowTimestamp);
457 for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
458 out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
459 ByteBuffer.wrap(attribute.getValue()));
461 if (in.getDurability() != Durability.USE_DEFAULT) {
462 out.setDurability(durabilityFromHBase(in.getDurability()));
464 // Delete the whole row
465 if (in.getFamilyCellMap().size() == 0) {
466 return out;
468 TDeleteType type = null;
469 for (Map.Entry<byte[], List<Cell>> familyEntry:
470 in.getFamilyCellMap().entrySet()) {
471 byte[] family = familyEntry.getKey();
472 TColumn column = new TColumn(ByteBuffer.wrap(familyEntry.getKey()));
473 for (Cell cell: familyEntry.getValue()) {
474 TDeleteType cellDeleteType = deleteTypeFromHBase(cell.getType());
475 if (type == null) {
476 type = cellDeleteType;
477 } else if (type != cellDeleteType){
478 throw new RuntimeException("Only the same delete type is supported, but two delete type "
479 + "is founded, one is " + type + " the other one is " + cellDeleteType);
481 byte[] qualifier = CellUtil.cloneQualifier(cell);
482 long timestamp = cell.getTimestamp();
483 column.setFamily(family);
484 if (qualifier != null) {
485 column.setQualifier(qualifier);
487 if (timestamp != HConstants.LATEST_TIMESTAMP) {
488 column.setTimestamp(timestamp);
491 columns.add(column);
493 out.setColumns(columns);
494 out.setDeleteType(type);
496 return out;
500 * Creates a {@link RowMutations} (HBase) from a {@link TRowMutations} (Thrift)
502 * @param in the <code>TRowMutations</code> to convert
504 * @return converted <code>RowMutations</code>
506 public static RowMutations rowMutationsFromThrift(TRowMutations in) throws IOException {
507 List<TMutation> mutations = in.getMutations();
508 RowMutations out = new RowMutations(in.getRow(), mutations.size());
509 for (TMutation mutation : mutations) {
510 if (mutation.isSetPut()) {
511 out.add(putFromThrift(mutation.getPut()));
513 if (mutation.isSetDeleteSingle()) {
514 out.add(deleteFromThrift(mutation.getDeleteSingle()));
517 return out;
520 public static Scan scanFromThrift(TScan in) throws IOException {
521 Scan out = new Scan();
523 if (in.isSetStartRow()) {
524 out.withStartRow(in.getStartRow());
526 if (in.isSetStopRow()) {
527 out.setStopRow(in.getStopRow());
529 if (in.isSetCaching()) {
530 out.setCaching(in.getCaching());
532 if (in.isSetMaxVersions()) {
533 out.readVersions(in.getMaxVersions());
536 if (in.isSetColumns()) {
537 for (TColumn column : in.getColumns()) {
538 if (column.isSetQualifier()) {
539 out.addColumn(column.getFamily(), column.getQualifier());
540 } else {
541 out.addFamily(column.getFamily());
546 TTimeRange timeRange = in.getTimeRange();
547 if (timeRange != null &&
548 timeRange.isSetMinStamp() && timeRange.isSetMaxStamp()) {
549 out.setTimeRange(timeRange.getMinStamp(), timeRange.getMaxStamp());
552 if (in.isSetBatchSize()) {
553 out.setBatch(in.getBatchSize());
556 if (in.isSetFilterString()) {
557 ParseFilter parseFilter = new ParseFilter();
558 out.setFilter(parseFilter.parseFilterString(in.getFilterString()));
561 if (in.isSetAttributes()) {
562 addAttributes(out,in.getAttributes());
565 if (in.isSetAuthorizations()) {
566 out.setAuthorizations(new Authorizations(in.getAuthorizations().getLabels()));
569 if (in.isSetReversed()) {
570 out.setReversed(in.isReversed());
573 if (in.isSetCacheBlocks()) {
574 out.setCacheBlocks(in.isCacheBlocks());
577 if (in.isSetColFamTimeRangeMap()) {
578 Map<ByteBuffer, TTimeRange> colFamTimeRangeMap = in.getColFamTimeRangeMap();
579 if (MapUtils.isNotEmpty(colFamTimeRangeMap)) {
580 for (Map.Entry<ByteBuffer, TTimeRange> entry : colFamTimeRangeMap.entrySet()) {
581 out.setColumnFamilyTimeRange(Bytes.toBytes(entry.getKey()),
582 entry.getValue().getMinStamp(), entry.getValue().getMaxStamp());
587 if (in.isSetReadType()) {
588 out.setReadType(readTypeFromThrift(in.getReadType()));
591 if (in.isSetLimit()) {
592 out.setLimit(in.getLimit());
595 if (in.isSetConsistency()) {
596 out.setConsistency(consistencyFromThrift(in.getConsistency()));
599 if (in.isSetTargetReplicaId()) {
600 out.setReplicaId(in.getTargetReplicaId());
603 if (in.isSetFilterBytes()) {
604 out.setFilter(filterFromThrift(in.getFilterBytes()));
607 return out;
610 public static byte[] filterFromHBase(Filter filter) throws IOException {
611 FilterProtos.Filter filterPB = ProtobufUtil.toFilter(filter);
612 return filterPB.toByteArray();
615 public static Filter filterFromThrift(byte[] filterBytes) throws IOException {
616 FilterProtos.Filter filterPB = FilterProtos.Filter.parseFrom(filterBytes);
617 return ProtobufUtil.toFilter(filterPB);
620 public static TScan scanFromHBase(Scan in) throws IOException {
621 TScan out = new TScan();
622 out.setStartRow(in.getStartRow());
623 out.setStopRow(in.getStopRow());
624 out.setCaching(in.getCaching());
625 out.setMaxVersions(in.getMaxVersions());
626 for (Map.Entry<byte[], NavigableSet<byte[]>> family : in.getFamilyMap().entrySet()) {
628 if (family.getValue() != null && !family.getValue().isEmpty()) {
629 for (byte[] qualifier : family.getValue()) {
630 TColumn column = new TColumn();
631 column.setFamily(family.getKey());
632 column.setQualifier(qualifier);
633 out.addToColumns(column);
635 } else {
636 TColumn column = new TColumn();
637 column.setFamily(family.getKey());
638 out.addToColumns(column);
641 TTimeRange tTimeRange = new TTimeRange();
642 tTimeRange.setMinStamp(in.getTimeRange().getMin()).setMaxStamp(in.getTimeRange().getMax());
643 out.setTimeRange(tTimeRange);
644 out.setBatchSize(in.getBatch());
646 for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
647 out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
648 ByteBuffer.wrap(attribute.getValue()));
651 try {
652 Authorizations authorizations = in.getAuthorizations();
653 if (authorizations != null) {
654 TAuthorization tAuthorization = new TAuthorization();
655 tAuthorization.setLabels(authorizations.getLabels());
656 out.setAuthorizations(tAuthorization);
658 } catch (DeserializationException e) {
659 throw new RuntimeException(e);
662 out.setReversed(in.isReversed());
663 out.setCacheBlocks(in.getCacheBlocks());
664 out.setReadType(readTypeFromHBase(in.getReadType()));
665 out.setLimit(in.getLimit());
666 out.setConsistency(consistencyFromHBase(in.getConsistency()));
667 out.setTargetReplicaId(in.getReplicaId());
668 for (Map.Entry<byte[], TimeRange> entry : in.getColumnFamilyTimeRange().entrySet()) {
669 if (entry.getValue() != null) {
670 TTimeRange timeRange = new TTimeRange();
671 timeRange.setMinStamp(entry.getValue().getMin()).setMaxStamp(entry.getValue().getMax());
672 out.putToColFamTimeRangeMap(ByteBuffer.wrap(entry.getKey()), timeRange);
675 if (in.getFilter() != null) {
676 try {
677 out.setFilterBytes(filterFromHBase(in.getFilter()));
678 } catch (IOException ioE) {
679 throw new RuntimeException(ioE);
682 return out;
685 public static Increment incrementFromThrift(TIncrement in) throws IOException {
686 Increment out = new Increment(in.getRow());
687 for (TColumnIncrement column : in.getColumns()) {
688 out.addColumn(column.getFamily(), column.getQualifier(), column.getAmount());
691 if (in.isSetAttributes()) {
692 addAttributes(out,in.getAttributes());
695 if (in.isSetDurability()) {
696 out.setDurability(durabilityFromThrift(in.getDurability()));
699 if(in.getCellVisibility() != null) {
700 out.setCellVisibility(new CellVisibility(in.getCellVisibility().getExpression()));
703 if (in.isSetReturnResults()) {
704 out.setReturnResults(in.isReturnResults());
707 return out;
710 public static Append appendFromThrift(TAppend append) throws IOException {
711 Append out = new Append(append.getRow());
712 for (TColumnValue column : append.getColumns()) {
713 out.addColumn(column.getFamily(), column.getQualifier(), column.getValue());
716 if (append.isSetAttributes()) {
717 addAttributes(out, append.getAttributes());
720 if (append.isSetDurability()) {
721 out.setDurability(durabilityFromThrift(append.getDurability()));
724 if(append.getCellVisibility() != null) {
725 out.setCellVisibility(new CellVisibility(append.getCellVisibility().getExpression()));
728 if (append.isSetReturnResults()) {
729 out.setReturnResults(append.isReturnResults());
732 return out;
735 public static THRegionLocation regionLocationFromHBase(HRegionLocation hrl) {
736 RegionInfo hri = hrl.getRegion();
737 ServerName serverName = hrl.getServerName();
739 THRegionInfo thRegionInfo = new THRegionInfo();
740 THRegionLocation thRegionLocation = new THRegionLocation();
741 TServerName tServerName = new TServerName();
743 tServerName.setHostName(serverName.getHostname());
744 tServerName.setPort(serverName.getPort());
745 tServerName.setStartCode(serverName.getStartcode());
747 thRegionInfo.setTableName(hri.getTable().getName());
748 thRegionInfo.setEndKey(hri.getEndKey());
749 thRegionInfo.setStartKey(hri.getStartKey());
750 thRegionInfo.setOffline(hri.isOffline());
751 thRegionInfo.setSplit(hri.isSplit());
752 thRegionInfo.setReplicaId(hri.getReplicaId());
754 thRegionLocation.setRegionInfo(thRegionInfo);
755 thRegionLocation.setServerName(tServerName);
757 return thRegionLocation;
760 public static List<THRegionLocation> regionLocationsFromHBase(List<HRegionLocation> locations) {
761 List<THRegionLocation> tlocations = new ArrayList<>(locations.size());
762 for (HRegionLocation hrl:locations) {
763 tlocations.add(regionLocationFromHBase(hrl));
765 return tlocations;
769 * Adds all the attributes into the Operation object
771 private static void addAttributes(OperationWithAttributes op,
772 Map<ByteBuffer, ByteBuffer> attributes) {
773 if (attributes == null || attributes.isEmpty()) {
774 return;
776 for (Map.Entry<ByteBuffer, ByteBuffer> entry : attributes.entrySet()) {
777 String name = Bytes.toStringBinary(getBytes(entry.getKey()));
778 byte[] value = getBytes(entry.getValue());
779 op.setAttribute(name, value);
783 private static Durability durabilityFromThrift(TDurability tDurability) {
784 switch (tDurability.getValue()) {
785 case 0: return Durability.USE_DEFAULT;
786 case 1: return Durability.SKIP_WAL;
787 case 2: return Durability.ASYNC_WAL;
788 case 3: return Durability.SYNC_WAL;
789 case 4: return Durability.FSYNC_WAL;
790 default: return Durability.USE_DEFAULT;
794 public static CompareOperator compareOpFromThrift(TCompareOperator tCompareOp) {
795 switch (tCompareOp.getValue()) {
796 case 0: return CompareOperator.LESS;
797 case 1: return CompareOperator.LESS_OR_EQUAL;
798 case 2: return CompareOperator.EQUAL;
799 case 3: return CompareOperator.NOT_EQUAL;
800 case 4: return CompareOperator.GREATER_OR_EQUAL;
801 case 5: return CompareOperator.GREATER;
802 case 6: return CompareOperator.NO_OP;
803 default: return null;
807 private static ReadType readTypeFromThrift(TReadType tReadType) {
808 switch (tReadType.getValue()) {
809 case 1: return ReadType.DEFAULT;
810 case 2: return ReadType.STREAM;
811 case 3: return ReadType.PREAD;
812 default: return null;
816 private static TReadType readTypeFromHBase(ReadType readType) {
817 switch (readType) {
818 case DEFAULT: return TReadType.DEFAULT;
819 case STREAM: return TReadType.STREAM;
820 case PREAD: return TReadType.PREAD;
821 default: return TReadType.DEFAULT;
825 private static Consistency consistencyFromThrift(TConsistency tConsistency) {
826 switch (tConsistency.getValue()) {
827 case 1: return Consistency.STRONG;
828 case 2: return Consistency.TIMELINE;
829 default: return Consistency.STRONG;
833 public static TableName tableNameFromThrift(TTableName tableName) {
834 return TableName.valueOf(tableName.getNs(), tableName.getQualifier());
837 public static TableName[] tableNamesArrayFromThrift(List<TTableName> tableNames) {
838 TableName[] out = new TableName[tableNames.size()];
839 int index = 0;
840 for (TTableName tableName : tableNames) {
841 out[index++] = tableNameFromThrift(tableName);
843 return out;
846 public static List<TableName> tableNamesFromThrift(List<TTableName> tableNames) {
847 List<TableName> out = new ArrayList<>(tableNames.size());
848 for (TTableName tableName : tableNames) {
849 out.add(tableNameFromThrift(tableName));
851 return out;
854 public static TTableName tableNameFromHBase(TableName table) {
855 TTableName tableName = new TTableName();
856 tableName.setNs(table.getNamespace());
857 tableName.setQualifier(table.getQualifier());
858 return tableName;
861 public static List<TTableName> tableNamesFromHBase(List<TableName> in) {
862 List<TTableName> out = new ArrayList<>(in.size());
863 for (TableName tableName : in) {
864 out.add(tableNameFromHBase(tableName));
866 return out;
869 public static List<TTableName> tableNamesFromHBase(TableName[] in) {
870 List<TTableName> out = new ArrayList<>(in.length);
871 for (TableName tableName : in) {
872 out.add(tableNameFromHBase(tableName));
874 return out;
877 public static byte[][] splitKeyFromThrift(List<ByteBuffer> in) {
878 if (in == null || in.size() == 0) {
879 return null;
881 byte[][] out = new byte[in.size()][];
882 int index = 0;
883 for (ByteBuffer key : in) {
884 out[index++] = key.array();
886 return out;
889 public static BloomType bloomFilterFromThrift(TBloomFilterType in) {
890 switch (in.getValue()) {
891 case 0: return BloomType.NONE;
892 case 1: return BloomType.ROW;
893 case 2: return BloomType.ROWCOL;
894 case 3: return BloomType.ROWPREFIX_FIXED_LENGTH;
895 default: return BloomType.ROW;
899 public static Compression.Algorithm compressionAlgorithmFromThrift(TCompressionAlgorithm in) {
900 switch (in.getValue()) {
901 case 0: return Compression.Algorithm.LZO;
902 case 1: return Compression.Algorithm.GZ;
903 case 2: return Compression.Algorithm.NONE;
904 case 3: return Compression.Algorithm.SNAPPY;
905 case 4: return Compression.Algorithm.LZ4;
906 case 5: return Compression.Algorithm.BZIP2;
907 case 6: return Compression.Algorithm.ZSTD;
908 default: return Compression.Algorithm.NONE;
912 public static DataBlockEncoding dataBlockEncodingFromThrift(TDataBlockEncoding in) {
913 switch (in.getValue()) {
914 case 0: return DataBlockEncoding.NONE;
915 case 2: return DataBlockEncoding.PREFIX;
916 case 3: return DataBlockEncoding.DIFF;
917 case 4: return DataBlockEncoding.FAST_DIFF;
918 case 7: return DataBlockEncoding.ROW_INDEX_V1;
919 default: return DataBlockEncoding.NONE;
923 public static KeepDeletedCells keepDeletedCellsFromThrift(TKeepDeletedCells in) {
924 switch (in.getValue()) {
925 case 0: return KeepDeletedCells.FALSE;
926 case 1: return KeepDeletedCells.TRUE;
927 case 2: return KeepDeletedCells.TTL;
928 default: return KeepDeletedCells.FALSE;
932 public static ColumnFamilyDescriptor columnFamilyDescriptorFromThrift(
933 TColumnFamilyDescriptor in) {
934 ColumnFamilyDescriptorBuilder builder = ColumnFamilyDescriptorBuilder
935 .newBuilder(in.getName());
937 if (in.isSetAttributes()) {
938 for (Map.Entry<ByteBuffer, ByteBuffer> attribute : in.getAttributes().entrySet()) {
939 builder.setValue(attribute.getKey().array(), attribute.getValue().array());
942 if (in.isSetConfiguration()) {
943 for (Map.Entry<String, String> conf : in.getConfiguration().entrySet()) {
944 builder.setConfiguration(conf.getKey(), conf.getValue());
947 if (in.isSetBlockSize()) {
948 builder.setBlocksize(in.getBlockSize());
950 if (in.isSetBloomnFilterType()) {
951 builder.setBloomFilterType(bloomFilterFromThrift(in.getBloomnFilterType()));
953 if (in.isSetCompressionType()) {
954 builder.setCompressionType(compressionAlgorithmFromThrift(in.getCompressionType()));
956 if (in.isSetDfsReplication()) {
957 builder.setDFSReplication(in.getDfsReplication());
959 if (in.isSetDataBlockEncoding()) {
960 builder.setDataBlockEncoding(dataBlockEncodingFromThrift(in.getDataBlockEncoding()));
962 if (in.isSetKeepDeletedCells()) {
963 builder.setKeepDeletedCells(keepDeletedCellsFromThrift(in.getKeepDeletedCells()));
965 if (in.isSetMaxVersions()) {
966 builder.setMaxVersions(in.getMaxVersions());
968 if (in.isSetMinVersions()) {
969 builder.setMinVersions(in.getMinVersions());
971 if (in.isSetScope()) {
972 builder.setScope(in.getScope());
974 if (in.isSetTimeToLive()) {
975 builder.setTimeToLive(in.getTimeToLive());
977 if (in.isSetBlockCacheEnabled()) {
978 builder.setBlockCacheEnabled(in.isBlockCacheEnabled());
980 if (in.isSetCacheBloomsOnWrite()) {
981 builder.setCacheBloomsOnWrite(in.isCacheBloomsOnWrite());
983 if (in.isSetCacheDataOnWrite()) {
984 builder.setCacheDataOnWrite(in.isCacheDataOnWrite());
986 if (in.isSetCacheIndexesOnWrite()) {
987 builder.setCacheIndexesOnWrite(in.isCacheIndexesOnWrite());
989 if (in.isSetCompressTags()) {
990 builder.setCompressTags(in.isCompressTags());
992 if (in.isSetEvictBlocksOnClose()) {
993 builder.setEvictBlocksOnClose(in.isEvictBlocksOnClose());
995 if (in.isSetInMemory()) {
996 builder.setInMemory(in.isInMemory());
1000 return builder.build();
1003 public static NamespaceDescriptor namespaceDescriptorFromThrift(TNamespaceDescriptor in) {
1004 NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(in.getName());
1005 if (in.isSetConfiguration()) {
1006 for (Map.Entry<String, String> conf : in.getConfiguration().entrySet()) {
1007 builder.addConfiguration(conf.getKey(), conf.getValue());
1010 return builder.build();
1013 public static TNamespaceDescriptor namespaceDescriptorFromHBase(NamespaceDescriptor in) {
1014 TNamespaceDescriptor out = new TNamespaceDescriptor();
1015 out.setName(in.getName());
1016 for (Map.Entry<String, String> conf : in.getConfiguration().entrySet()) {
1017 out.putToConfiguration(conf.getKey(), conf.getValue());
1019 return out;
1022 public static List<TNamespaceDescriptor> namespaceDescriptorsFromHBase(
1023 NamespaceDescriptor[] in) {
1024 List<TNamespaceDescriptor> out = new ArrayList<>(in.length);
1025 for (NamespaceDescriptor descriptor : in) {
1026 out.add(namespaceDescriptorFromHBase(descriptor));
1028 return out;
1031 public static TableDescriptor tableDescriptorFromThrift(TTableDescriptor in) {
1032 TableDescriptorBuilder builder = TableDescriptorBuilder
1033 .newBuilder(tableNameFromThrift(in.getTableName()));
1034 for (TColumnFamilyDescriptor column : in.getColumns()) {
1035 builder.setColumnFamily(columnFamilyDescriptorFromThrift(column));
1037 if (in.isSetAttributes()) {
1038 for (Map.Entry<ByteBuffer, ByteBuffer> attribute : in.getAttributes().entrySet()) {
1039 builder.setValue(attribute.getKey().array(), attribute.getValue().array());
1042 if (in.isSetDurability()) {
1043 builder.setDurability(durabilityFromThrift(in.getDurability()));
1045 return builder.build();
1048 public static HTableDescriptor hTableDescriptorFromThrift(TTableDescriptor in) {
1049 return new HTableDescriptor(tableDescriptorFromThrift(in));
1052 public static HTableDescriptor[] hTableDescriptorsFromThrift(List<TTableDescriptor> in) {
1053 HTableDescriptor[] out = new HTableDescriptor[in.size()];
1054 int index = 0;
1055 for (TTableDescriptor tTableDescriptor : in) {
1056 out[index++] = hTableDescriptorFromThrift(tTableDescriptor);
1058 return out;
1062 public static List<TableDescriptor> tableDescriptorsFromThrift(List<TTableDescriptor> in) {
1063 List<TableDescriptor> out = new ArrayList<>();
1064 for (TTableDescriptor tableDescriptor : in) {
1065 out.add(tableDescriptorFromThrift(tableDescriptor));
1067 return out;
1070 private static TDurability durabilityFromHBase(Durability durability) {
1071 switch (durability) {
1072 case USE_DEFAULT: return TDurability.USE_DEFAULT;
1073 case SKIP_WAL: return TDurability.SKIP_WAL;
1074 case ASYNC_WAL: return TDurability.ASYNC_WAL;
1075 case SYNC_WAL: return TDurability.SYNC_WAL;
1076 case FSYNC_WAL: return TDurability.FSYNC_WAL;
1077 default: return null;
1081 public static TTableDescriptor tableDescriptorFromHBase(TableDescriptor in) {
1082 TTableDescriptor out = new TTableDescriptor();
1083 out.setTableName(tableNameFromHBase(in.getTableName()));
1084 Map<Bytes, Bytes> attributes = in.getValues();
1085 for (Map.Entry<Bytes, Bytes> attribute : attributes.entrySet()) {
1086 out.putToAttributes(ByteBuffer.wrap(attribute.getKey().get()),
1087 ByteBuffer.wrap(attribute.getValue().get()));
1089 for (ColumnFamilyDescriptor column : in.getColumnFamilies()) {
1090 out.addToColumns(columnFamilyDescriptorFromHBase(column));
1092 out.setDurability(durabilityFromHBase(in.getDurability()));
1093 return out;
1096 public static List<TTableDescriptor> tableDescriptorsFromHBase(List<TableDescriptor> in) {
1097 List<TTableDescriptor> out = new ArrayList<>(in.size());
1098 for (TableDescriptor descriptor : in) {
1099 out.add(tableDescriptorFromHBase(descriptor));
1101 return out;
1104 public static List<TTableDescriptor> tableDescriptorsFromHBase(TableDescriptor[] in) {
1105 List<TTableDescriptor> out = new ArrayList<>(in.length);
1106 for (TableDescriptor descriptor : in) {
1107 out.add(tableDescriptorFromHBase(descriptor));
1109 return out;
1113 public static TBloomFilterType bloomFilterFromHBase(BloomType in) {
1114 switch (in) {
1115 case NONE: return TBloomFilterType.NONE;
1116 case ROW: return TBloomFilterType.ROW;
1117 case ROWCOL: return TBloomFilterType.ROWCOL;
1118 case ROWPREFIX_FIXED_LENGTH: return TBloomFilterType.ROWPREFIX_FIXED_LENGTH;
1119 default: return TBloomFilterType.ROW;
1123 public static TCompressionAlgorithm compressionAlgorithmFromHBase(Compression.Algorithm in) {
1124 switch (in) {
1125 case LZO: return TCompressionAlgorithm.LZO;
1126 case GZ: return TCompressionAlgorithm.GZ;
1127 case NONE: return TCompressionAlgorithm.NONE;
1128 case SNAPPY: return TCompressionAlgorithm.SNAPPY;
1129 case LZ4: return TCompressionAlgorithm.LZ4;
1130 case BZIP2: return TCompressionAlgorithm.BZIP2;
1131 case ZSTD: return TCompressionAlgorithm.ZSTD;
1132 default: return TCompressionAlgorithm.NONE;
1136 public static TDataBlockEncoding dataBlockEncodingFromHBase(DataBlockEncoding in) {
1137 switch (in) {
1138 case NONE: return TDataBlockEncoding.NONE;
1139 case PREFIX: return TDataBlockEncoding.PREFIX;
1140 case DIFF: return TDataBlockEncoding.DIFF;
1141 case FAST_DIFF: return TDataBlockEncoding.FAST_DIFF;
1142 case ROW_INDEX_V1: return TDataBlockEncoding.ROW_INDEX_V1;
1143 default: return TDataBlockEncoding.NONE;
1147 public static TKeepDeletedCells keepDeletedCellsFromHBase(KeepDeletedCells in) {
1148 switch (in) {
1149 case FALSE: return TKeepDeletedCells.FALSE;
1150 case TRUE: return TKeepDeletedCells.TRUE;
1151 case TTL: return TKeepDeletedCells.TTL;
1152 default: return TKeepDeletedCells.FALSE;
1156 public static TColumnFamilyDescriptor columnFamilyDescriptorFromHBase(
1157 ColumnFamilyDescriptor in) {
1158 TColumnFamilyDescriptor out = new TColumnFamilyDescriptor();
1159 out.setName(in.getName());
1160 for (Map.Entry<Bytes, Bytes> attribute : in.getValues().entrySet()) {
1161 out.putToAttributes(ByteBuffer.wrap(attribute.getKey().get()),
1162 ByteBuffer.wrap(attribute.getValue().get()));
1164 for (Map.Entry<String, String> conf : in.getConfiguration().entrySet()) {
1165 out.putToConfiguration(conf.getKey(), conf.getValue());
1167 out.setBlockSize(in.getBlocksize());
1168 out.setBloomnFilterType(bloomFilterFromHBase(in.getBloomFilterType()));
1169 out.setCompressionType(compressionAlgorithmFromHBase(in.getCompressionType()));
1170 out.setDfsReplication(in.getDFSReplication());
1171 out.setDataBlockEncoding(dataBlockEncodingFromHBase(in.getDataBlockEncoding()));
1172 out.setKeepDeletedCells(keepDeletedCellsFromHBase(in.getKeepDeletedCells()));
1173 out.setMaxVersions(in.getMaxVersions());
1174 out.setMinVersions(in.getMinVersions());
1175 out.setScope(in.getScope());
1176 out.setTimeToLive(in.getTimeToLive());
1177 out.setBlockCacheEnabled(in.isBlockCacheEnabled());
1178 out.setCacheBloomsOnWrite(in.isCacheBloomsOnWrite());
1179 out.setCacheDataOnWrite(in.isCacheDataOnWrite());
1180 out.setCacheIndexesOnWrite(in.isCacheIndexesOnWrite());
1181 out.setCompressTags(in.isCompressTags());
1182 out.setEvictBlocksOnClose(in.isEvictBlocksOnClose());
1183 out.setInMemory(in.isInMemory());
1184 return out;
1188 private static TConsistency consistencyFromHBase(Consistency consistency) {
1189 switch (consistency) {
1190 case STRONG: return TConsistency.STRONG;
1191 case TIMELINE: return TConsistency.TIMELINE;
1192 default: return TConsistency.STRONG;
1196 public static TGet getFromHBase(Get in) {
1197 TGet out = new TGet();
1198 out.setRow(in.getRow());
1200 TTimeRange tTimeRange = new TTimeRange();
1201 tTimeRange.setMaxStamp(in.getTimeRange().getMax()).setMinStamp(in.getTimeRange().getMin());
1202 out.setTimeRange(tTimeRange);
1203 out.setMaxVersions(in.getMaxVersions());
1205 for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
1206 out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
1207 ByteBuffer.wrap(attribute.getValue()));
1209 try {
1210 Authorizations authorizations = in.getAuthorizations();
1211 if (authorizations != null) {
1212 TAuthorization tAuthorization = new TAuthorization();
1213 tAuthorization.setLabels(authorizations.getLabels());
1214 out.setAuthorizations(tAuthorization);
1216 } catch (DeserializationException e) {
1217 throw new RuntimeException(e);
1219 out.setConsistency(consistencyFromHBase(in.getConsistency()));
1220 out.setTargetReplicaId(in.getReplicaId());
1221 out.setCacheBlocks(in.getCacheBlocks());
1222 out.setStoreLimit(in.getMaxResultsPerColumnFamily());
1223 out.setStoreOffset(in.getRowOffsetPerColumnFamily());
1224 out.setExistence_only(in.isCheckExistenceOnly());
1225 for (Map.Entry<byte[], NavigableSet<byte[]>> family : in.getFamilyMap().entrySet()) {
1227 if (family.getValue() != null && !family.getValue().isEmpty()) {
1228 for (byte[] qualifier : family.getValue()) {
1229 TColumn column = new TColumn();
1230 column.setFamily(family.getKey());
1231 column.setQualifier(qualifier);
1232 out.addToColumns(column);
1234 } else {
1235 TColumn column = new TColumn();
1236 column.setFamily(family.getKey());
1237 out.addToColumns(column);
1240 if (in.getFilter() != null) {
1241 try {
1242 out.setFilterBytes(filterFromHBase(in.getFilter()));
1243 } catch (IOException ioE) {
1244 throw new RuntimeException(ioE);
1247 return out;
1250 public static Cell toCell(ExtendedCellBuilder cellBuilder, byte[] row, TColumnValue columnValue) {
1251 return cellBuilder.clear()
1252 .setRow(row)
1253 .setFamily(columnValue.getFamily())
1254 .setQualifier(columnValue.getQualifier())
1255 .setTimestamp(columnValue.getTimestamp())
1256 .setType(columnValue.getType())
1257 .setValue(columnValue.getValue())
1258 .setTags(columnValue.getTags())
1259 .build();
1268 public static Result resultFromThrift(TResult in) {
1269 if (in == null) {
1270 return null;
1272 if (!in.isSetColumnValues() || in.getColumnValues().isEmpty()){
1273 return in.isStale() ? EMPTY_RESULT_STALE : EMPTY_RESULT;
1275 List<Cell> cells = new ArrayList<>(in.getColumnValues().size());
1276 ExtendedCellBuilder builder = ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);
1277 for (TColumnValue columnValue : in.getColumnValues()) {
1278 cells.add(toCell(builder, in.getRow(), columnValue));
1280 return Result.create(cells, null, in.isStale(), in.isPartial());
1283 public static TPut putFromHBase(Put in) {
1284 TPut out = new TPut();
1285 out.setRow(in.getRow());
1286 if (in.getTimestamp() != HConstants.LATEST_TIMESTAMP) {
1287 out.setTimestamp(in.getTimestamp());
1289 if (in.getDurability() != Durability.USE_DEFAULT) {
1290 out.setDurability(durabilityFromHBase(in.getDurability()));
1292 for (Map.Entry<byte [], List<Cell>> entry : in.getFamilyCellMap().entrySet()) {
1293 byte[] family = entry.getKey();
1294 for (Cell cell : entry.getValue()) {
1295 TColumnValue columnValue = new TColumnValue();
1296 columnValue.setFamily(family)
1297 .setQualifier(CellUtil.cloneQualifier(cell))
1298 .setType(cell.getType().getCode())
1299 .setTimestamp(cell.getTimestamp())
1300 .setValue(CellUtil.cloneValue(cell));
1301 if (cell.getTagsLength() != 0) {
1302 columnValue.setTags(PrivateCellUtil.cloneTags(cell));
1304 out.addToColumnValues(columnValue);
1307 for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
1308 out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
1309 ByteBuffer.wrap(attribute.getValue()));
1311 try {
1312 CellVisibility cellVisibility = in.getCellVisibility();
1313 if (cellVisibility != null) {
1314 TCellVisibility tCellVisibility = new TCellVisibility();
1315 tCellVisibility.setExpression(cellVisibility.getExpression());
1316 out.setCellVisibility(tCellVisibility);
1318 } catch (DeserializationException e) {
1319 throw new RuntimeException(e);
1321 return out;
1324 public static List<TPut> putsFromHBase(List<Put> in) {
1325 List<TPut> out = new ArrayList<>(in.size());
1326 for (Put put : in) {
1327 out.add(putFromHBase(put));
1329 return out;
1332 public static NamespaceDescriptor[] namespaceDescriptorsFromThrift(
1333 List<TNamespaceDescriptor> in) {
1334 NamespaceDescriptor[] out = new NamespaceDescriptor[in.size()];
1335 int index = 0;
1336 for (TNamespaceDescriptor descriptor : in) {
1337 out[index++] = namespaceDescriptorFromThrift(descriptor);
1339 return out;
1342 public static List<TDelete> deletesFromHBase(List<Delete> in) {
1343 List<TDelete> out = new ArrayList<>(in.size());
1344 for (Delete delete : in) {
1345 out.add(deleteFromHBase(delete));
1347 return out;
1350 public static TAppend appendFromHBase(Append in) throws IOException {
1351 TAppend out = new TAppend();
1352 out.setRow(in.getRow());
1354 if (in.getDurability() != Durability.USE_DEFAULT) {
1355 out.setDurability(durabilityFromHBase(in.getDurability()));
1357 for (Map.Entry<byte [], List<Cell>> entry : in.getFamilyCellMap().entrySet()) {
1358 byte[] family = entry.getKey();
1359 for (Cell cell : entry.getValue()) {
1360 TColumnValue columnValue = new TColumnValue();
1361 columnValue.setFamily(family)
1362 .setQualifier(CellUtil.cloneQualifier(cell))
1363 .setType(cell.getType().getCode())
1364 .setTimestamp(cell.getTimestamp())
1365 .setValue(CellUtil.cloneValue(cell));
1366 if (cell.getTagsLength() != 0) {
1367 columnValue.setTags(PrivateCellUtil.cloneTags(cell));
1369 out.addToColumns(columnValue);
1372 for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
1373 out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
1374 ByteBuffer.wrap(attribute.getValue()));
1376 try {
1377 CellVisibility cellVisibility = in.getCellVisibility();
1378 if (cellVisibility != null) {
1379 TCellVisibility tCellVisibility = new TCellVisibility();
1380 tCellVisibility.setExpression(cellVisibility.getExpression());
1381 out.setCellVisibility(tCellVisibility);
1383 } catch (DeserializationException e) {
1384 throw new RuntimeException(e);
1386 out.setReturnResults(in.isReturnResults());
1387 return out;
1390 public static TIncrement incrementFromHBase(Increment in) throws IOException {
1391 TIncrement out = new TIncrement();
1392 out.setRow(in.getRow());
1394 if (in.getDurability() != Durability.USE_DEFAULT) {
1395 out.setDurability(durabilityFromHBase(in.getDurability()));
1397 for (Map.Entry<byte [], List<Cell>> entry : in.getFamilyCellMap().entrySet()) {
1398 byte[] family = entry.getKey();
1399 for (Cell cell : entry.getValue()) {
1400 TColumnIncrement columnValue = new TColumnIncrement();
1401 columnValue.setFamily(family).setQualifier(CellUtil.cloneQualifier(cell));
1402 columnValue.setAmount(
1403 Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
1404 out.addToColumns(columnValue);
1407 for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
1408 out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
1409 ByteBuffer.wrap(attribute.getValue()));
1411 try {
1412 CellVisibility cellVisibility = in.getCellVisibility();
1413 if (cellVisibility != null) {
1414 TCellVisibility tCellVisibility = new TCellVisibility();
1415 tCellVisibility.setExpression(cellVisibility.getExpression());
1416 out.setCellVisibility(tCellVisibility);
1418 } catch (DeserializationException e) {
1419 throw new RuntimeException(e);
1421 out.setReturnResults(in.isReturnResults());
1422 return out;
1425 public static TRowMutations rowMutationsFromHBase(RowMutations in) {
1426 TRowMutations tRowMutations = new TRowMutations();
1427 tRowMutations.setRow(in.getRow());
1428 for (Mutation mutation : in.getMutations()) {
1429 TMutation tMutation = new TMutation();
1430 if (mutation instanceof Put) {
1431 tMutation.setPut(ThriftUtilities.putFromHBase((Put)mutation));
1432 } else if (mutation instanceof Delete) {
1433 tMutation.setDeleteSingle(ThriftUtilities.deleteFromHBase((Delete)mutation));
1434 } else {
1435 throw new IllegalArgumentException(
1436 "Only Put and Delete is supported in mutateRow, but muation=" + mutation);
1438 tRowMutations.addToMutations(tMutation);
1440 return tRowMutations;
1443 public static TCompareOperator compareOpFromHBase(CompareOperator compareOp) {
1444 switch (compareOp) {
1445 case LESS: return TCompareOperator.LESS;
1446 case LESS_OR_EQUAL: return TCompareOperator.LESS_OR_EQUAL;
1447 case EQUAL: return TCompareOperator.EQUAL;
1448 case NOT_EQUAL: return TCompareOperator.NOT_EQUAL;
1449 case GREATER_OR_EQUAL: return TCompareOperator.GREATER_OR_EQUAL;
1450 case GREATER: return TCompareOperator.GREATER;
1451 case NO_OP: return TCompareOperator.NO_OP;
1452 default: return null;
1455 public static List<ByteBuffer> splitKeyFromHBase(byte[][] in) {
1456 if (in == null || in.length == 0) {
1457 return null;
1459 List<ByteBuffer> out = new ArrayList<>(in.length);
1460 for (byte[] key : in) {
1461 out.add(ByteBuffer.wrap(key));
1463 return out;
1466 public static Result[] resultsFromThrift(List<TResult> in) {
1467 Result[] out = new Result[in.size()];
1468 int index = 0;
1469 for (TResult tResult : in) {
1470 out[index++] = resultFromThrift(tResult);
1472 return out;
1475 public static List<TGet> getsFromHBase(List<Get> in) {
1476 List<TGet> out = new ArrayList<>(in.size());
1477 for (Get get : in) {
1478 out.add(getFromHBase(get));
1480 return out;
1483 public static Set<TServerName> getServerNamesFromHBase(Set<ServerName> serverNames) {
1484 if (CollectionUtils.isEmpty(serverNames)) {
1485 return Collections.emptySet();
1487 return serverNames.stream().map(serverName -> {
1488 TServerName tServerName = new TServerName();
1489 tServerName.setHostName(serverName.getHostname());
1490 tServerName.setPort(serverName.getPort());
1491 tServerName.setStartCode(serverName.getStartcode());
1492 return tServerName;
1493 }).collect(Collectors.toSet());
1496 public static Set<ServerName> getServerNamesFromThrift(Set<TServerName> tServerNames) {
1497 if (CollectionUtils.isEmpty(tServerNames)) {
1498 return Collections.emptySet();
1500 return tServerNames.stream().map(tServerName ->
1501 ServerName.valueOf(tServerName.getHostName(),
1502 tServerName.getPort(),
1503 tServerName.getStartCode()))
1504 .collect(Collectors.toSet());
1507 public static TSlowLogQueryFilter getSlowLogQueryFromHBase(
1508 SlowLogQueryFilter slowLogQueryFilter) {
1509 TSlowLogQueryFilter tSlowLogQueryFilter = new TSlowLogQueryFilter();
1510 tSlowLogQueryFilter.setRegionName(slowLogQueryFilter.getRegionName());
1511 tSlowLogQueryFilter.setClientAddress(slowLogQueryFilter.getClientAddress());
1512 tSlowLogQueryFilter.setTableName(slowLogQueryFilter.getTableName());
1513 tSlowLogQueryFilter.setUserName(slowLogQueryFilter.getUserName());
1514 tSlowLogQueryFilter.setLimit(slowLogQueryFilter.getLimit());
1515 return tSlowLogQueryFilter;
1518 public static SlowLogQueryFilter getSlowLogQueryFromThrift(
1519 TSlowLogQueryFilter tSlowLogQueryFilter) {
1520 SlowLogQueryFilter slowLogQueryFilter = new SlowLogQueryFilter();
1521 slowLogQueryFilter.setRegionName(tSlowLogQueryFilter.getRegionName());
1522 slowLogQueryFilter.setClientAddress(tSlowLogQueryFilter.getClientAddress());
1523 slowLogQueryFilter.setTableName(tSlowLogQueryFilter.getTableName());
1524 slowLogQueryFilter.setUserName(tSlowLogQueryFilter.getUserName());
1525 slowLogQueryFilter.setLimit(tSlowLogQueryFilter.getLimit());
1526 return slowLogQueryFilter;
1529 public static List<TSlowLogRecord> getSlowLogRecordsFromHBase(
1530 List<SlowLogRecord> slowLogRecords) {
1531 if (CollectionUtils.isEmpty(slowLogRecords)) {
1532 return Collections.emptyList();
1534 return slowLogRecords.stream()
1535 .map(slowLogRecord -> {
1536 TSlowLogRecord tSlowLogRecord = new TSlowLogRecord();
1537 tSlowLogRecord.setCallDetails(slowLogRecord.getCallDetails());
1538 tSlowLogRecord.setClientAddress(slowLogRecord.getClientAddress());
1539 tSlowLogRecord.setMethodName(slowLogRecord.getMethodName());
1540 tSlowLogRecord.setMultiGetsCount(slowLogRecord.getMultiGetsCount());
1541 tSlowLogRecord.setMultiMutationsCount(slowLogRecord.getMultiMutationsCount());
1542 tSlowLogRecord.setMultiServiceCalls(slowLogRecord.getMultiServiceCalls());
1543 tSlowLogRecord.setParam(slowLogRecord.getParam());
1544 tSlowLogRecord.setProcessingTime(slowLogRecord.getProcessingTime());
1545 tSlowLogRecord.setQueueTime(slowLogRecord.getQueueTime());
1546 tSlowLogRecord.setRegionName(slowLogRecord.getRegionName());
1547 tSlowLogRecord.setResponseSize(slowLogRecord.getResponseSize());
1548 tSlowLogRecord.setServerClass(slowLogRecord.getServerClass());
1549 tSlowLogRecord.setStartTime(slowLogRecord.getStartTime());
1550 tSlowLogRecord.setUserName(slowLogRecord.getUserName());
1551 return tSlowLogRecord;
1552 }).collect(Collectors.toList());
1555 public static List<SlowLogRecord> getSlowLogRecordsFromThrift(
1556 List<TSlowLogRecord> tSlowLogRecords) {
1557 if (CollectionUtils.isEmpty(tSlowLogRecords)) {
1558 return Collections.emptyList();
1560 return tSlowLogRecords.stream()
1561 .map(tSlowLogRecord -> new SlowLogRecord.SlowLogRecordBuilder()
1562 .setCallDetails(tSlowLogRecord.getCallDetails())
1563 .setClientAddress(tSlowLogRecord.getClientAddress())
1564 .setMethodName(tSlowLogRecord.getMethodName())
1565 .setMultiGetsCount(tSlowLogRecord.getMultiGetsCount())
1566 .setMultiMutationsCount(tSlowLogRecord.getMultiMutationsCount())
1567 .setMultiServiceCalls(tSlowLogRecord.getMultiServiceCalls())
1568 .setParam(tSlowLogRecord.getParam())
1569 .setProcessingTime(tSlowLogRecord.getProcessingTime())
1570 .setQueueTime(tSlowLogRecord.getQueueTime())
1571 .setRegionName(tSlowLogRecord.getRegionName())
1572 .setResponseSize(tSlowLogRecord.getResponseSize())
1573 .setServerClass(tSlowLogRecord.getServerClass())
1574 .setStartTime(tSlowLogRecord.getStartTime())
1575 .setUserName(tSlowLogRecord.getUserName())
1576 .build())
1577 .collect(Collectors.toList());