001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to you under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.coprocessor.example;
018
019import java.io.IOException;
020import java.util.Optional;
021import org.apache.hadoop.hbase.CoprocessorEnvironment;
022import org.apache.hadoop.hbase.client.Scan;
023import org.apache.hadoop.hbase.coprocessor.ObserverContext;
024import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
025import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
026import org.apache.hadoop.hbase.coprocessor.RegionObserver;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * A RegionObserver which modifies incoming Scan requests to include additional
032 * columns than what the user actually requested.
033 */
034@InterfaceAudience.Private
035public class ScanModifyingObserver implements RegionCoprocessor, RegionObserver {
036
037  public static final String FAMILY_TO_ADD_KEY = "hbase.examples.coprocessor.scanmodifying.family";
038  public static final String QUALIFIER_TO_ADD_KEY =
039      "hbase.examples.coprocessor.scanmodifying.qualifier";
040
041  private byte[] FAMILY_TO_ADD = null;
042  private byte[] QUALIFIER_TO_ADD = null;
043
044  @Override
045  public void start(
046      @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(
060      ObserverContext<RegionCoprocessorEnvironment> c, Scan scan) throws IOException {
061    // Add another family:qualifier
062    scan.addColumn(FAMILY_TO_ADD, QUALIFIER_TO_ADD);
063  }
064}