Example usage for org.apache.cassandra.db ConsistencyLevel LOCAL_ONE

List of usage examples for org.apache.cassandra.db ConsistencyLevel LOCAL_ONE

Introduction

In this page you can find the example usage for org.apache.cassandra.db ConsistencyLevel LOCAL_ONE.

Prototype

ConsistencyLevel LOCAL_ONE

To view the source code for org.apache.cassandra.db ConsistencyLevel LOCAL_ONE.

Click Source Link

Usage

From source file:kina.config.GenericConfigFactoryTest.java

License:Apache License

@Test
public void testValidation() {

    CassandraKinaConfig<TestEntity> djc = CassandraConfigFactory.create(TestEntity.class);

    djc.host(null).rpcPort(null).pageSize(0).bisectFactor(3);

    try {//from w  w  w.  j  av a  2  s. co  m
        djc.getKeyspace();
    } catch (IllegalAccessException e) {
        log.info("Correctly catched IllegalAccessException: " + e.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());

    }

    try {
        djc.getHost();
    } catch (IllegalAccessException e) {
        log.info("Correctly catched IllegalAccessException: " + e.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());

    }

    try {
        djc.getRpcPort();
    } catch (IllegalAccessException e) {
        log.info("Correctly catched IllegalAccessException: " + e.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());

    }

    try {
        djc.getUsername();
    } catch (IllegalAccessException e) {
        log.info("Correctly catched IllegalAccessException: " + e.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());

    }

    try {
        djc.getPassword();
    } catch (IllegalAccessException e) {
        log.info("Correctly catched IllegalAccessException: " + e.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());

    }

    try {
        djc.getColumnFamily();
    } catch (IllegalAccessException e) {
        log.info("Correctly catched IllegalAccessException: " + e.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());

    }

    try {
        djc.getPageSize();
    } catch (IllegalAccessException e) {
        log.info("Correctly catched IllegalAccessException: " + e.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());
    }

    try {
        djc.getPageSize();
    } catch (IllegalAccessException e) {
        log.info("Correctly catched IllegalAccessException: " + e.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());

    }

    try {
        djc.getEntityClass();
    } catch (IllegalAccessException e) {
        log.info("Correctly catched IllegalAccessException: " + e.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());
    }

    try {
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());
    }

    djc.host("localhost");

    try {
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());
    }

    djc.rpcPort(CassandraServer.CASSANDRA_THRIFT_PORT).cqlPort(CassandraServer.CASSANDRA_CQL_PORT);

    try {
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());
    }

    djc.keyspace(KEYSPACE_NAME);

    try {
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());
    }

    djc.columnFamily("test_page");

    try {
        djc.readConsistencyLevel("not valid CL");
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());
    }

    try {
        djc.pageSize(0);
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());
    }

    try {
        djc.pageSize(1 + Constants.DEFAULT_MAX_PAGE_SIZE);
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());

        djc.pageSize(10);
    } catch (Exception e) {
        fail(e.getMessage());
    }

    djc.readConsistencyLevel(ConsistencyLevel.LOCAL_ONE.name());

    try {
        djc.writeConsistencyLevel("not valid CL");
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());
    } catch (Exception e) {
        fail(e.getMessage());
    }

    djc.writeConsistencyLevel(ConsistencyLevel.LOCAL_ONE.name());

    try {
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());
        djc.columnFamily(COLUMN_FAMILY);
    } catch (Exception e) {
        fail(e.getMessage());
    }

    try {
        djc.initialize();
        fail();
    } catch (IllegalArgumentException iae) {
        // OK
        log.info("Correctly catched IllegalArgumentException: " + iae.getLocalizedMessage());
        djc.bisectFactor(4);
    } catch (Exception e) {
        fail(e.getMessage());
    }

    djc.initialize();
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

/**
 * Don't use QueryProcessor.executeInternal, we need to propagate this on
 * all nodes.//from w  w  w  .  j a  v  a2  s  .  com
 * 
 * @see org.elasticsearch.cassandra.ElasticSchemaService#createIndexKeyspace(java.lang.String,
 *      int)
 **/
@Override
public void createIndexKeyspace(final String ksname, final int replicationFactor) throws IOException {
    try {
        Keyspace ks = Keyspace.open(ksname);
        if (ks != null && !(ks.getReplicationStrategy() instanceof NetworkTopologyStrategy)) {
            throw new IOException(
                    "Cannot create index, underlying keyspace requires the NetworkTopologyStrategy.");
        }
    } catch (AssertionError | NullPointerException e) {
    }

    try {
        QueryProcessor.process(String.format((Locale) null,
                "CREATE KEYSPACE IF NOT EXISTS \"%s\" WITH replication = {'class':'NetworkTopologyStrategy', '%s':'%d' };",
                ksname, DatabaseDescriptor.getLocalDataCenter(), replicationFactor),
                ConsistencyLevel.LOCAL_ONE);
    } catch (Throwable e) {
        throw new IOException(e.getMessage(), e);
    }
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

@Override
public void dropIndexKeyspace(final String ksname) throws IOException {
    try {/*from  ww w  .  j av  a  2 s  .c  om*/
        String query = String.format((Locale) null, "DROP KEYSPACE IF EXISTS \"%s\"", ksname);
        logger.debug(query);
        QueryProcessor.process(query, ConsistencyLevel.LOCAL_ONE);
    } catch (Throwable e) {
        throw new IOException(e.getMessage(), e);
    }
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

public String buildUDT(final String ksName, final String cfName, final String name,
        final ObjectMapper objectMapper) throws RequestExecutionException {
    String typeName = cfName + "_" + objectMapper.fullPath().replace('.', '_');

    if (!objectMapper.iterator().hasNext()) {
        throw new InvalidRequestException("Cannot create an empty nested type (not supported)");
    }/*  w ww  .j a va  2  s .c om*/

    // create sub-type first
    for (Iterator<Mapper> it = objectMapper.iterator(); it.hasNext();) {
        Mapper mapper = it.next();
        if (mapper instanceof ObjectMapper) {
            buildCql(ksName, cfName, mapper.simpleName(), (ObjectMapper) mapper);
        } else if (mapper instanceof GeoPointFieldMapper) {
            buildGeoPointType(ksName);
        }
    }

    Pair<List<String>, List<String>> udt = getUDTInfo(ksName, typeName);
    if (udt == null) {
        // create new UDT.
        StringBuilder create = new StringBuilder(
                String.format((Locale) null, "CREATE TYPE IF NOT EXISTS \"%s\".\"%s\" ( ", ksName, typeName));
        boolean first = true;
        for (Iterator<Mapper> it = objectMapper.iterator(); it.hasNext();) {
            Mapper mapper = it.next();
            if (first)
                first = false;
            else
                create.append(", ");

            // Use only the last part of the fullname to build UDT.
            int lastDotIndex = mapper.name().lastIndexOf('.');
            String shortName = (lastDotIndex > 0) ? mapper.name().substring(lastDotIndex + 1) : mapper.name();

            if (isReservedKeyword(shortName))
                throw new ConfigurationException(shortName + " is a reserved keyword");
            create.append('\"').append(shortName).append("\" ");
            if (mapper instanceof ObjectMapper) {
                if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                    create.append(mapper.cqlCollectionTag()).append("<");
                create.append("frozen<").append(cfName).append('_')
                        .append(((ObjectMapper) mapper).fullPath().replace('.', '_')).append(">");
                if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                    create.append(">");
            } else if (mapper instanceof BaseGeoPointFieldMapper) {
                if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                    create.append(mapper.cqlCollectionTag()).append("<");
                create.append("frozen<").append(GEO_POINT_TYPE).append(">");
                if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                    create.append(">");
            } else if (mapper instanceof GeoShapeFieldMapper) {
                if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                    create.append(mapper.cqlCollectionTag()).append("<");
                create.append("frozen<").append("text").append(">");
                if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                    create.append(">");
            } else {
                String cqlType = mapperToCql.get(mapper.getClass());
                if (mapper.cqlCollection().equals(CqlCollection.SINGLETON)) {
                    create.append(cqlType);
                } else {
                    create.append(mapper.cqlCollectionTag()).append("<");
                    if (!isNativeCql3Type(cqlType))
                        create.append("frozen<");
                    create.append(cqlType);
                    if (!isNativeCql3Type(cqlType))
                        create.append(">");
                    create.append(">");
                }
            }
        }
        create.append(" )");
        if (logger.isDebugEnabled())
            logger.debug("create UDT:" + create.toString());

        QueryProcessor.process(create.toString(), ConsistencyLevel.LOCAL_ONE);
    } else {
        // update existing UDT
        for (Iterator<Mapper> it = objectMapper.iterator(); it.hasNext();) {
            Mapper mapper = it.next();
            int lastDotIndex = mapper.name().lastIndexOf('.');
            String shortName = (lastDotIndex > 0) ? mapper.name().substring(lastDotIndex + 1) : mapper.name();
            if (isReservedKeyword(shortName))
                throw new ConfigurationException(shortName + " is a reserved keyword");

            StringBuilder update = new StringBuilder(String.format((Locale) null,
                    "ALTER TYPE \"%s\".\"%s\" ADD \"%s\" ", ksName, typeName, shortName));
            if (!udt.left.contains(shortName)) {
                if (mapper instanceof ObjectMapper) {
                    if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                        update.append(mapper.cqlCollectionTag()).append("<");
                    update.append("frozen<").append(cfName).append('_')
                            .append(((ObjectMapper) mapper).fullPath().replace('.', '_')).append(">");
                    if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                        update.append(">");
                } else if (mapper instanceof GeoPointFieldMapper) {
                    if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                        update.append(mapper.cqlCollectionTag()).append("<");
                    update.append("frozen<").append(GEO_POINT_TYPE).append(">");
                    if (!mapper.cqlCollection().equals(CqlCollection.SINGLETON))
                        update.append(">");
                } else {
                    String cqlType = mapperToCql.get(mapper.getClass());
                    if (mapper.cqlCollection().equals(CqlCollection.SINGLETON)) {
                        update.append(cqlType);
                    } else {
                        update.append(mapper.cqlCollectionTag()).append("<");
                        if (!isNativeCql3Type(cqlType))
                            update.append("frozen<");
                        update.append(cqlType);
                        if (!isNativeCql3Type(cqlType))
                            update.append(">");
                        update.append(">");
                    }
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("update UDT: " + update.toString());
                }
                QueryProcessor.process(update.toString(), ConsistencyLevel.LOCAL_ONE);
            }
        }
    }
    return typeName;
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

private void buildGeoPointType(String ksName) throws RequestExecutionException {
    String query = String.format((Locale) null,
            "CREATE TYPE IF NOT EXISTS \"%s\".\"%s\" ( %s double, %s double)", ksName, GEO_POINT_TYPE,
            org.elasticsearch.index.mapper.geo.BaseGeoPointFieldMapper.Names.LAT,
            org.elasticsearch.index.mapper.geo.BaseGeoPointFieldMapper.Names.LON);
    QueryProcessor.process(query, ConsistencyLevel.LOCAL_ONE);
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

private void buildAttachementType(String ksName) throws RequestExecutionException {
    String query = String.format((Locale) null,
            "CREATE TYPE IF NOT EXISTS \"%s\".\"%s\" (context text, content_type text, content_length bigint, date timestamp, title text, author text, keywords text, language text)",
            ksName, ATTACHEMENT_TYPE);/* w ww  .  j a v a 2s.  c  o  m*/
    QueryProcessor.process(query, ConsistencyLevel.LOCAL_ONE);
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

private void buildCompletionType(String ksName) throws RequestExecutionException {
    String query = String.format((Locale) null,
            "CREATE TYPE IF NOT EXISTS \"%s\".\"%s\" (input list<text>, output text, weight bigint, payload text)",
            ksName, COMPLETION_TYPE);/*from  w  w  w. j  ava 2  s  .c  o m*/
    QueryProcessor.process(query, ConsistencyLevel.LOCAL_ONE);
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

@Override
public void updateTableSchema(final IndexService indexService, final MappingMetaData mappingMd)
        throws IOException {
    try {/*www .  jav a 2  s  .  c o m*/
        String ksName = indexService.keyspace();
        String cfName = InternalCassandraClusterService.typeToCfName(mappingMd.type());

        createIndexKeyspace(ksName, settings.getAsInt(SETTING_NUMBER_OF_REPLICAS, 0) + 1);

        CFMetaData cfm = Schema.instance.getCFMetaData(ksName, cfName);
        boolean newTable = (cfm == null);

        DocumentMapper docMapper = indexService.mapperService().documentMapper(mappingMd.type());
        Map<String, Object> mappingMap = mappingMd.sourceAsMap();

        Set<String> columns = new HashSet();
        if (docMapper.sourceMapper().enabled())
            columns.add(SourceFieldMapper.NAME);
        if (mappingMap.get("properties") != null)
            columns.addAll(((Map<String, Object>) mappingMap.get("properties")).keySet());

        logger.debug("Updating CQL3 schema {}.{} columns={}", ksName, cfName, columns);
        StringBuilder columnsList = new StringBuilder();
        Map<String, Boolean> columnsMap = new HashMap<String, Boolean>(columns.size());
        String[] primaryKeyList = new String[(newTable) ? columns.size() + 1
                : cfm.partitionKeyColumns().size() + cfm.clusteringColumns().size()];
        int primaryKeyLength = 0;
        int partitionKeyLength = 0;
        for (String column : columns) {
            if (isReservedKeyword(column))
                throw new ConfigurationException(column + " is a CQL reserved keyword");

            if (column.equals(TokenFieldMapper.NAME))
                continue; // ignore pseudo column known by Elasticsearch

            if (columnsList.length() > 0)
                columnsList.append(',');

            String cqlType = null;
            String cqlNativeType = null;
            boolean isStatic = false;
            FieldMapper fieldMapper = docMapper.mappers().smartNameFieldMapper(column);
            if (fieldMapper != null) {
                if (fieldMapper instanceof GeoPointFieldMapper
                        || fieldMapper instanceof GeoPointFieldMapperLegacy) {
                    cqlType = GEO_POINT_TYPE;
                    buildGeoPointType(ksName);
                } else if (fieldMapper instanceof GeoShapeFieldMapper) {
                    cqlType = "text";
                } else if (fieldMapper instanceof CompletionFieldMapper) {
                    cqlType = COMPLETION_TYPE;
                    buildCompletionType(ksName);
                } else if (fieldMapper.getClass().getName()
                        .equals("org.elasticsearch.mapper.attachments.AttachmentMapper")) {
                    // attachement is a plugin, so class may not found.
                    cqlType = ATTACHEMENT_TYPE;
                    buildAttachementType(ksName);
                } else if (fieldMapper instanceof SourceFieldMapper) {
                    cqlType = "blob";
                } else {
                    cqlType = mapperToCql.get(fieldMapper.getClass());
                    if (cqlType == null) {
                        logger.warn("Ignoring field [{}] type [{}]", column, fieldMapper.name());
                        continue;
                    }
                }

                columnsMap.put(column, fieldMapper.cqlPartialUpdate());
                if (fieldMapper.cqlPrimaryKeyOrder() >= 0) {
                    if (fieldMapper.cqlPrimaryKeyOrder() < primaryKeyList.length
                            && primaryKeyList[fieldMapper.cqlPrimaryKeyOrder()] == null) {
                        primaryKeyList[fieldMapper.cqlPrimaryKeyOrder()] = column;
                        primaryKeyLength = Math.max(primaryKeyLength, fieldMapper.cqlPrimaryKeyOrder() + 1);
                        if (fieldMapper.cqlPartitionKey()) {
                            partitionKeyLength++;
                        }
                    } else {
                        throw new Exception("Wrong primary key order for column " + column);
                    }
                }

                if (!isNativeCql3Type(cqlType)) {
                    cqlType = "frozen<" + cqlType + ">";
                }
                if (!fieldMapper.cqlCollection().equals(CqlCollection.SINGLETON)) {
                    cqlType = fieldMapper.cqlCollectionTag() + "<" + cqlType + ">";
                }
                isStatic = fieldMapper.cqlStaticColumn();
            } else {
                ObjectMapper objectMapper = docMapper.objectMappers().get(column);
                if (objectMapper == null) {
                    logger.warn("Cannot infer CQL type from object mapping for field [{}]", column);
                    continue;
                }
                columnsMap.put(column, objectMapper.cqlPartialUpdate());
                if (objectMapper.cqlPrimaryKeyOrder() >= 0) {
                    if (objectMapper.cqlPrimaryKeyOrder() < primaryKeyList.length
                            && primaryKeyList[objectMapper.cqlPrimaryKeyOrder()] == null) {
                        primaryKeyList[objectMapper.cqlPrimaryKeyOrder()] = column;
                        primaryKeyLength = Math.max(primaryKeyLength, objectMapper.cqlPrimaryKeyOrder() + 1);
                        if (objectMapper.cqlPartitionKey()) {
                            partitionKeyLength++;
                        }
                    } else {
                        throw new Exception("Wrong primary key order for column " + column);
                    }
                }
                if (objectMapper.cqlStruct().equals(CqlStruct.MAP)) {
                    // TODO: check columnName exists and is map<text,?>
                    cqlType = buildCql(ksName, cfName, column, objectMapper);
                    if (!objectMapper.cqlCollection().equals(CqlCollection.SINGLETON)) {
                        cqlType = objectMapper.cqlCollectionTag() + "<" + cqlType + ">";
                    }
                    //logger.debug("Expecting column [{}] to be a map<text,?>", column);
                } else if (objectMapper.cqlStruct().equals(CqlStruct.UDT)) {
                    if (!objectMapper.iterator().hasNext()) {
                        // opaque json object
                        cqlType = "text";
                    } else {
                        // Cassandra 2.1.8 : Non-frozen collections are not allowed inside collections
                        cqlType = "frozen<\"" + buildCql(ksName, cfName, column, objectMapper) + "\">";
                    }
                    if (!objectMapper.cqlCollection().equals(CqlCollection.SINGLETON)
                            && !(cfName.equals(PERCOLATOR_TABLE) && column.equals("query"))) {
                        cqlType = objectMapper.cqlCollectionTag() + "<" + cqlType + ">";
                    }
                }
                isStatic = objectMapper.cqlStaticColumn();
            }

            if (newTable) {
                if (cqlType != null) {
                    columnsList.append("\"").append(column).append("\" ").append(cqlType);
                    if (isStatic)
                        columnsList.append(" static");
                }
            } else {
                ColumnDefinition cdef = cfm.getColumnDefinition(new ColumnIdentifier(column, true));
                if (cqlType != null) {
                    if (cdef == null) {
                        for (int i = 0; i < primaryKeyLength; i++) {
                            if (primaryKeyList[i].equals(column))
                                throw new Exception("Cannot alter primary key of an existing table");
                        }
                        try {
                            String query = String.format((Locale) null,
                                    "ALTER TABLE \"%s\".\"%s\" ADD \"%s\" %s %s", ksName, cfName, column,
                                    cqlType, (isStatic) ? "static" : "");
                            logger.debug(query);
                            QueryProcessor.process(query, ConsistencyLevel.LOCAL_ONE);
                        } catch (Exception e) {
                            logger.warn("Cannot alter table {}.{} column {} with type {}", e, ksName, cfName,
                                    column, cqlType);
                        }
                    } else {
                        // check that the existing column matches the provided mapping
                        // TODO: do this check for collection
                        String existingCqlType = cdef.type.asCQL3Type().toString();
                        if (!cdef.type.isCollection()) {
                            // cdef.type.asCQL3Type() does not include frozen, nor quote, so can do this check for collection.
                            if (!existingCqlType.equals(cqlType)
                                    && !cqlType.equals("frozen<" + existingCqlType + ">")) {
                                throw new IOException(
                                        "Existing column " + column + " mismatch type " + cqlType);
                            }
                        }
                    }
                }
            }

        }

        // add _parent column if necessary. Parent and child documents should have the same partition key.
        if (docMapper.parentFieldMapper().active() && docMapper.parentFieldMapper().pkColumns() == null) {
            if (newTable) {
                // _parent is a JSON array representation of the parent PK.
                if (columnsList.length() > 0)
                    columnsList.append(", ");
                columnsList.append("\"_parent\" text");
            } else {
                try {
                    String query = String.format((Locale) null,
                            "ALTER TABLE \"%s\".\"%s\" ADD \"_parent\" text", ksName, cfName);
                    logger.debug(query);
                    QueryProcessor.process(query, ConsistencyLevel.LOCAL_ONE);
                } catch (Exception e) {
                    logger.warn("Cannot alter table {}.{} column _parent with type text", e, ksName, cfName);
                }
            }
        }

        if (newTable) {
            if (partitionKeyLength == 0) {
                // build a default primary key _id text
                if (columnsList.length() > 0)
                    columnsList.append(',');
                columnsList.append("\"").append(ELASTIC_ID_COLUMN_NAME).append("\" text");
                primaryKeyList[0] = ELASTIC_ID_COLUMN_NAME;
                primaryKeyLength = 1;
                partitionKeyLength = 1;
            }
            // build the primary key definition
            StringBuilder primaryKey = new StringBuilder();
            primaryKey.append("(");
            for (int i = 0; i < primaryKeyLength; i++) {
                if (primaryKeyList[i] == null)
                    throw new Exception("Incomplet primary key definition at index " + i);
                primaryKey.append("\"").append(primaryKeyList[i]).append("\"");
                if (i == partitionKeyLength - 1)
                    primaryKey.append(")");
                if (i + 1 < primaryKeyLength)
                    primaryKey.append(",");
            }
            String query = String.format((Locale) null,
                    "CREATE TABLE IF NOT EXISTS \"%s\".\"%s\" ( %s, PRIMARY KEY (%s) ) WITH COMMENT='Auto-created by Elassandra'",
                    ksName, cfName, columnsList.toString(), primaryKey.toString());
            logger.debug(query);
            QueryProcessor.process(query, ConsistencyLevel.LOCAL_ONE);
        }

        updateMapping(indexService.index().name(), mappingMd);

    } catch (Throwable e) {
        throw new IOException(e.getMessage(), e);
    }
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

public void createSecondaryIndex(String ksName, MappingMetaData mapping, String className) throws IOException {
    final String cfName = typeToCfName(mapping.type());
    final CFMetaData cfm = Schema.instance.getCFMetaData(ksName, cfName);
    if (cfm != null) {
        String query = null;/*from  w w  w .  java2s  .  c  o  m*/
        try {
            Map<String, Object> mappingProperties = (Map<String, Object>) mapping.sourceAsMap()
                    .get("properties");
            if (mappingProperties != null) {
                for (Map.Entry<String, Object> entry : mappingProperties.entrySet()) {
                    String column = entry.getKey();
                    if (column.startsWith("_")) {
                        continue; // ignore pseudo column known by Elasticsearch
                    }
                    if ("no".equals(((Map<String, Object>) entry.getValue()).get("index"))) {
                        continue; // ignore field with index:no
                    }
                    ColumnDefinition cdef = (cfm == null) ? null
                            : cfm.getColumnDefinition(new ColumnIdentifier(column, true));
                    if ((cdef != null) && !cdef.isIndexed() && !(cfm.partitionKeyColumns().size() == 1
                            && cdef.kind == ColumnDefinition.Kind.PARTITION_KEY)) {
                        query = String.format((Locale) null,
                                "CREATE CUSTOM INDEX IF NOT EXISTS \"%s\" ON \"%s\".\"%s\" (\"%s\") USING '%s'",
                                buildIndexName(cfName, column), ksName, cfName, column, className);
                        logger.debug(query);
                        QueryProcessor.process(query, ConsistencyLevel.LOCAL_ONE);
                    }
                }
            }
        } catch (Throwable e) {
            throw new IOException("Failed to process query=[" + query + "]:" + e.getMessage(), e);
        }
    } else {
        logger.warn("Cannot create SECONDARY INDEX, [{}.{}] does not exist", ksName, cfName);
    }
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

@Override
public void dropSecondaryIndex(String ksName, String cfName) throws RequestExecutionException {
    CFMetaData cfm = Schema.instance.getCFMetaData(ksName, cfName);
    if (cfm != null) {
        for (ColumnDefinition cdef : Iterables.concat(cfm.partitionKeyColumns(), cfm.clusteringColumns(),
                cfm.regularColumns())) {
            if (cdef.isIndexed() && cdef.getIndexOptions().get(SecondaryIndex.CUSTOM_INDEX_OPTION_NAME)
                    .endsWith("ElasticSecondaryIndex")) {
                logger.debug("DROP INDEX IF EXISTS {}.{}", ksName,
                        buildIndexName(cfName, cdef.name.toString()));
                QueryProcessor.process(String.format((Locale) null, "DROP INDEX IF EXISTS \"%s\".\"%s\"",
                        ksName, buildIndexName(cfName, cdef.name.toString())), ConsistencyLevel.LOCAL_ONE);
            }//from  ww w.  ja  v a2  s .c  o m
        }
    }
}