@Override public Result[] next(int nbRows) throws IOException { StringBuilder sb = new StringBuilder(uri); sb.append("?n="); sb.append(nbRows); for (int i = 0; i < maxRetries; i++) { Response response = client.get(sb.toString(), Constants.MIMETYPE_PROTOBUF); int code = response.getCode(); switch (code) { case 200: CellSetModel model = new CellSetModel(); model.getObjectFromMessage(response.getBody()); return buildResultFromModel(model); case 204: case 206: return null; case 509: try { Thread.sleep(sleepTime); } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } break; default: throw new IOException("scanner.next request failed with " + code); } } throw new IOException("scanner.next request timed out"); }
private Result[] getResults(String spec) throws IOException { for (int i = 0; i < maxRetries; i++) { Response response = client.get(spec, Constants.MIMETYPE_PROTOBUF); int code = response.getCode(); switch (code) { case 200: CellSetModel model = new CellSetModel(); model.getObjectFromMessage(response.getBody()); Result[] results = buildResultFromModel(model); if ( results.length > 0) { return results; } // fall through case 404: return new Result[0]; case 509: try { Thread.sleep(sleepTime); } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } break; default: throw new IOException("get request returned " + code); } } throw new IOException("get request timed out"); }
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 checkAndDeleteXML(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); 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 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; }
protected static Response checkAndPutValueXML(String url, String table, String row, String column, String valueToCheck, String valueToPut, HashMap<String,String> otherCells) throws IOException, JAXBException { RowModel rowModel = new RowModel(row); rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(valueToPut))); if(otherCells != null) { for (Map.Entry<String,String> entry :otherCells.entrySet()) { rowModel.addCell(new CellModel(Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue()))); } } // This Cell need to be added as last cell. rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(valueToCheck))); 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; }
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()); }
private CellSetModel createModelFromResults(Result[] results) { CellSetModel cellSetModel = new CellSetModel(); for (Result rs : results) { byte[] rowKey = rs.getRow(); RowModel rModel = new RowModel(rowKey); List<Cell> kvs = rs.listCells(); for (Cell kv : kvs) { rModel.addCell(new CellModel(CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv), kv .getTimestamp(), CellUtil.cloneValue(kv))); } cellSetModel.addRow(rModel); } return cellSetModel; } }
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; }
protected static Response checkAndPutValuePB(String url, String table, String row, String column, String valueToCheck, String valueToPut, HashMap<String,String> otherCells) throws IOException { RowModel rowModel = new RowModel(row); rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(valueToPut))); if(otherCells != null) { for (Map.Entry<String,String> entry :otherCells.entrySet()) { rowModel.addCell(new CellModel(Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue()))); } } // This Cell need to be added as last cell. 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; }
protected CellSetModel buildModelFromPut(Put put) { RowModel row = new RowModel(put.getRow()); long ts = put.getTimestamp(); for (List<Cell> cells: put.getFamilyCellMap().values()) { for (Cell cell: cells) { row.addCell(new CellModel(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell), ts != HConstants.LATEST_TIMESTAMP ? ts : cell.getTimestamp(), CellUtil.cloneValue(cell))); } } CellSetModel model = new CellSetModel(); model.addRow(row); return model; }
protected static void checkValuePB(String table, String row, String column, String value) throws IOException { Response response = getValuePB(table, row, column); assertEquals(200, response.getCode()); assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type")); CellSetModel cellSet = new CellSetModel(); cellSet.getObjectFromMessage(response.getBody()); RowModel rowModel = cellSet.getRows().get(0); CellModel cell = rowModel.getCells().get(0); assertEquals(Bytes.toString(cell.getColumn()), column); assertEquals(Bytes.toString(cell.getValue()), value); }
protected static void checkIncrementValuePB(String table, String row, String column, long value) throws IOException { Response response = getValuePB(table, row, column); assertEquals(200, response.getCode()); assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type")); CellSetModel cellSet = new CellSetModel(); cellSet.getObjectFromMessage(response.getBody()); RowModel rowModel = cellSet.getRows().get(0); CellModel cell = rowModel.getCells().get(0); assertEquals(Bytes.toString(cell.getColumn()), column); assertEquals(Bytes.toLong(cell.getValue()), value); }
@Test public void testLatestCellGetJSON() throws IOException, JAXBException { final String path = "/" + TABLE + "/" + ROW_4 + "/" + COLUMN_1; CellSetModel cellSetModel = new CellSetModel(); RowModel rowModel = new RowModel(ROW_4); CellModel cellOne = new CellModel(Bytes.toBytes(COLUMN_1), 1L, Bytes.toBytes(VALUE_1)); CellModel cellTwo = new CellModel(Bytes.toBytes(COLUMN_1), 2L, Bytes.toBytes(VALUE_2)); rowModel.addCell(cellOne); rowModel.addCell(cellTwo); cellSetModel.addRow(rowModel); String jsonString = jsonMapper.writeValueAsString(cellSetModel); Response response = client.put(path, Constants.MIMETYPE_JSON, Bytes.toBytes(jsonString)); 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")); CellSetModel cellSet = jsonMapper.readValue(response.getBody(), CellSetModel.class); assertTrue(cellSet.getRows().size() == 1); assertTrue(cellSet.getRows().get(0).getCells().size() == 1); CellModel cell = cellSet.getRows().get(0).getCells().get(0); assertEquals(VALUE_2 , Bytes.toString(cell.getValue())); assertEquals(2L , cell.getTimestamp()); response = deleteRow(TABLE, ROW_4); assertEquals(200, response.getCode()); }
String path = "/" + TABLE + "/fakerow"; // deliberate nonexistent row CellSetModel cellSetModel = new CellSetModel(); RowModel rowModel = new RowModel(ROW_1); rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_1),
String path = "/" + TABLE + "/fakerow"; // deliberate nonexistent row CellSetModel cellSetModel = new CellSetModel(); RowModel rowModel = new RowModel(ROW_1); rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_1),
@Override protected CellSetModel buildTestModel() { CellSetModel model = new CellSetModel(); RowModel row; row = new RowModel(); row.setKey(ROW1); row.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1)); model.addRow(row); row = new RowModel(); row.setKey(ROW2); row.addCell(new CellModel(COLUMN2, TIMESTAMP2, VALUE2)); row.addCell(new CellModel(COLUMN3, TIMESTAMP3, VALUE3)); model.addRow(row); return model; }