Search in sources :

Example 51 with CellSetModel

use of org.apache.hadoop.hbase.rest.model.CellSetModel in project hbase by apache.

the class TestTableScan method readProtobufStream.

/**
 * Read protobuf stream.
 * @param inputStream the input stream
 * @return The number of rows in the cell set model.
 * @throws IOException Signals that an I/O exception has occurred.
 */
public int readProtobufStream(InputStream inputStream) throws IOException {
    DataInputStream stream = new DataInputStream(inputStream);
    CellSetModel model = null;
    int rowCount = 0;
    try {
        while (true) {
            byte[] lengthBytes = new byte[2];
            int readBytes = stream.read(lengthBytes);
            if (readBytes == -1) {
                break;
            }
            assertEquals(2, readBytes);
            int length = Bytes.toShort(lengthBytes);
            byte[] cellset = new byte[length];
            stream.read(cellset);
            model = new CellSetModel();
            model.getObjectFromMessage(cellset);
            checkRowsNotNull(model);
            rowCount = rowCount + TestScannerResource.countCellSet(model);
        }
    } catch (EOFException exp) {
        exp.printStackTrace();
    } finally {
        stream.close();
    }
    return rowCount;
}
Also used : CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) EOFException(java.io.EOFException) DataInputStream(java.io.DataInputStream)

Example 52 with CellSetModel

use of org.apache.hadoop.hbase.rest.model.CellSetModel in project hbase by apache.

the class TestTableScan method testQualifierAndPrefixFilters.

@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));
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 53 with CellSetModel

use of org.apache.hadoop.hbase.rest.model.CellSetModel in project hbase by apache.

the class TestTableScan method testSimpleScannerJson.

@Test
public void testSimpleScannerJson() throws IOException {
    // Test scanning particular columns with limit.
    StringBuilder builder = new StringBuilder();
    builder.append("/*");
    builder.append("?");
    builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_1);
    builder.append("&");
    builder.append(Constants.SCAN_LIMIT + "=2");
    Response response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON);
    assertEquals(200, response.getCode());
    assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
    ObjectMapper mapper = new JacksonJaxbJsonProvider().locateMapper(CellSetModel.class, MediaType.APPLICATION_JSON_TYPE);
    CellSetModel model = mapper.readValue(response.getStream(), CellSetModel.class);
    int count = TestScannerResource.countCellSet(model);
    assertEquals(2, count);
    checkRowsNotNull(model);
    // Test scanning with no limit.
    builder = new StringBuilder();
    builder.append("/*");
    builder.append("?");
    builder.append(Constants.SCAN_COLUMN + "=" + COLUMN_2);
    response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON);
    assertEquals(200, response.getCode());
    assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
    model = mapper.readValue(response.getStream(), CellSetModel.class);
    count = TestScannerResource.countCellSet(model);
    assertEquals(expectedRows2, count);
    checkRowsNotNull(model);
    // Test with start row and end row.
    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");
    response = client.get("/" + TABLE + builder.toString(), Constants.MIMETYPE_JSON);
    assertEquals(200, response.getCode());
    model = mapper.readValue(response.getStream(), CellSetModel.class);
    RowModel startRow = model.getRows().get(0);
    assertEquals("aaa", Bytes.toString(startRow.getKey()));
    RowModel endRow = model.getRows().get(model.getRows().size() - 1);
    assertEquals("aax", Bytes.toString(endRow.getKey()));
    count = TestScannerResource.countCellSet(model);
    assertEquals(24, count);
    checkRowsNotNull(model);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) JacksonJaxbJsonProvider(org.apache.hbase.thirdparty.com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 54 with CellSetModel

use of org.apache.hadoop.hbase.rest.model.CellSetModel in project hbase by apache.

the class RemoteHTable method buildModelFromPut.

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;
}
Also used : CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) Cell(org.apache.hadoop.hbase.Cell)

Example 55 with CellSetModel

use of org.apache.hadoop.hbase.rest.model.CellSetModel in project hbase by apache.

the class RemoteHTable method put.

@Override
public void put(List<Put> puts) throws IOException {
    // this is a trick: The gateway accepts multiple rows in a cell set and
    // ignores the row specification in the URI
    // separate puts by row
    TreeMap<byte[], List<Cell>> map = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    for (Put put : puts) {
        byte[] row = put.getRow();
        List<Cell> cells = map.get(row);
        if (cells == null) {
            cells = new ArrayList<>();
            map.put(row, cells);
        }
        for (List<Cell> l : put.getFamilyCellMap().values()) {
            cells.addAll(l);
        }
    }
    // build the cell set
    CellSetModel model = new CellSetModel();
    for (Map.Entry<byte[], List<Cell>> e : map.entrySet()) {
        RowModel row = new RowModel(e.getKey());
        for (Cell cell : e.getValue()) {
            row.addCell(new CellModel(cell));
        }
        model.addRow(row);
    }
    // build path for multiput
    StringBuilder sb = new StringBuilder();
    sb.append('/');
    sb.append(Bytes.toString(name));
    // can be any nonexistent row
    sb.append("/$multiput");
    for (int i = 0; i < maxRetries; i++) {
        Response response = client.put(sb.toString(), Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
        int code = response.getCode();
        switch(code) {
            case 200:
                return;
            case 509:
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    throw (InterruptedIOException) new InterruptedIOException().initCause(e);
                }
                break;
            default:
                throw new IOException("multiput request failed with " + code);
        }
    }
    throw new IOException("multiput request timed out");
}
Also used : InterruptedIOException(java.io.InterruptedIOException) CellSetModel(org.apache.hadoop.hbase.rest.model.CellSetModel) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Put(org.apache.hadoop.hbase.client.Put) List(java.util.List) ArrayList(java.util.ArrayList) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) Cell(org.apache.hadoop.hbase.Cell) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

CellSetModel (org.apache.hadoop.hbase.rest.model.CellSetModel)61 Response (org.apache.hadoop.hbase.rest.client.Response)44 RowModel (org.apache.hadoop.hbase.rest.model.RowModel)42 CellModel (org.apache.hadoop.hbase.rest.model.CellModel)37 Test (org.junit.Test)26 StringWriter (java.io.StringWriter)14 IOException (java.io.IOException)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 Cell (org.apache.hadoop.hbase.Cell)9 JacksonJaxbJsonProvider (org.apache.hbase.thirdparty.com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider)9 InterruptedIOException (java.io.InterruptedIOException)7 JAXBContext (javax.xml.bind.JAXBContext)7 Unmarshaller (javax.xml.bind.Unmarshaller)7 Map (java.util.Map)6 ScannerModel (org.apache.hadoop.hbase.rest.model.ScannerModel)6 HashMap (java.util.HashMap)5 Result (org.apache.hadoop.hbase.client.Result)5 KeyValue (org.apache.hadoop.hbase.KeyValue)4 Put (org.apache.hadoop.hbase.client.Put)3