blob: 1e836c0f8c069cfb36086c8952122e0284b07811 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Generated by javadoc (17) -->
<title>Source code</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="source: package: org.apache.hadoop.hbase.mapreduce, class: ExportUtils">
<meta name="generator" content="javadoc/SourceToHTMLConverter">
<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
</head>
<body class="source-page">
<main role="main">
<div class="source-container">
<pre><span class="source-line-no">001</span><span id="line-1">/*</span>
<span class="source-line-no">002</span><span id="line-2"> * Licensed to the Apache Software Foundation (ASF) under one</span>
<span class="source-line-no">003</span><span id="line-3"> * or more contributor license agreements. See the NOTICE file</span>
<span class="source-line-no">004</span><span id="line-4"> * distributed with this work for additional information</span>
<span class="source-line-no">005</span><span id="line-5"> * regarding copyright ownership. The ASF licenses this file</span>
<span class="source-line-no">006</span><span id="line-6"> * to you under the Apache License, Version 2.0 (the</span>
<span class="source-line-no">007</span><span id="line-7"> * "License"); you may not use this file except in compliance</span>
<span class="source-line-no">008</span><span id="line-8"> * with the License. You may obtain a copy of the License at</span>
<span class="source-line-no">009</span><span id="line-9"> *</span>
<span class="source-line-no">010</span><span id="line-10"> * http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="source-line-no">011</span><span id="line-11"> *</span>
<span class="source-line-no">012</span><span id="line-12"> * Unless required by applicable law or agreed to in writing, software</span>
<span class="source-line-no">013</span><span id="line-13"> * distributed under the License is distributed on an "AS IS" BASIS,</span>
<span class="source-line-no">014</span><span id="line-14"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="source-line-no">015</span><span id="line-15"> * See the License for the specific language governing permissions and</span>
<span class="source-line-no">016</span><span id="line-16"> * limitations under the License.</span>
<span class="source-line-no">017</span><span id="line-17"> */</span>
<span class="source-line-no">018</span><span id="line-18">package org.apache.hadoop.hbase.mapreduce;</span>
<span class="source-line-no">019</span><span id="line-19"></span>
<span class="source-line-no">020</span><span id="line-20">import java.io.IOException;</span>
<span class="source-line-no">021</span><span id="line-21">import java.util.Arrays;</span>
<span class="source-line-no">022</span><span id="line-22">import java.util.List;</span>
<span class="source-line-no">023</span><span id="line-23">import org.apache.hadoop.conf.Configuration;</span>
<span class="source-line-no">024</span><span id="line-24">import org.apache.hadoop.fs.Path;</span>
<span class="source-line-no">025</span><span id="line-25">import org.apache.hadoop.hbase.CompareOperator;</span>
<span class="source-line-no">026</span><span id="line-26">import org.apache.hadoop.hbase.HConstants;</span>
<span class="source-line-no">027</span><span id="line-27">import org.apache.hadoop.hbase.TableName;</span>
<span class="source-line-no">028</span><span id="line-28">import org.apache.hadoop.hbase.client.Scan;</span>
<span class="source-line-no">029</span><span id="line-29">import org.apache.hadoop.hbase.filter.Filter;</span>
<span class="source-line-no">030</span><span id="line-30">import org.apache.hadoop.hbase.filter.IncompatibleFilterException;</span>
<span class="source-line-no">031</span><span id="line-31">import org.apache.hadoop.hbase.filter.PrefixFilter;</span>
<span class="source-line-no">032</span><span id="line-32">import org.apache.hadoop.hbase.filter.RegexStringComparator;</span>
<span class="source-line-no">033</span><span id="line-33">import org.apache.hadoop.hbase.filter.RowFilter;</span>
<span class="source-line-no">034</span><span id="line-34">import org.apache.hadoop.hbase.security.visibility.Authorizations;</span>
<span class="source-line-no">035</span><span id="line-35">import org.apache.hadoop.hbase.util.Bytes;</span>
<span class="source-line-no">036</span><span id="line-36">import org.apache.hadoop.hbase.util.Triple;</span>
<span class="source-line-no">037</span><span id="line-37">import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;</span>
<span class="source-line-no">038</span><span id="line-38">import org.apache.yetus.audience.InterfaceAudience;</span>
<span class="source-line-no">039</span><span id="line-39">import org.slf4j.Logger;</span>
<span class="source-line-no">040</span><span id="line-40">import org.slf4j.LoggerFactory;</span>
<span class="source-line-no">041</span><span id="line-41"></span>
<span class="source-line-no">042</span><span id="line-42">/**</span>
<span class="source-line-no">043</span><span id="line-43"> * Some helper methods are used by {@link org.apache.hadoop.hbase.mapreduce.Export} and</span>
<span class="source-line-no">044</span><span id="line-44"> * org.apache.hadoop.hbase.coprocessor.Export (in hbase-endpooint).</span>
<span class="source-line-no">045</span><span id="line-45"> */</span>
<span class="source-line-no">046</span><span id="line-46">@InterfaceAudience.Private</span>
<span class="source-line-no">047</span><span id="line-47">public final class ExportUtils {</span>
<span class="source-line-no">048</span><span id="line-48"> private static final Logger LOG = LoggerFactory.getLogger(ExportUtils.class);</span>
<span class="source-line-no">049</span><span id="line-49"> public static final String RAW_SCAN = "hbase.mapreduce.include.deleted.rows";</span>
<span class="source-line-no">050</span><span id="line-50"> public static final String EXPORT_BATCHING = "hbase.export.scanner.batch";</span>
<span class="source-line-no">051</span><span id="line-51"> public static final String EXPORT_CACHING = "hbase.export.scanner.caching";</span>
<span class="source-line-no">052</span><span id="line-52"> public static final String EXPORT_VISIBILITY_LABELS = "hbase.export.visibility.labels";</span>
<span class="source-line-no">053</span><span id="line-53"></span>
<span class="source-line-no">054</span><span id="line-54"> /**</span>
<span class="source-line-no">055</span><span id="line-55"> * Common usage for other export tools.</span>
<span class="source-line-no">056</span><span id="line-56"> * @param errorMsg Error message. Can be null.</span>
<span class="source-line-no">057</span><span id="line-57"> */</span>
<span class="source-line-no">058</span><span id="line-58"> public static void usage(final String errorMsg) {</span>
<span class="source-line-no">059</span><span id="line-59"> if (errorMsg != null &amp;&amp; errorMsg.length() &gt; 0) {</span>
<span class="source-line-no">060</span><span id="line-60"> System.err.println("ERROR: " + errorMsg);</span>
<span class="source-line-no">061</span><span id="line-61"> }</span>
<span class="source-line-no">062</span><span id="line-62"> System.err.println("Usage: Export [-D &lt;property=value&gt;]* &lt;tablename&gt; &lt;outputdir&gt; [&lt;versions&gt; "</span>
<span class="source-line-no">063</span><span id="line-63"> + "[&lt;starttime&gt; [&lt;endtime&gt;]] [^[regex pattern] or [Prefix] to filter]]\n");</span>
<span class="source-line-no">064</span><span id="line-64"> System.err.println(" Note: -D properties will be applied to the conf used. ");</span>
<span class="source-line-no">065</span><span id="line-65"> System.err.println(" For example: ");</span>
<span class="source-line-no">066</span><span id="line-66"> System.err.println(" -D " + FileOutputFormat.COMPRESS + "=true");</span>
<span class="source-line-no">067</span><span id="line-67"> System.err.println(</span>
<span class="source-line-no">068</span><span id="line-68"> " -D " + FileOutputFormat.COMPRESS_CODEC + "=org.apache.hadoop.io.compress.GzipCodec");</span>
<span class="source-line-no">069</span><span id="line-69"> System.err.println(" -D " + FileOutputFormat.COMPRESS_TYPE + "=BLOCK");</span>
<span class="source-line-no">070</span><span id="line-70"> System.err.println(" Additionally, the following SCAN properties can be specified");</span>
<span class="source-line-no">071</span><span id="line-71"> System.err.println(" to control/limit what is exported..");</span>
<span class="source-line-no">072</span><span id="line-72"> System.err</span>
<span class="source-line-no">073</span><span id="line-73"> .println(" -D " + TableInputFormat.SCAN_COLUMN_FAMILY + "=&lt;family1&gt;,&lt;family2&gt;, ...");</span>
<span class="source-line-no">074</span><span id="line-74"> System.err.println(" -D " + RAW_SCAN + "=true");</span>
<span class="source-line-no">075</span><span id="line-75"> System.err.println(" -D " + TableInputFormat.SCAN_ROW_START + "=&lt;ROWSTART&gt;");</span>
<span class="source-line-no">076</span><span id="line-76"> System.err.println(" -D " + TableInputFormat.SCAN_ROW_STOP + "=&lt;ROWSTOP&gt;");</span>
<span class="source-line-no">077</span><span id="line-77"> System.err.println(" -D " + HConstants.HBASE_CLIENT_SCANNER_CACHING + "=100");</span>
<span class="source-line-no">078</span><span id="line-78"> System.err.println(" -D " + EXPORT_VISIBILITY_LABELS + "=&lt;labels&gt;");</span>
<span class="source-line-no">079</span><span id="line-79"> System.err.println("For tables with very wide rows consider setting the batch size as below:\n"</span>
<span class="source-line-no">080</span><span id="line-80"> + " -D " + EXPORT_BATCHING + "=10\n" + " -D " + EXPORT_CACHING + "=100");</span>
<span class="source-line-no">081</span><span id="line-81"> }</span>
<span class="source-line-no">082</span><span id="line-82"></span>
<span class="source-line-no">083</span><span id="line-83"> private static Filter getExportFilter(String[] args) {</span>
<span class="source-line-no">084</span><span id="line-84"> Filter exportFilter;</span>
<span class="source-line-no">085</span><span id="line-85"> String filterCriteria = (args.length &gt; 5) ? args[5] : null;</span>
<span class="source-line-no">086</span><span id="line-86"> if (filterCriteria == null) return null;</span>
<span class="source-line-no">087</span><span id="line-87"> if (filterCriteria.startsWith("^")) {</span>
<span class="source-line-no">088</span><span id="line-88"> String regexPattern = filterCriteria.substring(1, filterCriteria.length());</span>
<span class="source-line-no">089</span><span id="line-89"> exportFilter = new RowFilter(CompareOperator.EQUAL, new RegexStringComparator(regexPattern));</span>
<span class="source-line-no">090</span><span id="line-90"> } else {</span>
<span class="source-line-no">091</span><span id="line-91"> exportFilter = new PrefixFilter(Bytes.toBytesBinary(filterCriteria));</span>
<span class="source-line-no">092</span><span id="line-92"> }</span>
<span class="source-line-no">093</span><span id="line-93"> return exportFilter;</span>
<span class="source-line-no">094</span><span id="line-94"> }</span>
<span class="source-line-no">095</span><span id="line-95"></span>
<span class="source-line-no">096</span><span id="line-96"> public static boolean isValidArguements(String[] args) {</span>
<span class="source-line-no">097</span><span id="line-97"> return args != null &amp;&amp; args.length &gt;= 2;</span>
<span class="source-line-no">098</span><span id="line-98"> }</span>
<span class="source-line-no">099</span><span id="line-99"></span>
<span class="source-line-no">100</span><span id="line-100"> public static Triple&lt;TableName, Scan, Path&gt; getArgumentsFromCommandLine(Configuration conf,</span>
<span class="source-line-no">101</span><span id="line-101"> String[] args) throws IOException {</span>
<span class="source-line-no">102</span><span id="line-102"> if (!isValidArguements(args)) {</span>
<span class="source-line-no">103</span><span id="line-103"> return null;</span>
<span class="source-line-no">104</span><span id="line-104"> }</span>
<span class="source-line-no">105</span><span id="line-105"> return new Triple&lt;&gt;(TableName.valueOf(args[0]), getScanFromCommandLine(conf, args),</span>
<span class="source-line-no">106</span><span id="line-106"> new Path(args[1]));</span>
<span class="source-line-no">107</span><span id="line-107"> }</span>
<span class="source-line-no">108</span><span id="line-108"></span>
<span class="source-line-no">109</span><span id="line-109"> static Scan getScanFromCommandLine(Configuration conf, String[] args) throws IOException {</span>
<span class="source-line-no">110</span><span id="line-110"> Scan s = new Scan();</span>
<span class="source-line-no">111</span><span id="line-111"> // Optional arguments.</span>
<span class="source-line-no">112</span><span id="line-112"> // Set Scan Versions</span>
<span class="source-line-no">113</span><span id="line-113"> int versions = args.length &gt; 2 ? Integer.parseInt(args[2]) : 1;</span>
<span class="source-line-no">114</span><span id="line-114"> s.readVersions(versions);</span>
<span class="source-line-no">115</span><span id="line-115"> // Set Scan Range</span>
<span class="source-line-no">116</span><span id="line-116"> long startTime = args.length &gt; 3 ? Long.parseLong(args[3]) : 0L;</span>
<span class="source-line-no">117</span><span id="line-117"> long endTime = args.length &gt; 4 ? Long.parseLong(args[4]) : Long.MAX_VALUE;</span>
<span class="source-line-no">118</span><span id="line-118"> s.setTimeRange(startTime, endTime);</span>
<span class="source-line-no">119</span><span id="line-119"> // Set cache blocks</span>
<span class="source-line-no">120</span><span id="line-120"> s.setCacheBlocks(false);</span>
<span class="source-line-no">121</span><span id="line-121"> // set Start and Stop row</span>
<span class="source-line-no">122</span><span id="line-122"> if (conf.get(TableInputFormat.SCAN_ROW_START) != null) {</span>
<span class="source-line-no">123</span><span id="line-123"> s.withStartRow(Bytes.toBytesBinary(conf.get(TableInputFormat.SCAN_ROW_START)));</span>
<span class="source-line-no">124</span><span id="line-124"> }</span>
<span class="source-line-no">125</span><span id="line-125"> if (conf.get(TableInputFormat.SCAN_ROW_STOP) != null) {</span>
<span class="source-line-no">126</span><span id="line-126"> s.withStopRow(Bytes.toBytesBinary(conf.get(TableInputFormat.SCAN_ROW_STOP)));</span>
<span class="source-line-no">127</span><span id="line-127"> }</span>
<span class="source-line-no">128</span><span id="line-128"> // Set Scan Column Family</span>
<span class="source-line-no">129</span><span id="line-129"> boolean raw = Boolean.parseBoolean(conf.get(RAW_SCAN));</span>
<span class="source-line-no">130</span><span id="line-130"> if (raw) {</span>
<span class="source-line-no">131</span><span id="line-131"> s.setRaw(raw);</span>
<span class="source-line-no">132</span><span id="line-132"> }</span>
<span class="source-line-no">133</span><span id="line-133"> for (String columnFamily : conf.getTrimmedStrings(TableInputFormat.SCAN_COLUMN_FAMILY)) {</span>
<span class="source-line-no">134</span><span id="line-134"> s.addFamily(Bytes.toBytes(columnFamily));</span>
<span class="source-line-no">135</span><span id="line-135"> }</span>
<span class="source-line-no">136</span><span id="line-136"> // Set RowFilter or Prefix Filter if applicable.</span>
<span class="source-line-no">137</span><span id="line-137"> Filter exportFilter = getExportFilter(args);</span>
<span class="source-line-no">138</span><span id="line-138"> if (exportFilter != null) {</span>
<span class="source-line-no">139</span><span id="line-139"> LOG.info("Setting Scan Filter for Export.");</span>
<span class="source-line-no">140</span><span id="line-140"> s.setFilter(exportFilter);</span>
<span class="source-line-no">141</span><span id="line-141"> }</span>
<span class="source-line-no">142</span><span id="line-142"> List&lt;String&gt; labels = null;</span>
<span class="source-line-no">143</span><span id="line-143"> if (conf.get(EXPORT_VISIBILITY_LABELS) != null) {</span>
<span class="source-line-no">144</span><span id="line-144"> labels = Arrays.asList(conf.getStrings(EXPORT_VISIBILITY_LABELS));</span>
<span class="source-line-no">145</span><span id="line-145"> if (!labels.isEmpty()) {</span>
<span class="source-line-no">146</span><span id="line-146"> s.setAuthorizations(new Authorizations(labels));</span>
<span class="source-line-no">147</span><span id="line-147"> }</span>
<span class="source-line-no">148</span><span id="line-148"> }</span>
<span class="source-line-no">149</span><span id="line-149"></span>
<span class="source-line-no">150</span><span id="line-150"> int batching = conf.getInt(EXPORT_BATCHING, -1);</span>
<span class="source-line-no">151</span><span id="line-151"> if (batching != -1) {</span>
<span class="source-line-no">152</span><span id="line-152"> try {</span>
<span class="source-line-no">153</span><span id="line-153"> s.setBatch(batching);</span>
<span class="source-line-no">154</span><span id="line-154"> } catch (IncompatibleFilterException e) {</span>
<span class="source-line-no">155</span><span id="line-155"> LOG.error("Batching could not be set", e);</span>
<span class="source-line-no">156</span><span id="line-156"> }</span>
<span class="source-line-no">157</span><span id="line-157"> }</span>
<span class="source-line-no">158</span><span id="line-158"></span>
<span class="source-line-no">159</span><span id="line-159"> int caching = conf.getInt(EXPORT_CACHING, 100);</span>
<span class="source-line-no">160</span><span id="line-160"> if (caching != -1) {</span>
<span class="source-line-no">161</span><span id="line-161"> try {</span>
<span class="source-line-no">162</span><span id="line-162"> s.setCaching(caching);</span>
<span class="source-line-no">163</span><span id="line-163"> } catch (IncompatibleFilterException e) {</span>
<span class="source-line-no">164</span><span id="line-164"> LOG.error("Caching could not be set", e);</span>
<span class="source-line-no">165</span><span id="line-165"> }</span>
<span class="source-line-no">166</span><span id="line-166"> }</span>
<span class="source-line-no">167</span><span id="line-167"> LOG.info("versions=" + versions + ", starttime=" + startTime + ", endtime=" + endTime</span>
<span class="source-line-no">168</span><span id="line-168"> + ", keepDeletedCells=" + raw + ", visibility labels=" + labels);</span>
<span class="source-line-no">169</span><span id="line-169"> return s;</span>
<span class="source-line-no">170</span><span id="line-170"> }</span>
<span class="source-line-no">171</span><span id="line-171"></span>
<span class="source-line-no">172</span><span id="line-172"> private ExportUtils() {</span>
<span class="source-line-no">173</span><span id="line-173"> }</span>
<span class="source-line-no">174</span><span id="line-174">}</span>
</pre>
</div>
</main>
</body>
</html>