View Javadoc

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