/** * Read a document from the search index for a given id. * This is the cheapest document retrieval from the '_source' field because * elasticsearch does not do any json transformation or parsing. We * get simply the text from the '_source' field. This might be useful to * make a dump from the index content. * * @param id * the unique identifier of a document * @return the document as source text */ public byte[] readSource(String indexName, final String id) { GetResponse response = elasticsearchClient.prepareGet(indexName, null, id).execute().actionGet(); return response.getSourceAsBytes(); }
public void run() { if (esClient == null) { try { esClient = ElasticSearchConnection.getClient(stormConf, "config"); } catch (Exception e) { LOG.error("Exception while creating ES connection", e); } } if (esClient != null) { LOG.info("Reloading json resources from ES"); try { GetResponse response = esClient.get(new GetRequest( "config", "config", resource.getResourceFile())); resource.loadJSONResources(new ByteArrayInputStream( response.getSourceAsBytes())); } catch (Exception e) { LOG.error("Can't load config from ES", e.getMessage()); } } } }, 0, refreshRate * 1000);
public void run() { if (esClient == null) { try { esClient = ElasticSearchConnection.getClient(stormConf, "config"); } catch (Exception e) { LOG.error("Exception while creating ES connection", e); } } if (esClient != null) { LOG.info("Reloading json resources from ES"); try { GetResponse response = esClient.get(new GetRequest( "config", "config", resource.getResourceFile())); resource.loadJSONResources(new ByteArrayInputStream( response.getSourceAsBytes())); } catch (Exception e) { LOG.error("Can't load config from ES", e.getMessage()); } } } }, 0, refreshRate * 1000);
private ElasticSearchDoc parse(final GetResponse response) { ElasticSearchDoc result = null; if (response.isExists()) { final Map<String, Object> responseMap = response.getSourceAsMap(); final String user = (String) responseMap.get(ElasticSearchDoc.Field.USER); final boolean deleted = (boolean) responseMap.get(ElasticSearchDoc.Field.DELETED); final long timestamp = (long) responseMap.get(ElasticSearchDoc.Field.TIMESTAMP); @SuppressWarnings("unchecked") final Object dto = metacatJson.parseJsonValue( response.getSourceAsBytes(), getClass(response.getType()) ); result = new ElasticSearchDoc(response.getId(), dto, user, deleted, timestamp); } return result; }
/** get the last tick from ES */ public Tick getLastTick() { LOG.debug("Fetching last tick"); GetResponse get = client // .prepareGet(index, type, id) // .get(); if (get.isSourceEmpty()) { return null; } byte[] bytes = get.getSourceAsBytes(); try { return MAPPER.readValue(bytes, Tick.class); } catch (IOException e) { return null; } } }
@Override public List<T> selectByPrimaryKeys(Collection<ID> primaryKeys) { try { // bulk delete if(null == primaryKeys || primaryKeys.size() == 0){ return null; } List<T> list = new ArrayList<T>(); for(ID primaryKey: primaryKeys){ // bulk delete GetResponse response = getElasticClient().prepareGet(getIndexName(), getIndexType(), "" + primaryKey).execute().get(); if(!response.isSourceEmpty()){ response.getSourceAsBytes(); } } } catch (Exception e) { LOG.error(e.getLocalizedMessage(), e); } return null; }
@Override public T selectByPrimaryKey(ID primaryKey) { try { // bulk delete GetResponse response = getElasticClient().prepareGet(getIndexName(), getIndexType(), "" + primaryKey).execute().get(); if(!response.isSourceEmpty()){ response.getSourceAsBytes(); } } catch (Exception e) { LOG.error(e.getLocalizedMessage(), e); } return null; }
@Override protected ElasticsearchDocumentHistory loadExistingDocumentHistory(String documentId) throws BaleenException { try { GetResponse response = new GetRequestBuilder(elasticsearch.getClient(), GetAction.INSTANCE) .setId(documentId) .setIndex(esIndex) .setType(esType) .get(); if (!response.isExists() || response.isSourceEmpty()) { // If we don't have any data, then let parent implementation create a new history return null; } else { ESHistory esh = mapper.readValue(response.getSourceAsBytes(), ESHistory.class); if (esh == null) { return new ElasticsearchDocumentHistory( this, documentId, new LinkedBlockingDeque<HistoryEvent>(Collections.emptyList())); } else { return new ElasticsearchDocumentHistory( this, documentId, new LinkedBlockingDeque<HistoryEvent>(esh.getEvents())); } } } catch (IOException e) { throw new BaleenException(e); } } }
/** * Retrieve an indexed Fact by its UUID. Returns NULL if Fact cannot be fetched from ElasticSearch. * * @param id UUID of indexed Fact * @return Indexed Fact or NULL if not available */ public FactDocument getFact(UUID id) { if (id == null) return null; GetResponse response; try { GetRequest request = new GetRequest(INDEX_NAME, TYPE_NAME, id.toString()); response = clientFactory.getHighLevelClient().get(request); } catch (IOException ex) { throw logAndExit(ex, String.format("Could not perform request to fetch Fact with id = %s.", id)); } if (response.isExists()) { LOGGER.info("Successfully fetched Fact with id = %s.", id); return decodeFactDocument(id, response.getSourceAsBytes()); } else { // Fact isn't indexed in ElasticSearch, log warning and return null. LOGGER.warning("Could not fetch Fact with id = %s. Fact not indexed?", id); return null; } }