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.coprocessor.example; 019 020import java.io.IOException; 021import java.util.Optional; 022import org.apache.hadoop.hbase.CoprocessorEnvironment; 023import org.apache.hadoop.hbase.client.Scan; 024import org.apache.hadoop.hbase.coprocessor.ObserverContext; 025import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 026import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 027import org.apache.hadoop.hbase.coprocessor.RegionObserver; 028import org.apache.hadoop.hbase.util.Bytes; 029import org.apache.yetus.audience.InterfaceAudience; 030 031/** 032 * A RegionObserver which modifies incoming Scan requests to include additional columns than what 033 * the user actually requested. 034 */ 035@InterfaceAudience.Private 036public class ScanModifyingObserver implements RegionCoprocessor, RegionObserver { 037 038 public static final String FAMILY_TO_ADD_KEY = "hbase.examples.coprocessor.scanmodifying.family"; 039 public static final String QUALIFIER_TO_ADD_KEY = 040 "hbase.examples.coprocessor.scanmodifying.qualifier"; 041 042 private byte[] FAMILY_TO_ADD = null; 043 private byte[] QUALIFIER_TO_ADD = null; 044 045 @Override 046 public void start(@SuppressWarnings("rawtypes") CoprocessorEnvironment env) throws IOException { 047 RegionCoprocessorEnvironment renv = (RegionCoprocessorEnvironment) env; 048 FAMILY_TO_ADD = Bytes.toBytes(renv.getConfiguration().get(FAMILY_TO_ADD_KEY)); 049 QUALIFIER_TO_ADD = Bytes.toBytes(renv.getConfiguration().get(QUALIFIER_TO_ADD_KEY)); 050 } 051 052 @Override 053 public Optional<RegionObserver> getRegionObserver() { 054 // Extremely important to be sure that the coprocessor is invoked as a RegionObserver 055 return Optional.of(this); 056 } 057 058 @Override 059 public void preScannerOpen(ObserverContext<? extends RegionCoprocessorEnvironment> c, Scan scan) 060 throws IOException { 061 // Add another family:qualifier 062 scan.addColumn(FAMILY_TO_ADD, QUALIFIER_TO_ADD); 063 } 064}