HBaseDataStore implements DataStore { @Override public void save(final Table table, Document document) throws DataStoreException { if (document == null || document.getData() == null || document.getId() == null) { throw new DataStoreException(DataStoreException.ErrorCode.STORE_INVALID_REQUEST, "Invalid Document"); } HTableInterface hTable = null; try { hTable = tableWrapper.getTable(table); Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); hTable.put(getPutForDocument(table, document)); logger.error(String.format("HBASE put took : %d table : %s", stopwatch.elapsedMillis(), table)); } catch (JsonProcessingException e) { throw new DataStoreException(DataStoreException.ErrorCode.STORE_INVALID_REQUEST, e.getMessage(), e); } catch (IOException e) { throw new DataStoreException(DataStoreException.ErrorCode.STORE_SINGLE_SAVE, e.getMessage(), e); } catch (Exception e) { throw new DataStoreException(DataStoreException.ErrorCode.STORE_SINGLE_SAVE, e.getMessage(), e); } finally { if (null != hTable) { try { hTable.close(); } catch (IOException e) { logger.error("Error closing table: ", e); } } } } HBaseDataStore(HbaseTableConnection tableWrapper, ObjectMapper mapper); @Override void save(final Table table, Document document); @Override void save(final Table table, List<Document> documents); @Override Document get(final Table table, String id); @Override List<Document> get(final Table table, List<String> ids); Put getPutForDocument(final Table table, Document document);  }
@Test public void testSaveSingle() throws Exception { Document expectedDocument = new Document(); expectedDocument.setId(UUID.randomUUID().toString()); expectedDocument.setTimestamp(System.currentTimeMillis()); JsonNode data = mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")); expectedDocument.setData(data); HBaseDataStore.save(TEST_APP, expectedDocument); validateSave(expectedDocument); }
@Test public void testSaveSingleNullDocument() throws Exception { Document document = null; try { HBaseDataStore.save(TEST_APP, document); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testSaveSingleNullId() throws Exception { Document document = new Document(null, System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST"))); try { HBaseDataStore.save(TEST_APP, document); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testSaveSingleNullData() throws Exception { Document document = new Document(UUID.randomUUID().toString(), System.currentTimeMillis(), null); try { HBaseDataStore.save(TEST_APP, document); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testSaveSingleHBaseWriteException() throws Exception { Document document = new Document(UUID.randomUUID().toString(), System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST"))); doThrow(new IOException()) .when(tableInterface) .put(Matchers.<Put>any()); try { HBaseDataStore.save(TEST_APP, document); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_SINGLE_SAVE, ex.getErrorCode()); } }
@Test public void testSaveSingleHBaseCloseException() throws Exception { Document document = new Document(UUID.randomUUID().toString(), System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST"))); doThrow(new IOException()) .when(tableInterface) .close(); HBaseDataStore.save(TEST_APP, document); verify(tableInterface, times(1)).close(); }
@Test public void testSaveSingleNullHBaseTableConnection() throws Exception { Document expectedDocument = new Document(); expectedDocument.setId(UUID.randomUUID().toString()); expectedDocument.setTimestamp(System.currentTimeMillis()); JsonNode data = mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")); expectedDocument.setData(data); when(hBaseTableConnection.getTable(Matchers.<Table>any())).thenReturn(null); try { HBaseDataStore.save(TEST_APP, expectedDocument); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_SINGLE_SAVE, ex.getErrorCode()); } }
@Test public void testSaveBulk() throws Exception { List<Document> documents = new Vector<Document>(); for (int i = 0; i < 10; i++) { documents.add(new Document(UUID.randomUUID().toString(), System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")))); } HBaseDataStore.save(TEST_APP, documents); for (Document document : documents) { validateSave(document); } }
@Test public void testSaveBulkNullDocuments() throws Exception { List<Document> documents = new Vector<Document>(); for (int i = 0; i < 10; i++) { documents.add(null); } try { HBaseDataStore.save(TEST_APP, documents); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testSaveBulkNullIdList() throws Exception { List<Document> documents = null; try { HBaseDataStore.save(TEST_APP, documents); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testSaveBulkNullId() throws Exception { List<Document> documents = new Vector<Document>(); for (int i = 0; i < 10; i++) { documents.add(new Document(null, System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST_NAME", "SINGLE_SAVE_TEST")))); } try { HBaseDataStore.save(TEST_APP, documents); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testSaveBulkNullData() throws Exception { List<Document> documents = new Vector<Document>(); for (int i = 0; i < 10; i++) { documents.add(new Document(UUID.randomUUID().toString(), System.currentTimeMillis(), null)); } try { HBaseDataStore.save(TEST_APP, documents); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testSaveBulkHBaseWriteException() throws Exception { List<Document> documents = new Vector<Document>(); documents.add(new Document(UUID.randomUUID().toString(), System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST", "TEST")))); doThrow(new IOException()) .when(tableInterface) .put(Matchers.anyListOf(Put.class)); try { HBaseDataStore.save(TEST_APP, documents); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_MULTI_SAVE, ex.getErrorCode()); } }
@Test public void testSaveBulkHBaseCloseException() throws Exception { List<Document> documents = new Vector<Document>(); documents.add(new Document(UUID.randomUUID().toString(), System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST_NAME", "BULK_SAVE_TEST")))); doThrow(new IOException()) .when(tableInterface) .close(); HBaseDataStore.save(TEST_APP, documents); verify(tableInterface, times(1)).close(); }
@Test public void testSaveBulkNullHBaseTableConnection() throws Exception { List<Document> documents = new Vector<Document>(); for (int i = 0; i < 10; i++) { documents.add(new Document(UUID.randomUUID().toString(), System.currentTimeMillis(), mapper.valueToTree(Collections.singletonMap("TEST_NAME", "BULK_SAVE_TEST")))); } when(hBaseTableConnection.getTable(Matchers.<Table>any())).thenReturn(null); try { HBaseDataStore.save(TEST_APP, documents); fail(); } catch (DataStoreException ex) { assertEquals(DataStoreException.ErrorCode.STORE_MULTI_SAVE, ex.getErrorCode()); } }