Java Code Examples for com.carrotsearch.hppc.cursors.ObjectObjectCursor

The following examples show how to use com.carrotsearch.hppc.cursors.ObjectObjectCursor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: dk-fitting   Source File: ElasticsearchSchema.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();

    try {
        GetMappingsResponse response = client.admin().indices().getMappings(
                new GetMappingsRequest().indices(index)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(index);
        for (ObjectObjectCursor<String, MappingMetaData> c: mapping) {
            builder.put(c.key, new ElasticsearchTable(client, index, c.key));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return builder.build();
}
 
Example 2
Source Project: dk-fitting   Source File: ElasticsearchSchema.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();

    try {
        GetMappingsResponse response = client.admin().indices().getMappings(
                new GetMappingsRequest().indices(index)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(index);
        for (ObjectObjectCursor<String, MappingMetaData> c: mapping) {
            builder.put(c.key, new ElasticsearchTable(client, index, c.key));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return builder.build();
}
 
Example 3
Source Project: Elasticsearch   Source File: AggregatedDfs.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(final StreamOutput out) throws IOException {
    out.writeVInt(termStatistics.size());
    
    for (ObjectObjectCursor<Term, TermStatistics> c : termStatistics()) {
        Term term = (Term) c.key;
        out.writeString(term.field());
        out.writeBytesRef(term.bytes());
        TermStatistics stats = (TermStatistics) c.value;
        out.writeBytesRef(stats.term());
        out.writeVLong(stats.docFreq());
        out.writeVLong(DfsSearchResult.addOne(stats.totalTermFreq()));
    }

    DfsSearchResult.writeFieldStats(out, fieldStatistics);
    out.writeVLong(maxDoc);
}
 
Example 4
Source Project: Elasticsearch   Source File: RestAliasAction.java    License: Apache License 2.0 6 votes vote down vote up
private Table buildTable(RestRequest request, GetAliasesResponse response) {
    Table table = getTableWithHeader(request);

    for (ObjectObjectCursor<String, List<AliasMetaData>> cursor : response.getAliases()) {
        String indexName = cursor.key;
        for (AliasMetaData aliasMetaData : cursor.value) {
            table.startRow();
            table.addCell(aliasMetaData.alias());
            table.addCell(indexName);
            table.addCell(aliasMetaData.filteringRequired() ? "*" : "-");
            String indexRouting = Strings.hasLength(aliasMetaData.indexRouting()) ? aliasMetaData.indexRouting() : "-";
            table.addCell(indexRouting);
            String searchRouting = Strings.hasLength(aliasMetaData.searchRouting()) ? aliasMetaData.searchRouting() : "-";
            table.addCell(searchRouting);
            table.endRow();
        }
    }

    return table;
}
 
Example 5
Source Project: Elasticsearch   Source File: AsyncShardFetch.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Fills the shard fetched data with new (data) nodes and a fresh NodeEntry, and removes from
 * it nodes that are no longer part of the state.
 */
private void fillShardCacheWithDataNodes(Map<String, NodeEntry<T>> shardCache, DiscoveryNodes nodes) {
    // verify that all current data nodes are there
    for (ObjectObjectCursor<String, DiscoveryNode> cursor : nodes.dataNodes()) {
        DiscoveryNode node = cursor.value;
        if (shardCache.containsKey(node.getId()) == false) {
            shardCache.put(node.getId(), new NodeEntry<T>(node.getId()));
        }
    }
    // remove nodes that are not longer part of the data nodes set
    for (Iterator<String> it = shardCache.keySet().iterator(); it.hasNext(); ) {
        String nodeId = it.next();
        if (nodes.nodeExists(nodeId) == false) {
            it.remove();
        }
    }
}
 
Example 6
Source Project: Elasticsearch   Source File: DiscoveryNodes.java    License: Apache License 2.0 6 votes vote down vote up
public DiscoveryNodes build() {
    ImmutableOpenMap.Builder<String, DiscoveryNode> dataNodesBuilder = ImmutableOpenMap.builder();
    ImmutableOpenMap.Builder<String, DiscoveryNode> masterNodesBuilder = ImmutableOpenMap.builder();
    Version minNodeVersion = Version.CURRENT;
    Version minNonClientNodeVersion = Version.CURRENT;
    for (ObjectObjectCursor<String, DiscoveryNode> nodeEntry : nodes) {
        if (nodeEntry.value.dataNode()) {
            dataNodesBuilder.put(nodeEntry.key, nodeEntry.value);
            minNonClientNodeVersion = Version.smallest(minNonClientNodeVersion, nodeEntry.value.version());
        }
        if (nodeEntry.value.masterNode()) {
            masterNodesBuilder.put(nodeEntry.key, nodeEntry.value);
            minNonClientNodeVersion = Version.smallest(minNonClientNodeVersion, nodeEntry.value.version());
        }
        minNodeVersion = Version.smallest(minNodeVersion, nodeEntry.value.version());
    }

    return new DiscoveryNodes(nodes.build(), dataNodesBuilder.build(), masterNodesBuilder.build(), deadNodes.build(), masterNodeId, localNodeId, minNodeVersion, minNonClientNodeVersion);
}
 
Example 7
@Override
protected void masterOperation(GetIndexTemplatesRequest request, ClusterState state, ActionListener<GetIndexTemplatesResponse> listener) {
    List<IndexTemplateMetaData> results;

    // If we did not ask for a specific name, then we return all templates
    if (request.names().length == 0) {
        results = Arrays.asList(state.metaData().templates().values().toArray(IndexTemplateMetaData.class));
    } else {
        results = new ArrayList<>();
    }

    for (String name : request.names()) {
        if (Regex.isSimpleMatchPattern(name)) {
            for (ObjectObjectCursor<String, IndexTemplateMetaData> entry : state.metaData().templates()) {
                if (Regex.simpleMatch(name, entry.key)) {
                    results.add(entry.value);
                }
            }
        } else if (state.metaData().templates().containsKey(name)) {
            results.add(state.metaData().templates().get(name));
        }
    }

    listener.onResponse(new GetIndexTemplatesResponse(results));
}
 
Example 8
Source Project: Elasticsearch   Source File: IndicesShardStoresResponse.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeVInt(storeStatuses.size());
    for (ObjectObjectCursor<String, ImmutableOpenIntMap<List<StoreStatus>>> indexShards : storeStatuses) {
        out.writeString(indexShards.key);
        out.writeVInt(indexShards.value.size());
        for (IntObjectCursor<List<StoreStatus>> shardStatusesEntry : indexShards.value) {
            out.writeInt(shardStatusesEntry.key);
            out.writeVInt(shardStatusesEntry.value.size());
            for (StoreStatus storeStatus : shardStatusesEntry.value) {
                storeStatus.writeTo(out);
            }
        }
    }
    out.writeVInt(failures.size());
    for (ShardOperationFailedException failure : failures) {
        failure.writeTo(out);
    }
}
 
Example 9
protected void dumpIndices() throws Exception {
    ThreadContext threadContext = esNode1.client().threadPool().getThreadContext();
    try (StoredContext cxt = threadContext.stashContext()) {
        threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true");
        ClusterStateResponse response = esNode1.client().admin().cluster().prepareState().get();
        Iterator<ObjectObjectCursor<String, IndexMetaData>> iterator = response.getState().getMetaData().indices().iterator();
        while (iterator.hasNext()) {
            ObjectObjectCursor<String, IndexMetaData> c = (ObjectObjectCursor<String, IndexMetaData>) iterator.next();
            IndexMetaData meta = c.value;
            ImmutableOpenMap<String, MappingMetaData> mappings = meta.getMappings();
            Iterator<String> it = mappings.keysIt();
            while (it.hasNext()) {
                String key = it.next();
                System.out.println(String.format("%s %s %s", c.key, key,  mappings.get(key).type()));
            }
        }
    }
}
 
Example 10
Source Project: crate   Source File: GatewayMetaState.java    License: Apache License 2.0 6 votes vote down vote up
private static <Data> boolean applyPluginUpgraders(ImmutableOpenMap<String, Data> existingData,
                                                   UnaryOperator<Map<String, Data>> upgrader,
                                                   Consumer<String> removeData,
                                                   BiConsumer<String, Data> putData) {
    // collect current data
    Map<String, Data> existingMap = new HashMap<>();
    for (ObjectObjectCursor<String, Data> customCursor : existingData) {
        existingMap.put(customCursor.key, customCursor.value);
    }
    // upgrade global custom meta data
    Map<String, Data> upgradedCustoms = upgrader.apply(existingMap);
    if (upgradedCustoms.equals(existingMap) == false) {
        // remove all data first so a plugin can remove custom metadata or templates if needed
        existingMap.keySet().forEach(removeData);
        for (Map.Entry<String, Data> upgradedCustomEntry : upgradedCustoms.entrySet()) {
            putData.accept(upgradedCustomEntry.getKey(), upgradedCustomEntry.getValue());
        }
        return true;
    }
    return false;
}
 
Example 11
Source Project: crate   Source File: ClusterBlocks.java    License: Apache License 2.0 6 votes vote down vote up
private static EnumMap<ClusterBlockLevel, ImmutableLevelHolder> generateLevelHolders(Set<ClusterBlock> global,
        ImmutableOpenMap<String, Set<ClusterBlock>> indicesBlocks) {
    EnumMap<ClusterBlockLevel, ImmutableLevelHolder> levelHolders = new EnumMap<>(ClusterBlockLevel.class);
    for (final ClusterBlockLevel level : ClusterBlockLevel.values()) {
        Predicate<ClusterBlock> containsLevel = block -> block.contains(level);
        Set<ClusterBlock> newGlobal = unmodifiableSet(global.stream()
            .filter(containsLevel)
            .collect(toSet()));

        ImmutableOpenMap.Builder<String, Set<ClusterBlock>> indicesBuilder = ImmutableOpenMap.builder();
        for (ObjectObjectCursor<String, Set<ClusterBlock>> entry : indicesBlocks) {
            indicesBuilder.put(entry.key, unmodifiableSet(entry.value.stream()
                .filter(containsLevel)
                .collect(toSet())));
        }
        levelHolders.put(level, new ImmutableLevelHolder(newGlobal, indicesBuilder.build()));
    }
    return levelHolders;
}
 
Example 12
Source Project: crate   Source File: SnapshotsInProgress.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeVInt(entries.size());
    for (Entry entry : entries) {
        entry.snapshot().writeTo(out);
        out.writeBoolean(entry.includeGlobalState());
        out.writeBoolean(entry.partial());
        out.writeByte(entry.state().value());
        out.writeVInt(entry.indices().size());
        for (IndexId index : entry.indices()) {
            index.writeTo(out);
        }
        out.writeLong(entry.startTime());
        out.writeVInt(entry.shards().size());
        for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardEntry : entry.shards()) {
            shardEntry.key.writeTo(out);
            shardEntry.value.writeTo(out);
        }
        out.writeLong(entry.repositoryStateId);
        out.writeOptionalString(entry.failure);
        if (out.getVersion().onOrAfter(SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION)) {
            out.writeBoolean(entry.useShardGenerations);
        }
    }
}
 
Example 13
Source Project: crate   Source File: RestoreInProgress.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeVInt(entries.size());
    for (Entry entry : entries) {
        entry.snapshot().writeTo(out);
        out.writeByte(entry.state().value());
        out.writeVInt(entry.indices().size());
        for (String index : entry.indices()) {
            out.writeString(index);
        }
        out.writeVInt(entry.shards().size());
        for (ObjectObjectCursor<ShardId, ShardRestoreStatus> shardEntry : entry.shards()) {
            shardEntry.key.writeTo(out);
            shardEntry.value.writeTo(out);
        }
    }
}
 
Example 14
Source Project: crate   Source File: ClusterChangedEvent.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns a set of custom meta data types when any custom metadata for the cluster has changed
 * between the previous cluster state and the new cluster state. custom meta data types are
 * returned iff they have been added, updated or removed between the previous and the current state
 */
public Set<String> changedCustomMetaDataSet() {
    Set<String> result = new HashSet<>();
    ImmutableOpenMap<String, MetaData.Custom> currentCustoms = state.metaData().customs();
    ImmutableOpenMap<String, MetaData.Custom> previousCustoms = previousState.metaData().customs();
    if (currentCustoms.equals(previousCustoms) == false) {
        for (ObjectObjectCursor<String, MetaData.Custom> currentCustomMetaData : currentCustoms) {
            // new custom md added or existing custom md changed
            if (previousCustoms.containsKey(currentCustomMetaData.key) == false
                    || currentCustomMetaData.value.equals(previousCustoms.get(currentCustomMetaData.key)) == false) {
                result.add(currentCustomMetaData.key);
            }
        }
        // existing custom md deleted
        for (ObjectObjectCursor<String, MetaData.Custom> previousCustomMetaData : previousCustoms) {
            if (currentCustoms.containsKey(previousCustomMetaData.key) == false) {
                result.add(previousCustomMetaData.key);
            }
        }
    }
    return result;
}
 
Example 15
Source Project: crate   Source File: SnapshotsService.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns list of indices with missing shards, and list of indices that are closed
 *
 * @param shards list of shard statuses
 * @return list of failed and closed indices
 */
private static Tuple<Set<String>, Set<String>> indicesWithMissingShards(
    ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards, MetaData metaData) {
    Set<String> missing = new HashSet<>();
    Set<String> closed = new HashSet<>();
    for (ObjectObjectCursor<ShardId, SnapshotsInProgress.ShardSnapshotStatus> entry : shards) {
        if (entry.value.state() == ShardState.MISSING) {
            if (metaData.hasIndex(entry.key.getIndex().getName()) &&
                metaData.getIndexSafe(entry.key.getIndex()).getState() == IndexMetaData.State.CLOSE) {
                closed.add(entry.key.getIndex().getName());
            } else {
                missing.add(entry.key.getIndex().getName());
            }
        }
    }
    return new Tuple<>(missing, closed);
}
 
Example 16
Source Project: crate   Source File: RestoreService.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check if any of the indices to be closed are currently being restored from a snapshot and fail closing if such an index
 * is found as closing an index that is being restored makes the index unusable (it cannot be recovered).
 */
public static void checkIndexClosing(ClusterState currentState, Set<IndexMetaData> indices) {
    RestoreInProgress restore = currentState.custom(RestoreInProgress.TYPE);
    if (restore != null) {
        Set<Index> indicesToFail = null;
        for (RestoreInProgress.Entry entry : restore.entries()) {
            for (ObjectObjectCursor<ShardId, RestoreInProgress.ShardRestoreStatus> shard : entry.shards()) {
                if (!shard.value.state().completed()) {
                    IndexMetaData indexMetaData = currentState.metaData().index(shard.key.getIndex());
                    if (indexMetaData != null && indices.contains(indexMetaData)) {
                        if (indicesToFail == null) {
                            indicesToFail = new HashSet<>();
                        }
                        indicesToFail.add(shard.key.getIndex());
                    }
                }
            }
        }
        if (indicesToFail != null) {
            throw new IllegalArgumentException("Cannot close indices that are being restored: " + indicesToFail);
        }
    }
}
 
Example 17
Source Project: crate   Source File: TransportGetIndexTemplatesAction.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(GetIndexTemplatesRequest request, ClusterState state, ActionListener<GetIndexTemplatesResponse> listener) {
    List<IndexTemplateMetaData> results;

    // If we did not ask for a specific name, then we return all templates
    if (request.names().length == 0) {
        results = Arrays.asList(state.metaData().templates().values().toArray(IndexTemplateMetaData.class));
    } else {
        results = new ArrayList<>();
    }

    for (String name : request.names()) {
        if (Regex.isSimpleMatchPattern(name)) {
            for (ObjectObjectCursor<String, IndexTemplateMetaData> entry : state.metaData().templates()) {
                if (Regex.simpleMatch(name, entry.key)) {
                    results.add(entry.value);
                }
            }
        } else if (state.metaData().templates().containsKey(name)) {
            results.add(state.metaData().templates().get(name));
        }
    }

    listener.onResponse(new GetIndexTemplatesResponse(results));
}
 
Example 18
Source Project: crate   Source File: TransportSchemaUpdateAction.java    License: Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static ClusterState updateTemplate(NamedXContentRegistry xContentRegistry,
                                   ClusterState currentState,
                                   String templateName,
                                   Map<String, Object> newMapping) throws Exception {
    IndexTemplateMetaData template = currentState.metaData().templates().get(templateName);
    if (template == null) {
        throw new ResourceNotFoundException("Template \"" + templateName + "\" for partitioned table is missing");
    }

    IndexTemplateMetaData.Builder templateBuilder = new IndexTemplateMetaData.Builder(template);
    for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
        Map<String, Object> source = parseMapping(xContentRegistry, cursor.value.toString());
        mergeIntoSource(source, newMapping);
        try (XContentBuilder xContentBuilder = JsonXContent.contentBuilder()) {
            templateBuilder.putMapping(cursor.key, Strings.toString(xContentBuilder.map(source)));
        }
    }
    MetaData.Builder builder = MetaData.builder(currentState.metaData()).put(templateBuilder);
    return ClusterState.builder(currentState).metaData(builder).build();
}
 
Example 19
Source Project: crate   Source File: Templates.java    License: Apache License 2.0 6 votes vote down vote up
public static IndexTemplateMetaData.Builder copyWithNewName(IndexTemplateMetaData source, RelationName newName) {
    String targetTemplateName = PartitionName.templateName(newName.schema(), newName.name());
    String targetAlias = newName.indexNameOrAlias();
    IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData
        .builder(targetTemplateName)
        .patterns(Collections.singletonList(PartitionName.templatePrefix(newName.schema(), newName.name())))
        .settings(source.settings())
        .order(source.order())
        .putAlias(AliasMetaData.builder(targetAlias).build())
        .version(source.version());

    for (ObjectObjectCursor<String, CompressedXContent> mapping : source.mappings()) {
        try {
            templateBuilder.putMapping(mapping.key, mapping.value);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return templateBuilder;
}
 
Example 20
Source Project: crate   Source File: PartitionInfos.java    License: Apache License 2.0 6 votes vote down vote up
private static PartitionInfo createPartitionInfo(ObjectObjectCursor<String, IndexMetaData> indexMetaDataEntry) {
    PartitionName partitionName = PartitionName.fromIndexOrTemplate(indexMetaDataEntry.key);
    try {
        IndexMetaData indexMetaData = indexMetaDataEntry.value;
        MappingMetaData mappingMetaData = indexMetaData.mapping(Constants.DEFAULT_MAPPING_TYPE);
        Map<String, Object> mappingMap = mappingMetaData.sourceAsMap();
        Map<String, Object> valuesMap = buildValuesMap(partitionName, mappingMetaData);
        Settings settings = indexMetaData.getSettings();
        String numberOfReplicas = NumberOfReplicas.fromSettings(settings);
        return new PartitionInfo(
            partitionName,
            indexMetaData.getNumberOfShards(),
            numberOfReplicas,
            IndexMetaData.SETTING_INDEX_VERSION_CREATED.get(settings),
            settings.getAsVersion(IndexMetaData.SETTING_VERSION_UPGRADED, null),
            DocIndexMetaData.isClosed(indexMetaData, mappingMap, false),
            valuesMap,
            indexMetaData.getSettings());
    } catch (Exception e) {
        LOGGER.trace("error extracting partition infos from index {}", e, indexMetaDataEntry.key);
        return null; // must filter on null
    }
}
 
Example 21
Source Project: summerframework   Source File: ElasticsearchTemplate.java    License: Apache License 2.0 5 votes vote down vote up
public List<Map<String, Object>> getAllMapping(String index) throws IOException {
    List<Map<String, Object>> result = new ArrayList<>();
    ImmutableOpenMap<String, MappingMetaData> mappings = esClient.admin().cluster().prepareState().execute()
        .actionGet().getState().getMetaData().getIndices().get(index).getMappings();

    for (ObjectObjectCursor<String, MappingMetaData> cursor : mappings) {
        log.info("type is:{}", cursor.key);
        result.add(cursor.value.getSourceAsMap());
    }
    return result;
}
 
Example 22
Source Project: summerframework   Source File: ElasticsearchTemplate.java    License: Apache License 2.0 5 votes vote down vote up
public List<Map<String, Object>> getAllMapping(String index) throws IOException {
    List<Map<String, Object>> result = new ArrayList<>();
    ImmutableOpenMap<String, MappingMetaData> mappings = esClient.admin().cluster().prepareState().execute()
        .actionGet().getState().getMetaData().getIndices().get(index).getMappings();

    for (ObjectObjectCursor<String, MappingMetaData> cursor : mappings) {
        log.info("type is:{}", cursor.key);
        result.add(cursor.value.getSourceAsMap());
    }
    return result;
}
 
Example 23
Source Project: presto-connectors   Source File: Elasticsearch2Client.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    //----es scher error --
    Thread.currentThread().setName("getTable_001");
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}
 
Example 24
Source Project: presto-connectors   Source File: Elasticsearch6Client.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    Thread.currentThread().setName("getTable_001"); //----es scher error --
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}
 
Example 25
Source Project: presto-connectors   Source File: Elasticsearch5Client.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    Thread.currentThread().setName("getTable_001"); //----es scher error --
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}
 
Example 26
Source Project: Elasticsearch   Source File: AlterTableOperation.java    License: Apache License 2.0 5 votes vote down vote up
private ListenableFuture<Long> updateTemplate(Map<String, Object> newMappings,
                                              Settings newSettings,
                                              TableIdent tableIdent,
                                              AbstractDDLAnalyzedStatement statement) {
    String templateName = PartitionName.templateName(tableIdent.schema(), tableIdent.name());
    IndexTemplateMetaData indexTemplateMetaData =
            clusterService.state().metaData().templates().get(templateName);
    if (indexTemplateMetaData == null) {
        return Futures.immediateFailedFuture(new RuntimeException("Template for partitioned table is missing"));
    }

    // merge mappings
    Map<String, Object> mapping = mergeTemplateMapping(indexTemplateMetaData, newMappings);

    // merge settings
    Settings.Builder settingsBuilder = Settings.builder();
    settingsBuilder.put(indexTemplateMetaData.settings());
    settingsBuilder.put(newSettings);

    PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName)
            .create(false)
            .mapping(Constants.DEFAULT_MAPPING_TYPE, mapping)
            .order(indexTemplateMetaData.order())
            .settings(settingsBuilder.build())
            .template(indexTemplateMetaData.template());

    request.putHeader(LoginUserContext.USER_INFO_KEY, statement.getParameterContext().getLoginUserContext());
    for (ObjectObjectCursor<String, AliasMetaData> container : indexTemplateMetaData.aliases()) {
        Alias alias = new Alias(container.key);
        request.alias(alias);
    }

    SettableFuture<Long> result = SettableFuture.create();
    transportActionProvider.transportPutIndexTemplateAction().execute(request,
            new SettableFutureToNullActionListener<PutIndexTemplateResponse>(result));

    return result;
}
 
Example 27
Source Project: Elasticsearch   Source File: FieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
private MultiFields(ContentPath.Type pathType, ImmutableOpenMap<String, FieldMapper> mappers) {
    this.pathType = pathType;
    ImmutableOpenMap.Builder<String, FieldMapper> builder = new ImmutableOpenMap.Builder<>();
    // we disable the all in multi-field mappers
    for (ObjectObjectCursor<String, FieldMapper> cursor : mappers) {
        FieldMapper mapper = cursor.value;
        if (mapper instanceof AllFieldMapper.IncludeInAll) {
            mapper = (FieldMapper) ((AllFieldMapper.IncludeInAll) mapper).unsetIncludeInAll();
        }
        builder.put(cursor.key, mapper);
    }
    this.mappers = builder.build();
}
 
Example 28
Source Project: Elasticsearch   Source File: RestGetAliasesAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name");
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases);
    getAliasesRequest.indices(indices);
    getAliasesRequest.indicesOptions(IndicesOptions.fromRequest(request, getAliasesRequest.indicesOptions()));
    getAliasesRequest.local(request.paramAsBoolean("local", getAliasesRequest.local()));

    client.admin().indices().getAliases(getAliasesRequest, new RestBuilderListener<GetAliasesResponse>(channel) {
        @Override
        public RestResponse buildResponse(GetAliasesResponse response, XContentBuilder builder) throws Exception {
            // empty body, if indices were specified but no aliases were
            if (indices.length > 0 && response.getAliases().isEmpty()) {
                return new BytesRestResponse(OK, builder.startObject().endObject());
            } else if (response.getAliases().isEmpty()) {
                String message = String.format(Locale.ROOT, "alias [%s] missing", toNamesString(getAliasesRequest.aliases()));
                builder.startObject()
                        .field("error", message)
                        .field("status", RestStatus.NOT_FOUND.getStatus())
                        .endObject();
                return new BytesRestResponse(RestStatus.NOT_FOUND, builder);
            }

            builder.startObject();
            for (ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) {
                builder.startObject(entry.key, XContentBuilder.FieldCaseConversion.NONE);
                builder.startObject(Fields.ALIASES);
                for (AliasMetaData alias : entry.value) {
                    AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS);
                }
                builder.endObject();
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
 
Example 29
Source Project: Elasticsearch   Source File: RestGetWarmerAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final String[] types = Strings.splitStringByCommaToArray(request.param("type"));
    final String[] names = request.paramAsStringArray("name", Strings.EMPTY_ARRAY);

    GetWarmersRequest getWarmersRequest = new GetWarmersRequest();
    getWarmersRequest.indices(indices).types(types).warmers(names);
    getWarmersRequest.local(request.paramAsBoolean("local", getWarmersRequest.local()));
    getWarmersRequest.indicesOptions(IndicesOptions.fromRequest(request, getWarmersRequest.indicesOptions()));
    client.admin().indices().getWarmers(getWarmersRequest, new RestBuilderListener<GetWarmersResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetWarmersResponse response, XContentBuilder builder) throws Exception {
            if (indices.length > 0 && response.warmers().isEmpty()) {
                return new BytesRestResponse(OK, builder.startObject().endObject());
            }

            builder.startObject();
            for (ObjectObjectCursor<String, List<IndexWarmersMetaData.Entry>> entry : response.warmers()) {
                builder.startObject(entry.key, XContentBuilder.FieldCaseConversion.NONE);
                builder.startObject(IndexWarmersMetaData.TYPE, XContentBuilder.FieldCaseConversion.NONE);
                for (IndexWarmersMetaData.Entry warmerEntry : entry.value) {
                    IndexWarmersMetaData.toXContent(warmerEntry, builder, request);
                }
                builder.endObject();
                builder.endObject();
            }
            builder.endObject();

            return new BytesRestResponse(OK, builder);
        }
    });
}
 
Example 30
Source Project: Elasticsearch   Source File: RestGetSettingsAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] names = request.paramAsStringArrayOrEmptyIfAll("name");
    GetSettingsRequest getSettingsRequest = new GetSettingsRequest()
            .indices(Strings.splitStringByCommaToArray(request.param("index")))
            .indicesOptions(IndicesOptions.fromRequest(request, IndicesOptions.strictExpandOpen()))
            .humanReadable(request.hasParam("human"))
            .names(names);
    getSettingsRequest.local(request.paramAsBoolean("local", getSettingsRequest.local()));

    client.admin().indices().getSettings(getSettingsRequest, new RestBuilderListener<GetSettingsResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetSettingsResponse getSettingsResponse, XContentBuilder builder) throws Exception {
            builder.startObject();
            for (ObjectObjectCursor<String, Settings> cursor : getSettingsResponse.getIndexToSettings()) {
                // no settings, jump over it to shorten the response data
                if (cursor.value.getAsMap().isEmpty()) {
                    continue;
                }
                builder.startObject(cursor.key, XContentBuilder.FieldCaseConversion.NONE);
                builder.startObject(Fields.SETTINGS);
                cursor.value.toXContent(builder, request);
                builder.endObject();
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}