public static void checkIndexNameForRead(Settings settings) { Resource readResource = new Resource(settings, true); if (readResource.index().contains("{") && readResource.index().contains("}")) { throw new EsHadoopIllegalArgumentException("Cannot read indices that have curly brace field extraction patterns in them: " + readResource.index()); } }
public SearchRequestBuilder resource(Resource resource) { this.indices = resource.index(); if (resource.isTyped()) { this.types = resource.type(); } else { this.types = null; } return this; }
public boolean touch() { return client.touch(resources.getResourceWrite().index()); }
public static void checkIndexStatus(Settings settings) { if (!settings.getIndexReadAllowRedStatus()) { RestClient bootstrap = new RestClient(settings); Resource readResource = new Resource(settings, true); try { if (bootstrap.indexExists(readResource.index())) { RestClient.Health status = bootstrap.getHealth(readResource.index()); if (status == RestClient.Health.RED) { throw new EsHadoopIllegalStateException("Index specified [" + readResource.index() + "] is either red or " + "includes an index that is red, and thus all requested data cannot be safely and fully loaded. " + "Bailing out..."); } } } finally { bootstrap.close(); } } }
throw new EsHadoopIllegalArgumentException("Cannot initialize alias write resource [" + resource.index() + "] if it does not have any alias entries."); } else if (indexAliasTable.size() > 1) { String indexName = indexRow.getKey(); Map<String, IndicesAliases.Alias> aliases = indexRow.getValue(); IndicesAliases.Alias aliasInfo = aliases.get(resource.index()); if (aliasInfo.isWriteIndex()) { currentWriteIndex = indexName; throw new EsHadoopIllegalArgumentException("Attempting to write to alias [" + resource.index() + "], " + "but detected multiple indices [" + indexAliasTable.size() + "] with no write index selected. " + "Bailing out...");
protected List<List<Map<String, Object>>> doGetReadTargetShards() { return client.targetShards(resources.getResourceRead().index(), SettingsUtils.getFixedRouting(settings)); }
public boolean waitForYellow() { return client.waitForHealth(resources.getResourceWrite().index(), RestClient.Health.YELLOW, TimeValue.timeValueSeconds(10)); }
@Override public BulkOutputGenerator addSuccess(String operation, int status) { Assert.notNull(resource); items.add(getSuccess() .replace(OP, operation) .replace(IDX, resource.index()) .replace(TYPE, resource.type()) .replace(ID, UUID.randomUUID().toString()) .replace(VER, "1") .replace(STAT, "201") ); return this; }
public boolean isEmpty(boolean read) { Resource res = (read ? resources.getResourceRead() : resources.getResourceWrite()); boolean exists = client.indexExists(res.index()); return (exists ? count(read) <= 0 : true); }
protected Map<ShardInfo, NodeInfo> doGetWriteTargetPrimaryShards(boolean clientNodesOnly) { List<List<Map<String, Object>>> info = client.targetShards(resources.getResourceWrite().index(), SettingsUtils.getFixedRouting(settings)); Map<ShardInfo, NodeInfo> shards = new LinkedHashMap<ShardInfo, NodeInfo>(); List<NodeInfo> nodes = client.getHttpNodes(clientNodesOnly); Map<String, NodeInfo> nodeMap = new HashMap<String, NodeInfo>(nodes.size()); for (NodeInfo node : nodes) { nodeMap.put(node.getId(), node); } for (List<Map<String, Object>> shardGroup : info) { // consider only primary shards for (Map<String, Object> shardData : shardGroup) { ShardInfo shard = new ShardInfo(shardData); if (shard.isPrimary()) { NodeInfo node = nodeMap.get(shard.getNode()); if (node == null) { log.warn(String.format("Cannot find node with id [%s] (is HTTP enabled?) from shard [%s] in nodes [%s]; layout [%s]", shard.getNode(), shard, nodes, info)); return null; } shards.put(shard, node); break; } } } return shards; }
@Override public BulkOutputGenerator addFailure(String operation, int status, String type, String errorMessage) { Assert.notNull(resource); errors = true; items.add(getFailure() .replace(OP, operation) .replace(IDX, resource.index()) .replace(TYPE, resource.type()) .replace(ID, UUID.randomUUID().toString()) .replace(STAT, Integer.toString(status)) .replace(ETYPE, type) .replace(EMESG, errorMessage) ); return this; }
@Override public BulkOutputGenerator addRejection(String operation) { Assert.notNull(resource); errors = true; items.add(getFailure() .replace(OP, operation) .replace(IDX, resource.index()) .replace(TYPE, resource.type()) .replace(ID, UUID.randomUUID().toString()) .replace(STAT, Integer.toString(getRejectedStatus())) .replace(ETYPE, getRejectionType()) .replace(EMESG, getRejectionMsg()) ); return this; }
public boolean resourceExists(boolean read) { Resource res = (read ? resources.getResourceRead() : resources.getResourceWrite()); // cheap hit - works for exact index names, index patterns, the `_all` resource, and alias names boolean exists = client.indexExists(res.index()); // Do we really care if it's typed? // Yes! If the index exists and a type is given, the type should exist on the index as well. if (exists && res.isTyped()) { exists = client.typeExists(res.index(), res.type()); } // could be a _all or a pattern which is valid for read // try again by asking the mapping - could be expensive if (!exists && read) { try { // make sure the mapping is null since the index might exist but the type might be missing MappingSet mappings = client.getMappings(res); exists = mappings != null && !mappings.isEmpty(); } catch (EsHadoopInvalidRequest ex) { exists = false; } } return exists; }
public String postDocument(Resource resource, BytesArray document) throws IOException { // If untyped, the type() method returns '_doc' Request request = new SimpleRequest(Method.POST, null, resource.index() + "/" + resource.type(), null, document); Response response = execute(request, true); Object id = parseContent(response.body(), "_id"); if (id == null || !StringUtils.hasText(id.toString())) { throw new EsHadoopInvalidRequest( String.format("Could not determine successful write operation. Request[%s > %s] Response[%s]", request.method(), request.path(), IOUtils.asString(response.body()) ) ); } return id.toString(); }
} else { if (!StringUtils.isValidSingularIndexName(resource.index())) { throw new EsHadoopIllegalArgumentException("Illegal write index name [" + resource.index() + "]. Write resources must " + "be lowercase singular index names, with no illegal pattern characters except for multi-resource writes."); GetAliasesRequestBuilder.Response response = null; try { response = new GetAliasesRequestBuilder(bootstrap).aliases(resource.index()).execute(); } catch (EsHadoopInvalidRequest remoteException) { resource.index(), remoteException.getMessage()));
client.delete(resources.getResourceWrite().index() + "/" + resources.getResourceWrite().type()); client.delete(resources.getResourceWrite().index() + "/" + resources.getResourceWrite().type() + "/_query?q=*"); } else { client.delete(resources.getResourceWrite().index() + "/_query?q=*"); StringBuilder sb = new StringBuilder(resources.getResourceWrite().index()); if (resources.getResourceWrite().isTyped()) { sb.append('/').append(resources.getResourceWrite().type());
requestBuilder.slice(partition.getSlice().id, partition.getSlice().max); String[] indices = read.index().split(","); if (QueryUtils.isExplicitlyRequested(partition.getIndex(), indices) == false) { IndicesAliases indicesAliases =