List of usage examples for com.amazonaws.services.dynamodbv2.model LocalSecondaryIndexDescription getKeySchema
public java.util.List<KeySchemaElement> getKeySchema()
The complete key schema for the local secondary index, consisting of one or more pairs of attribute names and key types:
HASH
- partition key
RANGE
- sort key
The partition key of an item is also known as its hash attribute.
From source file:org.apache.metamodel.dynamodb.DynamoDbDataContext.java
License:Apache License
@Override protected Schema getMainSchema() throws MetaModelException { final Map<String, SimpleTableDef> tableDefs = new HashMap<>(); for (final SimpleTableDef tableDef : _tableDefs) { tableDefs.put(tableDef.getName(), tableDef); }/*w w w . jav a 2s. c o m*/ final MutableSchema schema = new MutableSchema(getMainSchemaName()); final ListTablesResult tables = _dynamoDb.listTables(); final List<String> tableNames = tables.getTableNames(); for (final String tableName : tableNames) { final MutableTable table = new MutableTable(tableName, schema); schema.addTable(table); final DescribeTableResult descripeTableResult = _dynamoDb.describeTable(tableName); final TableDescription tableDescription = descripeTableResult.getTable(); // add primary keys addColumnFromKeySchema("Primary index", tableDescription.getKeySchema(), table, true); // add attributes from global and local indices final List<GlobalSecondaryIndexDescription> globalSecondaryIndexes = tableDescription .getGlobalSecondaryIndexes(); if (globalSecondaryIndexes != null) { for (final GlobalSecondaryIndexDescription globalSecondaryIndex : globalSecondaryIndexes) { addColumnFromKeySchema(globalSecondaryIndex.getIndexName(), globalSecondaryIndex.getKeySchema(), table, false); } } final List<LocalSecondaryIndexDescription> localSecondaryIndexes = tableDescription .getLocalSecondaryIndexes(); if (localSecondaryIndexes != null) { for (final LocalSecondaryIndexDescription localSecondaryIndex : localSecondaryIndexes) { addColumnFromKeySchema(localSecondaryIndex.getIndexName(), localSecondaryIndex.getKeySchema(), table, false); } } // add top-level attribute definitions final List<AttributeDefinition> attributeDefinitions = tableDescription.getAttributeDefinitions(); for (final AttributeDefinition attributeDefinition : attributeDefinitions) { final String attributeName = attributeDefinition.getAttributeName(); MutableColumn column = (MutableColumn) table.getColumnByName(attributeName); if (column == null) { column = new MutableColumn(attributeName, table); table.addColumn(column); } final String attributeType = attributeDefinition.getAttributeType(); column.setType(DynamoDbUtils.toColumnType(attributeName, attributeType)); column.setIndexed(true); column.setNativeType(attributeType); } // add additional metadata from SimpleTableDefs if available final SimpleTableDef tableDef = tableDefs.get(tableName); if (tableDef != null) { final String[] columnNames = tableDef.getColumnNames(); final ColumnType[] columnTypes = tableDef.getColumnTypes(); for (int i = 0; i < columnNames.length; i++) { final String columnName = columnNames[i]; final ColumnType columnType = columnTypes[i]; MutableColumn column = (MutableColumn) table.getColumnByName(columnName); if (column == null) { column = new MutableColumn(columnName, table); table.addColumn(column); } if (column.getType() == null && columnType != null) { column.setType(columnType); } } } // add additional attributes based on global and local indices if (globalSecondaryIndexes != null) { for (final GlobalSecondaryIndexDescription globalSecondaryIndex : globalSecondaryIndexes) { final List<String> nonKeyAttributes = globalSecondaryIndex.getProjection() .getNonKeyAttributes(); for (final String attributeName : nonKeyAttributes) { addColumnFromNonKeyAttribute(globalSecondaryIndex.getIndexName(), table, attributeName); } } } if (localSecondaryIndexes != null) { for (final LocalSecondaryIndexDescription localSecondaryIndex : localSecondaryIndexes) { final List<String> nonKeyAttributes = localSecondaryIndex.getProjection().getNonKeyAttributes(); for (final String attributeName : nonKeyAttributes) { addColumnFromNonKeyAttribute(localSecondaryIndex.getIndexName(), table, attributeName); } } } } return schema; }
From source file:org.iternine.jeppetto.dao.dynamodb.DynamoDBQueryModelDAO.java
License:Apache License
private Map<String, Map<String, IndexData>> processIndexes(TableDescription tableDescription, ProjectionExpressionBuilder projectionExpressionBuilder, IndexData baseIndexData) { // Collect information about the local secondary indexes. These will be included with the global indexes below. Map<String, IndexData> localIndexes; List<LocalSecondaryIndexDescription> localSecondaryIndexes = tableDescription.getLocalSecondaryIndexes(); if (localSecondaryIndexes != null) { localIndexes = new HashMap<String, IndexData>(localSecondaryIndexes.size() + 2); // We include these as local indexes to make findUsingQueryModel() code below simpler localIndexes.put(rangeKeyField, baseIndexData); localIndexes.put(null, baseIndexData); for (LocalSecondaryIndexDescription description : localSecondaryIndexes) { String indexField = getKeyAttributeNames(description.getKeySchema()).getSecond(); boolean projectsOverEntity = description.getProjection().getProjectionType().equals("ALL") || projectionExpressionBuilder != null && projectionExpressionBuilder.isCoveredBy(description.getProjection()); List<String> keyFields = new ArrayList<String>(baseIndexData.keyFields); keyFields.add(indexField);/*from ww w. j ava 2s. c om*/ localIndexes.put(indexField, new IndexData(description.getIndexName(), keyFields, projectsOverEntity)); } } else if (rangeKeyField != null) { localIndexes = new HashMap<String, IndexData>(2); localIndexes.put(rangeKeyField, baseIndexData); localIndexes.put(null, baseIndexData); } else { localIndexes = Collections.singletonMap(null, baseIndexData); } // Process the global secondary indexes. When done, add the local index information. List<GlobalSecondaryIndexDescription> globalSecondaryIndexes = tableDescription.getGlobalSecondaryIndexes(); if (globalSecondaryIndexes != null) { Map<String, Map<String, IndexData>> indexes = new HashMap<String, Map<String, IndexData>>( globalSecondaryIndexes.size() + 1); for (GlobalSecondaryIndexDescription description : globalSecondaryIndexes) { Pair<String, String> indexFields = getKeyAttributeNames(description.getKeySchema()); boolean projectsOverEntity = description.getProjection().getProjectionType().equals("ALL") || projectionExpressionBuilder != null && projectionExpressionBuilder.isCoveredBy(description.getProjection()); List<String> keyFields = new ArrayList<String>(); keyFields.add(indexFields.getFirst()); if (indexFields.getSecond() != null) { keyFields.add(indexFields.getSecond()); } keyFields.add(hashKeyField); if (rangeKeyField != null) { keyFields.add(rangeKeyField); } IndexData indexData = new IndexData(description.getIndexName(), keyFields, projectsOverEntity); if (!indexes.containsKey(indexFields.getFirst())) { indexes.put(indexFields.getFirst(), new HashMap<String, IndexData>()); } indexes.get(indexFields.getFirst()).put(indexFields.getSecond(), indexData); // In case a query doesn't specify a range key, we still want to select an index for this hash key. // If one has already been selected, pick one that projects over this entity to avoid extra DB reads. IndexData noRangeKeyIndexData = indexes.get(indexFields.getFirst()).get(null); if (noRangeKeyIndexData == null || !noRangeKeyIndexData.projectsOverEntity) { indexes.get(indexFields.getFirst()).put(null, indexData); } } indexes.put(hashKeyField, localIndexes); return indexes; } else { return Collections.singletonMap(hashKeyField, localIndexes); } }
From source file:org.xmlsh.aws.util.AWSDDBCommand.java
License:BSD License
private void writeLocalSecondaryIndex(LocalSecondaryIndexDescription index) throws XMLStreamException { startElement("local-secondary-index"); attribute("index-name", index.getIndexName()); attribute("index-size", index.getIndexSizeBytes()); attribute("item-count", index.getItemCount()); writeKeySchemaList(index.getKeySchema()); writeProjection(index.getProjection()); endElement();//from w w w .j a v a 2 s. c o m }