/** * Send a POST request * @param path the path or URI * @param contentType the content MIME type * @param content the content bytes * @return a Response object with response detail * @throws IOException */ public Response post(String path, String contentType, byte[] content) throws IOException { return post(cluster, path, contentType, content); }
/** * Send a DELETE request * @param path the path or URI * @return a Response object with response detail * @throws IOException */ public Response delete(String path) throws IOException { return delete(cluster, path); }
/** * Send a GET request * @param path the path or URI * @return a Response object with response detail * @throws IOException */ public Response get(String path) throws IOException { return get(cluster, path); }
/** * Execute a transaction method. Will call either <tt>executePathOnly</tt> * or <tt>executeURI</tt> depending on whether a path only is supplied in * 'path', or if a complete URI is passed instead, respectively. * @param cluster the cluster definition * @param method the HTTP method * @param headers HTTP header values to send * @param path the properly urlencoded path or URI * @return the HTTP response code * @throws IOException */ public HttpResponse execute(Cluster cluster, HttpUriRequest method, Header[] headers, String path) throws IOException { if (path.startsWith("/")) { return executePathOnly(cluster, method, headers, path); } return executeURI(method, headers, path); }
/** * Send a DELETE request * @param cluster the cluster definition * @param path the path or URI * @return a Response object with response detail * @throws IOException for error */ public Response delete(Cluster cluster, String path) throws IOException { HttpDelete method = new HttpDelete(path); try { HttpResponse resp = execute(cluster, method, null, path); Header[] headers = resp.getAllHeaders(); byte[] content = getResponseBody(resp); return new Response(resp.getStatusLine().getStatusCode(), headers, content); } finally { method.releaseConnection(); } }
@Test public void testMultiCellGetXML() throws IOException, JAXBException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2; StringBuilder path = new StringBuilder(); path.append("/"); path.append(TABLE); path.append("/multiget/?row="); path.append(ROW_1); path.append("&row="); path.append(ROW_2); client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr); client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2), extraHdr); Response response = client.get(path.toString(), Constants.MIMETYPE_XML); assertEquals(200, response.getCode()); assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); client.delete(row_5_url, extraHdr); client.delete(row_6_url, extraHdr); }
@Before public void setup() throws Exception { client = mock(Client.class); Response response = new Response(509); when(client.get(anyString(), anyString())).thenReturn(response); when(client.delete(anyString())).thenReturn(response); when(client.put(anyString(), anyString(), any())).thenReturn(response); when(client.post(anyString(), anyString(), any())).thenReturn(response); Configuration configuration = TEST_UTIL.getConfiguration(); configuration.setInt("hbase.rest.client.max.retries", RETRIES); configuration.setInt("hbase.rest.client.sleep", SLEEP_TIME); remoteAdmin = new RemoteAdmin(client, TEST_UTIL.getConfiguration(), "MyTable"); }
public void run() throws Exception { Cluster cluster = new Cluster(); cluster.add(host, port); Client restClient = new Client(cluster, conf.getBoolean(Constants.REST_SSL_ENABLED, false)); try (RemoteHTable remoteTable = new RemoteHTable(restClient, conf, "example")) { // Write data to the table String rowKey = "row1"; Put p = new Put(rowKey.getBytes()); p.addColumn("family1".getBytes(), "qualifier1".getBytes(), "value1".getBytes()); remoteTable.put(p); // Get the data from the table Get g = new Get(rowKey.getBytes()); Result result = remoteTable.get(g); Preconditions.checkArgument(result != null, Bytes.toString(remoteTable.getTableName()) + " should have a row with key as " + rowKey); System.out.println("row = " + new String(result.getRow())); for (Cell cell : result.rawCells()) { System.out.print("family = " + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t"); System.out.print("qualifier = " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t"); System.out.print("value = " + Bytes.toString(CellUtil.cloneValue(cell)) + "\t"); System.out.println("timestamp = " + Long.toString(cell.getTimestamp())); } } }
@Test public void testTableDoesNotExist() throws IOException, JAXBException { ScannerModel model = new ScannerModel(); StringWriter writer = new StringWriter(); marshaller.marshal(model, writer); byte[] body = Bytes.toBytes(writer.toString()); Response response = client.put("/" + NONEXISTENT_TABLE + "/scanner", Constants.MIMETYPE_XML, body); String scannerURI = response.getLocation(); assertNotNull(scannerURI); response = client.get(scannerURI, Constants.MIMETYPE_XML); assertEquals(404, response.getCode()); }
/** * Send a PUT request * @param path the path or URI * @param contentType the content MIME type * @param content the content bytes * @return a Response object with response detail * @throws IOException */ public Response put(String path, String contentType, byte[] content) throws IOException { return put(cluster, path, contentType, content); }
@Test public void testNoSuchCF() throws IOException, JAXBException { final String goodPath = "/" + TABLE + "/" + ROW_1 + "/" + CFA+":"; final String badPath = "/" + TABLE + "/" + ROW_1 + "/" + "BAD"; Response response = client.post(goodPath, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1)); assertEquals(200, response.getCode()); assertEquals(200, client.get(goodPath, Constants.MIMETYPE_BINARY).getCode()); assertEquals(404, client.get(badPath, Constants.MIMETYPE_BINARY).getCode()); assertEquals(200, client.get(goodPath, Constants.MIMETYPE_BINARY).getCode()); }
@Override public void close() throws IOException { client.shutdown(); }
/** * Send a GET request * @param c the cluster definition * @param path the path or URI * @param headers the HTTP headers to include in the request * @return a Response object with response detail * @throws IOException */ public Response get(Cluster c, String path, Header[] headers) throws IOException { if (httpGet != null) { httpGet.releaseConnection(); } httpGet = new HttpGet(path); HttpResponse resp = execute(c, httpGet, headers, path); return new Response(resp.getStatusLine().getStatusCode(), resp.getAllHeaders(), resp, resp.getEntity() == null ? null : resp.getEntity().getContent()); }
/** * Send a HEAD request * @param path the path or URI * @return a Response object with response detail * @throws IOException */ public Response head(String path) throws IOException { return head(cluster, path, null); }
method = post; return executeURI(method, headers, uri.toString()); } catch (IOException e) { lastException = e;
@Test public void testMultiCellGetJSON() throws IOException, JAXBException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; String row_6_url = "/" + TABLE + "/" + ROW_2 + "/" + COLUMN_2; StringBuilder path = new StringBuilder(); path.append("/"); path.append(TABLE); path.append("/multiget/?row="); path.append(ROW_1); path.append("&row="); path.append(ROW_2); if (csrfEnabled) { Response response = client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1)); assertEquals(400, response.getCode()); } client.post(row_5_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_1), extraHdr); client.post(row_6_url, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_2), extraHdr); Response response = client.get(path.toString(), Constants.MIMETYPE_JSON); assertEquals(200, response.getCode()); assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type")); client.delete(row_5_url, extraHdr); client.delete(row_6_url, extraHdr); }
@Before public void setup() throws Exception { client = mock(Client.class); Response response = new Response(509); when(client.get(anyString(), anyString())).thenReturn(response); when(client.delete(anyString())).thenReturn(response); when(client.put(anyString(), anyString(), any())).thenReturn( response); when(client.post(anyString(), anyString(), any())).thenReturn( response); Configuration configuration = TEST_UTIL.getConfiguration(); configuration.setInt("hbase.rest.client.max.retries", RETRIES); configuration.setInt("hbase.rest.client.sleep", SLEEP_TIME); remoteTable = new RemoteHTable(client, TEST_UTIL.getConfiguration(), "MyTable"); }
private void runNIsMoreThanOne(final Class<? extends Test> cmd) throws IOException, InterruptedException, ClassNotFoundException { RemoteAdmin remoteAdmin = new RemoteAdmin(new Client(cluster), getConf()); checkTable(remoteAdmin); if (nomapred) { doMultipleClients(cmd); } else { doMapReduce(cmd); } }
@Test public void testSingleCellGetJSON() throws IOException, JAXBException { final String path = "/" + TABLE + "/" + ROW_4 + "/" + COLUMN_1; Response response = client.put(path, Constants.MIMETYPE_BINARY, Bytes.toBytes(VALUE_4)); assertEquals(200, response.getCode()); Thread.yield(); response = client.get(path, Constants.MIMETYPE_JSON); assertEquals(200, response.getCode()); assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type")); response = deleteRow(TABLE, ROW_4); assertEquals(200, response.getCode()); }
/** * Send a PUT request * @param path the path or URI * @param headers the HTTP headers to include, <tt>Content-Type</tt> must be * supplied * @param content the content bytes * @return a Response object with response detail * @throws IOException */ public Response put(String path, Header[] headers, byte[] content) throws IOException { return put(cluster, path, headers, content); }