001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.mapreduce; 020 021import java.io.IOException; 022import java.util.Arrays; 023import java.util.List; 024 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.CompareOperator; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.yetus.audience.InterfaceAudience; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033import org.apache.hadoop.hbase.client.Scan; 034import org.apache.hadoop.hbase.filter.Filter; 035import org.apache.hadoop.hbase.filter.IncompatibleFilterException; 036import org.apache.hadoop.hbase.filter.PrefixFilter; 037import org.apache.hadoop.hbase.filter.RegexStringComparator; 038import org.apache.hadoop.hbase.filter.RowFilter; 039import org.apache.hadoop.hbase.security.visibility.Authorizations; 040import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 041import org.apache.hadoop.hbase.util.Bytes; 042import org.apache.hadoop.hbase.util.Triple; 043import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 044 045/** 046 * Some helper methods are used by {@link org.apache.hadoop.hbase.mapreduce.Export} 047 * and org.apache.hadoop.hbase.coprocessor.Export (in hbase-endpooint). 048 */ 049@InterfaceAudience.Private 050public final class ExportUtils { 051 private static final Logger LOG = LoggerFactory.getLogger(ExportUtils.class); 052 public static final String RAW_SCAN = "hbase.mapreduce.include.deleted.rows"; 053 public static final String EXPORT_BATCHING = "hbase.export.scanner.batch"; 054 public static final String EXPORT_CACHING = "hbase.export.scanner.caching"; 055 public static final String EXPORT_VISIBILITY_LABELS = "hbase.export.visibility.labels"; 056 /** 057 * Common usage for other export tools. 058 * @param errorMsg Error message. Can be null. 059 */ 060 public static void usage(final String errorMsg) { 061 if (errorMsg != null && errorMsg.length() > 0) { 062 System.err.println("ERROR: " + errorMsg); 063 } 064 System.err.println("Usage: Export [-D <property=value>]* <tablename> <outputdir> [<versions> " + 065 "[<starttime> [<endtime>]] [^[regex pattern] or [Prefix] to filter]]\n"); 066 System.err.println(" Note: -D properties will be applied to the conf used. "); 067 System.err.println(" For example: "); 068 System.err.println(" -D " + FileOutputFormat.COMPRESS + "=true"); 069 System.err.println(" -D " + FileOutputFormat.COMPRESS_CODEC + "=org.apache.hadoop.io.compress.GzipCodec"); 070 System.err.println(" -D " + FileOutputFormat.COMPRESS_TYPE + "=BLOCK"); 071 System.err.println(" Additionally, the following SCAN properties can be specified"); 072 System.err.println(" to control/limit what is exported.."); 073 System.err.println(" -D " + TableInputFormat.SCAN_COLUMN_FAMILY + "=<family1>,<family2>, ..."); 074 System.err.println(" -D " + RAW_SCAN + "=true"); 075 System.err.println(" -D " + TableInputFormat.SCAN_ROW_START + "=<ROWSTART>"); 076 System.err.println(" -D " + TableInputFormat.SCAN_ROW_STOP + "=<ROWSTOP>"); 077 System.err.println(" -D " + HConstants.HBASE_CLIENT_SCANNER_CACHING + "=100"); 078 System.err.println(" -D " + EXPORT_VISIBILITY_LABELS + "=<labels>"); 079 System.err.println("For tables with very wide rows consider setting the batch size as below:\n" 080 + " -D " + EXPORT_BATCHING + "=10\n" 081 + " -D " + EXPORT_CACHING + "=100"); 082 } 083 084 private static Filter getExportFilter(String[] args) { 085 Filter exportFilter; 086 String filterCriteria = (args.length > 5) ? args[5]: null; 087 if (filterCriteria == null) return null; 088 if (filterCriteria.startsWith("^")) { 089 String regexPattern = filterCriteria.substring(1, filterCriteria.length()); 090 exportFilter = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(regexPattern)); 091 } else { 092 exportFilter = new PrefixFilter(Bytes.toBytesBinary(filterCriteria)); 093 } 094 return exportFilter; 095 } 096 097 public static boolean isValidArguements(String[] args) { 098 return args != null && args.length >= 2; 099 } 100 101 public static Triple<TableName, Scan, Path> getArgumentsFromCommandLine( 102 Configuration conf, String[] args) throws IOException { 103 if (!isValidArguements(args)) { 104 return null; 105 } 106 return new Triple<>(TableName.valueOf(args[0]), getScanFromCommandLine(conf, args), new Path(args[1])); 107 } 108 109 @VisibleForTesting 110 static Scan getScanFromCommandLine(Configuration conf, String[] args) throws IOException { 111 Scan s = new Scan(); 112 // Optional arguments. 113 // Set Scan Versions 114 int versions = args.length > 2? Integer.parseInt(args[2]): 1; 115 s.setMaxVersions(versions); 116 // Set Scan Range 117 long startTime = args.length > 3? Long.parseLong(args[3]): 0L; 118 long endTime = args.length > 4? Long.parseLong(args[4]): Long.MAX_VALUE; 119 s.setTimeRange(startTime, endTime); 120 // Set cache blocks 121 s.setCacheBlocks(false); 122 // set Start and Stop row 123 if (conf.get(TableInputFormat.SCAN_ROW_START) != null) { 124 s.setStartRow(Bytes.toBytesBinary(conf.get(TableInputFormat.SCAN_ROW_START))); 125 } 126 if (conf.get(TableInputFormat.SCAN_ROW_STOP) != null) { 127 s.setStopRow(Bytes.toBytesBinary(conf.get(TableInputFormat.SCAN_ROW_STOP))); 128 } 129 // Set Scan Column Family 130 boolean raw = Boolean.parseBoolean(conf.get(RAW_SCAN)); 131 if (raw) { 132 s.setRaw(raw); 133 } 134 for (String columnFamily : conf.getTrimmedStrings(TableInputFormat.SCAN_COLUMN_FAMILY)) { 135 s.addFamily(Bytes.toBytes(columnFamily)); 136 } 137 // Set RowFilter or Prefix Filter if applicable. 138 Filter exportFilter = getExportFilter(args); 139 if (exportFilter!= null) { 140 LOG.info("Setting Scan Filter for Export."); 141 s.setFilter(exportFilter); 142 } 143 List<String> labels = null; 144 if (conf.get(EXPORT_VISIBILITY_LABELS) != null) { 145 labels = Arrays.asList(conf.getStrings(EXPORT_VISIBILITY_LABELS)); 146 if (!labels.isEmpty()) { 147 s.setAuthorizations(new Authorizations(labels)); 148 } 149 } 150 151 int batching = conf.getInt(EXPORT_BATCHING, -1); 152 if (batching != -1) { 153 try { 154 s.setBatch(batching); 155 } catch (IncompatibleFilterException e) { 156 LOG.error("Batching could not be set", e); 157 } 158 } 159 160 int caching = conf.getInt(EXPORT_CACHING, 100); 161 if (caching != -1) { 162 try { 163 s.setCaching(caching); 164 } catch (IncompatibleFilterException e) { 165 LOG.error("Caching could not be set", e); 166 } 167 } 168 LOG.info("versions=" + versions + ", starttime=" + startTime 169 + ", endtime=" + endTime + ", keepDeletedCells=" + raw 170 + ", visibility labels=" + labels); 171 return s; 172 } 173 174 private ExportUtils() { 175 } 176}