/** * 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); }
/** * Send a PUT request * @param path the path or URI * @param contentType the content MIME type * @param content the content bytes * @param extraHdr extra Header to send * @return a Response object with response detail * @throws IOException */ public Response put(String path, String contentType, byte[] content, Header extraHdr) throws IOException { return put(cluster, path, contentType, content, extraHdr); }
/** * 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); }
/** * Send a PUT request * @param cluster the cluster definition * @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 for error */ public Response put(Cluster cluster, String path, String contentType, byte[] content) throws IOException { Header[] headers = new Header[1]; headers[0] = new BasicHeader("Content-Type", contentType); return put(cluster, path, headers, content); }
@Test public void testSingleRowPut() throws Exception { testTimedOutCall(new CallExecutor() { @Override public void run() throws Exception { remoteTable.put(new Put(Bytes.toBytes("Row"))); } }); verify(client, times(RETRIES)).put(anyString(), anyString(), any()); }
@Test public void testMultiRowPut() throws Exception { testTimedOutCall(new CallExecutor() { @Override public void run() throws Exception { Put[] puts = { new Put(Bytes.toBytes("Row1")), new Put(Bytes.toBytes("Row2")) }; remoteTable.put(Arrays.asList(puts)); } }); verify(client, times(RETRIES)).put(anyString(), anyString(), any()); }
@Test @SuppressWarnings("deprecation") public void testFailingCreateTable() throws Exception { testTimedOutCall(new CallExecutor() { @Override public void run() throws Exception { remoteAdmin.createTable(new HTableDescriptor(TableName.valueOf("TestTable"))); } }); verify(client, times(RETRIES)).put(anyString(), anyString(), any()); }
@Test public void testCheckAndPut() throws Exception { testTimedOutCall(new CallExecutor() { @Override public void run() throws Exception { Put put = new Put(ROW_1); put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1) .ifEquals(VALUE_1).thenPut(put); } }); verify(client, times(RETRIES)).put(anyString(), anyString(), any()); }
@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"); }
@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"); }
protected static Response putValueXML(String url, String table, String row, String column, String value) throws IOException, JAXBException { RowModel rowModel = new RowModel(row); rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(value))); CellSetModel cellSetModel = new CellSetModel(); cellSetModel.addRow(rowModel); StringWriter writer = new StringWriter(); xmlMarshaller.marshal(cellSetModel, writer); Response response = client.put(url, Constants.MIMETYPE_XML, Bytes.toBytes(writer.toString())); Thread.yield(); return response; }
protected static Response putValueJson(String url, String table, String row, String column, String value) throws IOException, JAXBException { RowModel rowModel = new RowModel(row); rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(value))); CellSetModel cellSetModel = new CellSetModel(); cellSetModel.addRow(rowModel); String jsonString = jsonMapper.writeValueAsString(cellSetModel); Response response = client.put(url, Constants.MIMETYPE_JSON, Bytes.toBytes(jsonString)); Thread.yield(); return response; }
protected static Response checkAndDeleteJson(String url, String table, String row, String column, String valueToCheck, HashMap<String,String> cellsToDelete) throws IOException, JAXBException { RowModel rowModel = new RowModel(row); if(cellsToDelete != null) { for (Map.Entry<String,String> entry :cellsToDelete.entrySet()) { rowModel.addCell(new CellModel(Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue()))); } } // Add this at the end rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(valueToCheck))); CellSetModel cellSetModel = new CellSetModel(); cellSetModel.addRow(rowModel); String jsonString = jsonMapper.writeValueAsString(cellSetModel); Response response = client.put(url, Constants.MIMETYPE_JSON, Bytes.toBytes(jsonString)); Thread.yield(); return response; }
static Response putValuePB(String url, String table, String row, String column, String value) throws IOException { RowModel rowModel = new RowModel(row); rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(value))); CellSetModel cellSetModel = new CellSetModel(); cellSetModel.addRow(rowModel); Response response = client.put(url, Constants.MIMETYPE_PROTOBUF, cellSetModel.createProtobufOutput()); Thread.yield(); return response; }
@Test public void testInvalidCheckParam() throws IOException, JAXBException { CellSetModel cellSetModel = new CellSetModel(); RowModel rowModel = new RowModel(ROW_1); rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_1), Bytes.toBytes(VALUE_1))); cellSetModel.addRow(rowModel); StringWriter writer = new StringWriter(); xmlMarshaller.marshal(cellSetModel, writer); final String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1 + "?check=blah"; Response response = client.put(path, Constants.MIMETYPE_XML, Bytes.toBytes(writer.toString())); assertEquals(400, response.getCode()); }
@Test public void testInvalidColumnPut() throws IOException, JAXBException { String dummyColumn = "doesnot:exist"; CellSetModel cellSetModel = new CellSetModel(); RowModel rowModel = new RowModel(ROW_1); rowModel.addCell(new CellModel(Bytes.toBytes(dummyColumn), Bytes.toBytes(VALUE_1))); cellSetModel.addRow(rowModel); StringWriter writer = new StringWriter(); xmlMarshaller.marshal(cellSetModel, writer); final String path = "/" + TABLE + "/" + ROW_1 + "/" + dummyColumn; Response response = client.put(path, Constants.MIMETYPE_XML, Bytes.toBytes(writer.toString())); assertEquals(404, response.getCode()); }
@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()); }
protected static Response checkAndDeleteValuePB(String url, String table, String row, String column, String valueToCheck, HashMap<String,String> cellsToDelete) throws IOException { RowModel rowModel = new RowModel(row); if(cellsToDelete != null) { for (Map.Entry<String,String> entry :cellsToDelete.entrySet()) { rowModel.addCell(new CellModel(Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue()))); } } // Add this at the end rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes .toBytes(valueToCheck))); CellSetModel cellSetModel = new CellSetModel(); cellSetModel.addRow(rowModel); Response response = client.put(url, Constants.MIMETYPE_PROTOBUF, cellSetModel.createProtobufOutput()); Thread.yield(); return response; }
@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()); }
@Test public void testTableScanWithTableDisable() throws IOException { ScannerModel model = new ScannerModel(); model.addColumn(Bytes.toBytes(COLUMN_1)); model.setCaching(1); Response response = client.put("/" + TABLE_TO_BE_DISABLED + "/scanner", Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput()); assertEquals(201, response.getCode()); String scannerURI = response.getLocation(); assertNotNull(scannerURI); TEST_UTIL.getAdmin().disableTable(TABLE_TO_BE_DISABLED); response = client.get(scannerURI, Constants.MIMETYPE_PROTOBUF); assertTrue("got " + response.getCode(), response.getCode() == 410); }