private boolean containsCellModel(List<CellModel> cells, String column, String value) { boolean contains = false; for (CellModel cell : cells) { if (Bytes.toString(cell.getColumn()).equals(column) && Bytes.toString(cell.getValue()).equals(value)) { contains = true; return contains; } } return contains; }
protected Result[] buildResultFromModel(final CellSetModel model) { List<Result> results = new ArrayList<>(); for (RowModel row: model.getRows()) { List<Cell> kvs = new ArrayList<>(row.getCells().size()); for (CellModel cell: row.getCells()) { byte[][] split = CellUtil.parseColumn(cell.getColumn()); byte[] column = split[0]; byte[] qualifier = null; if (split.length == 1) { qualifier = HConstants.EMPTY_BYTE_ARRAY; } else if (split.length == 2) { qualifier = split[1]; } else { throw new IllegalArgumentException("Invalid familyAndQualifier provided."); } kvs.add(new KeyValue(row.getKey(), column, qualifier, cell.getTimestamp(), cell.getValue())); } results.add(Result.create(kvs)); } return results.toArray(new Result[results.size()]); }
@Test public void testSimpleFilter() throws IOException, JAXBException { StringBuilder builder = new StringBuilder(); builder.append("/*"); builder.append("?"); builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1); builder.append("&"); builder.append(Constants.SCAN_START_ROW + "=aaa"); builder.append("&"); builder.append(Constants.SCAN_END_ROW + "=aay"); builder.append("&"); builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("PrefixFilter('aab')", "UTF-8")); Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML); assertEquals(200, response.getCode()); JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class); Unmarshaller ush = ctx.createUnmarshaller(); CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream()); int count = TestScannerResource.countCellSet(model); assertEquals(1, count); assertEquals("aab", new String(model.getRows().get(0).getCells().get(0).getValue(), StandardCharsets.UTF_8)); }
@Test public void testCustomFilter() throws IOException, JAXBException { StringBuilder builder = new StringBuilder(); builder.append("/a*"); builder.append("?"); builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1); builder.append("&"); builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("CustomFilter('abc')", "UTF-8")); Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML); assertEquals(200, response.getCode()); JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class); Unmarshaller ush = ctx.createUnmarshaller(); CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream()); int count = TestScannerResource.countCellSet(model); assertEquals(1, count); assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(), StandardCharsets.UTF_8)); }
@Test public void testQualifierAndPrefixFilters() throws IOException, JAXBException { StringBuilder builder = new StringBuilder(); builder.append("/abc*"); builder.append("?"); builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("QualifierFilter(=,'binary:1')", "UTF-8")); Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML); assertEquals(200, response.getCode()); JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class); Unmarshaller ush = ctx.createUnmarshaller(); CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream()); int count = TestScannerResource.countCellSet(model); assertEquals(1, count); assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(), StandardCharsets.UTF_8)); }
@Test public void testCompoundFilter() throws IOException, JAXBException { StringBuilder builder = new StringBuilder(); builder.append("/*"); builder.append("?"); builder.append(Constants.SCAN_FILTER + "=" + URLEncoder.encode("PrefixFilter('abc') AND QualifierFilter(=,'binary:1')", "UTF-8")); Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_XML); assertEquals(200, response.getCode()); JAXBContext ctx = JAXBContext.newInstance(CellSetModel.class); Unmarshaller ush = ctx.createUnmarshaller(); CellSetModel model = (CellSetModel) ush.unmarshal(response.getStream()); int count = TestScannerResource.countCellSet(model); assertEquals(1, count); assertEquals("abc", new String(model.getRows().get(0).getCells().get(0).getValue(), StandardCharsets.UTF_8)); }
protected static void checkValueXML(String url, String table, String row, String column, String value) throws IOException, JAXBException { Response response = getValueXML(url); assertEquals(200, response.getCode()); assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); CellSetModel cellSet = (CellSetModel) xmlUnmarshaller.unmarshal(new ByteArrayInputStream(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 checkValueXML(String table, String row, String column, String value) throws IOException, JAXBException { Response response = getValueXML(table, row, column); assertEquals(200, response.getCode()); assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); CellSetModel cellSet = (CellSetModel) xmlUnmarshaller.unmarshal(new ByteArrayInputStream(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); }
@Test public void testMultiCellGetJSONNotFound() throws IOException, JAXBException { String row_5_url = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1; 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); Response response = client.get(path.toString(), Constants.MIMETYPE_JSON); assertEquals(200, response.getCode()); ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE); CellSetModel cellSet = (CellSetModel) mapper.readValue(response.getBody(), CellSetModel.class); assertEquals(1, cellSet.getRows().size()); assertEquals(ROW_1, Bytes.toString(cellSet.getRows().get(0).getKey())); assertEquals(VALUE_1, Bytes.toString(cellSet.getRows().get(0).getCells().get(0).getValue())); client.delete(row_5_url, extraHdr); }
protected static void checkIncrementValueXML(String table, String row, String column, long value) throws IOException, JAXBException { Response response1 = getValueXML(table, row, column); assertEquals(200, response1.getCode()); assertEquals(Constants.MIMETYPE_XML, response1.getHeader("content-type")); CellSetModel cellSet = (CellSetModel) xmlUnmarshaller.unmarshal(new ByteArrayInputStream(response1.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 testMultiCellGetWithColsJSON() 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"); path.append("/" + COLUMN_1 + "," + CFB); path.append("?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_JSON); assertEquals(200, response.getCode()); ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE); CellSetModel cellSet = mapper.readValue(response.getBody(), CellSetModel.class); assertEquals(2, cellSet.getRows().size()); assertEquals(ROW_1, Bytes.toString(cellSet.getRows().get(0).getKey())); assertEquals(VALUE_1, Bytes.toString(cellSet.getRows().get(0).getCells().get(0).getValue())); assertEquals(ROW_2, Bytes.toString(cellSet.getRows().get(1).getKey())); assertEquals(VALUE_2, Bytes.toString(cellSet.getRows().get(1).getCells().get(0).getValue())); client.delete(row_5_url, extraHdr); client.delete(row_6_url, extraHdr); }
protected static void checkValueJSON(String table, String row, String column, String value) throws IOException, JAXBException { Response response = getValueJson(table, row, column); assertEquals(200, response.getCode()); assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type")); ObjectMapper mapper = new JacksonJaxbJsonProvider() .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE); CellSetModel cellSet = mapper.readValue(response.getBody(), CellSetModel.class); 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 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); }
@Override public byte[] createProtobufOutput() { Cell.Builder builder = Cell.newBuilder(); builder.setColumn(ByteStringer.wrap(getColumn())); builder.setData(ByteStringer.wrap(getValue())); if (hasUserTimestamp()) { builder.setTimestamp(getTimestamp()); } return builder.build().toByteArray(); }
protected static void checkIncrementValueJSON(String table, String row, String column, long value) throws IOException, JAXBException { Response response = getValueJson(table, row, column); assertEquals(200, response.getCode()); assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type")); ObjectMapper mapper = new JacksonJaxbJsonProvider() .locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE); CellSetModel cellSet = mapper.readValue(response.getBody(), CellSetModel.class); RowModel rowModel = cellSet.getRows().get(0); CellModel cell = rowModel.getCells().get(0); assertEquals(Bytes.toString(cell.getColumn()), column); assertEquals(Bytes.toLong(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); }
@Override protected void checkModel(RowModel model) { assertTrue(Bytes.equals(ROW1, model.getKey())); Iterator<CellModel> cells = model.getCells().iterator(); CellModel cell = cells.next(); assertTrue(Bytes.equals(COLUMN1, cell.getColumn())); assertTrue(Bytes.equals(VALUE1, cell.getValue())); assertTrue(cell.hasUserTimestamp()); assertEquals(TIMESTAMP1, cell.getTimestamp()); assertFalse(cells.hasNext()); }
@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()); }
@Override protected void checkModel(CellModel model) { assertTrue(Bytes.equals(model.getColumn(), COLUMN)); assertTrue(Bytes.equals(model.getValue(), VALUE)); assertTrue(model.hasUserTimestamp()); assertEquals(TIMESTAMP, model.getTimestamp()); }
@Override public byte[] createProtobufOutput() { CellSet.Builder builder = CellSet.newBuilder(); for (RowModel row: getRows()) { CellSet.Row.Builder rowBuilder = CellSet.Row.newBuilder(); rowBuilder.setKey(ByteStringer.wrap(row.getKey())); for (CellModel cell: row.getCells()) { Cell.Builder cellBuilder = Cell.newBuilder(); cellBuilder.setColumn(ByteStringer.wrap(cell.getColumn())); cellBuilder.setData(ByteStringer.wrap(cell.getValue())); if (cell.hasUserTimestamp()) { cellBuilder.setTimestamp(cell.getTimestamp()); } rowBuilder.addValues(cellBuilder); } builder.addRows(rowBuilder); } return builder.build().toByteArray(); }