001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import java.util.Map; 021import org.apache.hadoop.hbase.exceptions.DeserializationException; 022import org.apache.hadoop.hbase.filter.Filter; 023import org.apache.hadoop.hbase.io.TimeRange; 024import org.apache.hadoop.hbase.security.access.AccessControlConstants; 025import org.apache.hadoop.hbase.security.access.AccessControlUtil; 026import org.apache.hadoop.hbase.security.access.Permission; 027import org.apache.hadoop.hbase.security.visibility.Authorizations; 028import org.apache.hadoop.hbase.security.visibility.VisibilityConstants; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.apache.yetus.audience.InterfaceAudience; 031 032import org.apache.hbase.thirdparty.com.google.common.collect.ArrayListMultimap; 033import org.apache.hbase.thirdparty.com.google.common.collect.ListMultimap; 034import org.apache.hbase.thirdparty.com.google.common.collect.Maps; 035 036import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 037 038/** 039 * Base class for HBase read operations; e.g. Scan and Get. 040 */ 041@InterfaceAudience.Public 042public abstract class Query extends OperationWithAttributes { 043 private static final String ISOLATION_LEVEL = "_isolationlevel_"; 044 protected Filter filter = null; 045 protected int targetReplicaId = -1; 046 protected Consistency consistency = Consistency.STRONG; 047 protected Map<byte[], TimeRange> colFamTimeRangeMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR); 048 protected Boolean loadColumnFamiliesOnDemand = null; 049 050 /** 051 * n 052 */ 053 public Filter getFilter() { 054 return filter; 055 } 056 057 /** 058 * Apply the specified server-side filter when performing the Query. Only 059 * {@link Filter#filterCell(org.apache.hadoop.hbase.Cell)} is called AFTER all tests for ttl, 060 * column match, deletes and column family's max versions have been run. 061 * @param filter filter to run on the server 062 * @return this for invocation chaining 063 */ 064 public Query setFilter(Filter filter) { 065 this.filter = filter; 066 return this; 067 } 068 069 /** 070 * Sets the authorizations to be used by this Query n 071 */ 072 public Query setAuthorizations(Authorizations authorizations) { 073 this.setAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY, 074 ProtobufUtil.toAuthorizations(authorizations).toByteArray()); 075 return this; 076 } 077 078 /** Returns The authorizations this Query is associated with. n */ 079 public Authorizations getAuthorizations() throws DeserializationException { 080 byte[] authorizationsBytes = this.getAttribute(VisibilityConstants.VISIBILITY_LABELS_ATTR_KEY); 081 if (authorizationsBytes == null) return null; 082 return ProtobufUtil.toAuthorizations(authorizationsBytes); 083 } 084 085 /** Returns The serialized ACL for this operation, or null if none */ 086 public byte[] getACL() { 087 return getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL); 088 } 089 090 /** 091 * Set the ACL for the operation. 092 * @param user User short name 093 * @param perms Permissions for the user 094 */ 095 public Query setACL(String user, Permission perms) { 096 setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL, 097 AccessControlUtil.toUsersAndPermissions(user, perms).toByteArray()); 098 return this; 099 } 100 101 /** 102 * Set the ACL for the operation. 103 * @param perms A map of permissions for a user or users 104 */ 105 public Query setACL(Map<String, Permission> perms) { 106 ListMultimap<String, Permission> permMap = ArrayListMultimap.create(); 107 for (Map.Entry<String, Permission> entry : perms.entrySet()) { 108 permMap.put(entry.getKey(), entry.getValue()); 109 } 110 setAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL, 111 AccessControlUtil.toUsersAndPermissions(permMap).toByteArray()); 112 return this; 113 } 114 115 /** 116 * Returns the consistency level for this operation 117 * @return the consistency level 118 */ 119 public Consistency getConsistency() { 120 return consistency; 121 } 122 123 /** 124 * Sets the consistency level for this operation 125 * @param consistency the consistency level 126 */ 127 public Query setConsistency(Consistency consistency) { 128 this.consistency = consistency; 129 return this; 130 } 131 132 /** 133 * Specify region replica id where Query will fetch data from. Use this together with 134 * {@link #setConsistency(Consistency)} passing {@link Consistency#TIMELINE} to read data from a 135 * specific replicaId. <br> 136 * <b> Expert: </b>This is an advanced API exposed. Only use it if you know what you are doing n 137 */ 138 public Query setReplicaId(int Id) { 139 this.targetReplicaId = Id; 140 return this; 141 } 142 143 /** 144 * Returns region replica id where Query will fetch data from. 145 * @return region replica id or -1 if not set. 146 */ 147 public int getReplicaId() { 148 return this.targetReplicaId; 149 } 150 151 /** 152 * Set the isolation level for this query. If the isolation level is set to READ_UNCOMMITTED, then 153 * this query will return data from committed and uncommitted transactions. If the isolation level 154 * is set to READ_COMMITTED, then this query will return data from committed transactions only. If 155 * a isolation level is not explicitly set on a Query, then it is assumed to be READ_COMMITTED. 156 * @param level IsolationLevel for this query 157 */ 158 public Query setIsolationLevel(IsolationLevel level) { 159 setAttribute(ISOLATION_LEVEL, level.toBytes()); 160 return this; 161 } 162 163 /** 164 * Returns The isolation level of this query. If no isolation level was set for this query object, 165 * then it returns READ_COMMITTED. 166 */ 167 public IsolationLevel getIsolationLevel() { 168 byte[] attr = getAttribute(ISOLATION_LEVEL); 169 return attr == null ? IsolationLevel.READ_COMMITTED : IsolationLevel.fromBytes(attr); 170 } 171 172 /** 173 * Set the value indicating whether loading CFs on demand should be allowed (cluster default is 174 * false). On-demand CF loading doesn't load column families until necessary, e.g. if you filter 175 * on one column, the other column family data will be loaded only for the rows that are included 176 * in result, not all rows like in normal case. With column-specific filters, like 177 * SingleColumnValueFilter w/filterIfMissing == true, this can deliver huge perf gains when 178 * there's a cf with lots of data; however, it can also lead to some inconsistent results, as 179 * follows: - if someone does a concurrent update to both column families in question you may get 180 * a row that never existed, e.g. for { rowKey = 5, { cat_videos => 1 }, { video => "my cat" 181 * } } someone puts rowKey 5 with { cat_videos => 0 }, { video => "my dog" }, concurrent 182 * scan filtering on "cat_videos == 1" can get { rowKey = 5, { cat_videos => 1 }, { video => 183 * "my dog" } }. - if there's a concurrent split and you have more than 2 column families, some 184 * rows may be missing some column families. 185 */ 186 public Query setLoadColumnFamiliesOnDemand(boolean value) { 187 this.loadColumnFamiliesOnDemand = value; 188 return this; 189 } 190 191 /** 192 * Get the raw loadColumnFamiliesOnDemand setting; if it's not set, can be null. 193 */ 194 public Boolean getLoadColumnFamiliesOnDemandValue() { 195 return this.loadColumnFamiliesOnDemand; 196 } 197 198 /** 199 * Get the logical value indicating whether on-demand CF loading should be allowed. 200 */ 201 public boolean doLoadColumnFamiliesOnDemand() { 202 return (this.loadColumnFamiliesOnDemand != null) && this.loadColumnFamiliesOnDemand; 203 } 204 205 /** 206 * Get versions of columns only within the specified timestamp range, [minStamp, maxStamp) on a 207 * per CF bases. Note, default maximum versions to return is 1. If your time range spans more than 208 * one version and you want all versions returned, up the number of versions beyond the default. 209 * Column Family time ranges take precedence over the global time range. 210 * @param cf the column family for which you want to restrict 211 * @param minStamp minimum timestamp value, inclusive 212 * @param maxStamp maximum timestamp value, exclusive n 213 */ 214 215 public Query setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) { 216 colFamTimeRangeMap.put(cf, new TimeRange(minStamp, maxStamp)); 217 return this; 218 } 219 220 /** Returns A map of column families to time ranges */ 221 public Map<byte[], TimeRange> getColumnFamilyTimeRange() { 222 return this.colFamTimeRangeMap; 223 } 224}