View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.client;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.classification.InterfaceStability;
26  import org.apache.hadoop.conf.Configuration;
27  
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.rest.Constants;
30  import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  @InterfaceAudience.Public
34  @InterfaceStability.Stable
35  public class RemoteAdmin {
36  
37    final Client client;
38    final Configuration conf;
39    final String accessToken;
40    final int maxRetries;
41    final long sleepTime;
42  
43    /**
44     * Constructor
45     * @param client
46     * @param conf
47     */
48    public RemoteAdmin(Client client, Configuration conf) {
49      this(client, conf, null);
50    }
51  
52    /**
53     * Constructor
54     * @param client
55     * @param conf
56     * @param accessToken
57     */
58    public RemoteAdmin(Client client, Configuration conf, String accessToken) {
59      this.client = client;
60      this.conf = conf;
61      this.accessToken = accessToken;
62      this.maxRetries = conf.getInt("hbase.rest.client.max.retries", 10);
63      this.sleepTime = conf.getLong("hbase.rest.client.sleep", 1000);
64    }
65  
66    /**
67     * @param tableName name of table to check
68     * @return true if all regions of the table are available
69     * @throws IOException if a remote or network exception occurs
70     */
71    public boolean isTableAvailable(String tableName) throws IOException {
72      return isTableAvailable(Bytes.toBytes(tableName));
73    }
74  
75    /**
76     * @param tableName name of table to check
77     * @return true if all regions of the table are available
78     * @throws IOException if a remote or network exception occurs
79     */
80    public boolean isTableAvailable(byte[] tableName) throws IOException {
81      StringBuilder sb = new StringBuilder();
82      sb.append('/');
83      if (accessToken != null) {
84        sb.append(accessToken);
85        sb.append('/');
86      }
87      sb.append(Bytes.toStringBinary(tableName));
88      sb.append('/');
89      sb.append("exists");
90      int code = 0;
91      for (int i = 0; i < maxRetries; i++) {
92        Response response = client.get(sb.toString());
93        code = response.getCode();
94        switch (code) {
95        case 200:
96          return true;
97        case 404:
98          return false;
99        case 509:
100         try {
101           Thread.sleep(sleepTime);
102         } catch (InterruptedException e) { }
103         break;
104       default:
105         throw new IOException("exists request returned " + code);
106       }
107     }
108     throw new IOException("exists request timed out");
109   }
110 
111   /**
112    * Creates a new table.
113    * @param desc table descriptor for table
114    * @throws IOException if a remote or network exception occurs
115    */
116   public void createTable(HTableDescriptor desc)
117       throws IOException {
118     TableSchemaModel model = new TableSchemaModel(desc);
119     StringBuilder sb = new StringBuilder();
120     sb.append('/');
121     if (accessToken != null) {
122       sb.append(accessToken);
123       sb.append('/');
124     }
125     sb.append(Bytes.toStringBinary(desc.getName()));
126     sb.append('/');
127     sb.append("schema");
128     int code = 0;
129     for (int i = 0; i < maxRetries; i++) {
130       Response response = client.put(sb.toString(), Constants.MIMETYPE_PROTOBUF,
131         model.createProtobufOutput());
132       code = response.getCode();
133       switch (code) {
134       case 201:
135         return;
136       case 509:
137         try {
138           Thread.sleep(sleepTime);
139         } catch (InterruptedException e) { }
140         break;
141       default:
142         throw new IOException("create request returned " + code);
143       }
144     }
145     throw new IOException("create request timed out");
146   }
147 
148   /**
149    * Deletes a table.
150    * @param tableName name of table to delete
151    * @throws IOException if a remote or network exception occurs
152    */
153   public void deleteTable(final String tableName) throws IOException {
154     deleteTable(Bytes.toBytes(tableName));
155   }
156 
157   /**
158    * Deletes a table.
159    * @param tableName name of table to delete
160    * @throws IOException if a remote or network exception occurs
161    */
162   public void deleteTable(final byte [] tableName) throws IOException {
163     StringBuilder sb = new StringBuilder();
164     sb.append('/');
165     if (accessToken != null) {
166       sb.append(accessToken);
167       sb.append('/');
168     }
169     sb.append(Bytes.toStringBinary(tableName));
170     sb.append('/');
171     sb.append("schema");
172     int code = 0;
173     for (int i = 0; i < maxRetries; i++) { 
174       Response response = client.delete(sb.toString());
175       code = response.getCode();
176       switch (code) {
177       case 200:
178         return;
179       case 509:
180         try {
181           Thread.sleep(sleepTime);
182         } catch (InterruptedException e) { }
183         break;
184       default:
185         throw new IOException("delete request returned " + code);
186       }
187     }
188     throw new IOException("delete request timed out");
189   }
190 
191 }