View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.coprocessor;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.fs.Path;
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.client.HTable;
24  import org.apache.hadoop.hbase.util.Methods;
25  import org.apache.hadoop.hbase.util.Pair;
26  import org.apache.hadoop.security.token.Token;
27  
28  import java.io.IOException;
29  import java.util.List;
30  
31  public class SecureBulkLoadClient {
32    private static Class protocolClazz;
33    private static Class endpointClazz;
34    private Object proxy;
35    private HTable table;
36  
37    public SecureBulkLoadClient(HTable table) throws IOException {
38      this(table, HConstants.EMPTY_START_ROW);
39    }
40  
41    public SecureBulkLoadClient(HTable table, byte[] startRow) throws IOException {
42      try {
43        protocolClazz = protocolClazz!=null?protocolClazz:
44            Class.forName("org.apache.hadoop.hbase.security.access.SecureBulkLoadProtocol");
45        endpointClazz = endpointClazz!=null?endpointClazz:
46            Class.forName("org.apache.hadoop.hbase.security.access.SecureBulkLoadEndpoint");
47        proxy = table.coprocessorProxy(protocolClazz, startRow);
48        this.table = table;
49      } catch (ClassNotFoundException e) {
50        throw new IOException("Failed to initialize SecureBulkLoad", e);
51      }
52    }
53  
54    public String prepareBulkLoad(byte[] tableName) throws IOException {
55      try {
56        String bulkToken = (String) Methods.call(protocolClazz, proxy,
57            "prepareBulkLoad", new Class[]{byte[].class}, new Object[]{tableName});
58        return bulkToken;
59      } catch (Exception e) {
60        throw new IOException("Failed to prepareBulkLoad", e);
61      }
62    }
63  
64    public void cleanupBulkLoad(String bulkToken) throws IOException {
65      try {
66        Methods.call(protocolClazz, proxy,
67            "cleanupBulkLoad", new Class[]{String.class},new Object[]{bulkToken});
68      } catch (Exception e) {
69        throw new IOException("Failed to prepareBulkLoad", e);
70      }
71    }
72  
73    public boolean bulkLoadHFiles(List<Pair<byte[], String>> familyPaths, Token<?> userToken,
74        String bulkToken) throws IOException {
75      return bulkLoadHFiles(familyPaths, userToken, bulkToken, false);
76    }
77  
78    public boolean bulkLoadHFiles(List<Pair<byte[], String>> familyPaths, Token<?> userToken,
79        String bulkToken, boolean assignSeqNum) throws IOException {
80      try {
81        return (Boolean) Methods.call(protocolClazz, proxy, "bulkLoadHFiles", new Class[] {
82            List.class, Token.class, String.class, boolean.class },
83          new Object[] { familyPaths, userToken, bulkToken, assignSeqNum });
84      } catch (Exception e) {
85        throw new IOException("Failed to bulkLoadHFiles", e);
86      }
87    }
88  
89    public Path getStagingPath(String bulkToken, byte[] family) throws IOException {
90      try {
91        return (Path)Methods.call(endpointClazz, null, "getStagingPath",
92            new Class[]{Configuration.class, String.class, byte[].class},
93            new Object[]{table.getConfiguration(), bulkToken, family});
94      } catch (Exception e) {
95        throw new IOException("Failed to getStagingPath", e);
96      }
97    }
98  }