Example usage for org.apache.cassandra.cql3 QueryProcessor executeInternal

List of usage examples for org.apache.cassandra.cql3 QueryProcessor executeInternal

Introduction

In this page you can find the example usage for org.apache.cassandra.cql3 QueryProcessor executeInternal.

Prototype

public static UntypedResultSet executeInternal(String query, Object... values) 

Source Link

Usage

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

License:Apache License

public Pair<List<String>, List<String>> getUDTInfo(final String ksName, final String typeName) {
    try {//from w w  w  .j  ava 2 s . c  om
        UntypedResultSet result = QueryProcessor.executeInternal(
                "SELECT field_names, field_types FROM system.schema_usertypes WHERE keyspace_name = ? AND type_name = ?",
                new Object[] { ksName, typeName });
        Row row = result.one();
        if ((row != null) && row.has("field_names")) {
            List<String> field_names = row.getList("field_names", UTF8Type.instance);
            List<String> field_types = row.getList("field_types", UTF8Type.instance);
            return Pair.<List<String>, List<String>>create(field_names, field_types);
        }
    } catch (Exception e) {
    }
    return null;
}

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

License:Apache License

public static MapType<?, ?> getMapType(final String ksName, final String cfName, final String colName) {
    try {/* ww  w . j  a  va 2s  .c  o  m*/
        UntypedResultSet result = QueryProcessor.executeInternal(
                "SELECT validator FROM system.schema_columns WHERE keyspace_name = ? AND columnfamily_name = ? AND column_name = ?",
                new Object[] { ksName, cfName, colName });
        Row row = result.one();
        if ((row != null) && row.has("validator")) {
            AbstractType<?> type = TypeParser.parse(row.getString("validator"));
            if (type instanceof MapType) {
                return (MapType<?, ?>) type;
            }
        }
    } catch (Exception e) {

    }
    return null;
}

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

License:Apache License

@Override
public UntypedResultSet fetchRowInternal(final String ksName, final String index, final String cfName,
        final String[] columns, final Object[] pkColumns, boolean forStaticDocument)
        throws ConfigurationException, IOException, IndexNotFoundException {
    return QueryProcessor.executeInternal(buildFetchQuery(ksName, index, cfName, columns, forStaticDocument),
            pkColumns);//from  ww w . j  a va2  s.  co m
}

From source file:org.elasticsearch.cassandra.cluster.InternalCassandraClusterService.java

License:Apache License

public MapType<?, ?> getMapType(final String ksName, final String cfName, final String colName) {
    try {/*from  ww w. j a  v a2s. co  m*/
        UntypedResultSet result = QueryProcessor.executeInternal(
                "SELECT validator FROM system.schema_columns WHERE columnfamily_name = ? AND column_name = ?",
                new Object[] { ksName, cfName, colName });
        Row row = result.one();
        if ((row != null) && row.has("validator")) {
            AbstractType<?> type = TypeParser.parse(row.getString("validator"));
            if (type instanceof MapType) {
                return (MapType<?, ?>) type;
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:org.elasticsearch.cassandra.cluster.InternalCassandraClusterService.java

License:Apache License

public Map<String, Object> expandTableMapping(final String ksName, final String cfName,
        Map<String, Object> mapping) throws IOException, SyntaxException, ConfigurationException {
    final String columnRegexp = (String) mapping.get("columns_regexp");
    if (columnRegexp != null) {
        mapping.remove("columns_regexp");
        Pattern pattern = Pattern.compile(columnRegexp);
        Map<String, Object> properties = (Map) mapping.get("properties");
        if (properties == null) {
            properties = Maps.newHashMap();
            mapping.put("properties", properties);
        }/*  ww  w . j  av a2 s  .c  o m*/
        try {
            UntypedResultSet result = QueryProcessor.executeInternal(
                    "SELECT  column_name,validator FROM system.schema_columns WHERE keyspace_name=? and columnfamily_name=?",
                    new Object[] { ksName, cfName });
            for (Row row : result) {
                if (row.has("validator") && pattern.matcher(row.getString("column_name")).matches()
                        && !row.getString("column_name").startsWith("_")) {
                    String columnName = row.getString("column_name");
                    Map<String, Object> props = (Map<String, Object>) properties.get(columnName);
                    if (props == null) {
                        props = Maps.newHashMap();
                        properties.put(columnName, props);
                    }
                    AbstractType<?> type = TypeParser.parse(row.getString("validator"));
                    buildTypeMapping(props, type);
                }
            }
            if (logger.isDebugEnabled())
                logger.debug("mapping {} : {}", cfName, mapping);
            return mapping;
        } catch (IOException | SyntaxException | ConfigurationException e) {
            logger.warn("Failed to build elasticsearch mapping " + ksName + "." + cfName, e);
            throw e;
        }
    }
    return mapping;
}

From source file:org.elasticsearch.index.mapper.MapperService.java

License:Apache License

public Map<String, Object> discoverTableMapping(final String ksName, final String type,
        Map<String, Object> mapping) throws IOException, SyntaxException, ConfigurationException {
    final String columnRegexp = (String) mapping.get(DISCOVER);
    final String cfName = InternalCassandraClusterService.typeToCfName(type);
    if (columnRegexp != null) {
        mapping.remove(DISCOVER);//from  ww w .  jav  a 2s  . c  om
        Pattern pattern = Pattern.compile(columnRegexp);
        Map<String, Object> properties = (Map) mapping.get("properties");
        if (properties == null) {
            properties = Maps.newHashMap();
            mapping.put("properties", properties);
        }
        try {
            CFMetaData metadata = InternalCassandraClusterService.getCFMetaData(ksName, cfName);
            List<String> pkColNames = new ArrayList<String>(
                    metadata.partitionKeyColumns().size() + metadata.clusteringColumns().size());
            for (ColumnDefinition cd : Iterables.concat(metadata.partitionKeyColumns(),
                    metadata.clusteringColumns())) {
                pkColNames.add(cd.name.toString());
            }

            UntypedResultSet result = QueryProcessor.executeInternal(
                    "SELECT column_name, validator FROM system.schema_columns WHERE keyspace_name=? and columnfamily_name=?",
                    new Object[] { ksName, cfName });
            for (Row row : result) {
                if (row.has("validator") && pattern.matcher(row.getString("column_name")).matches()
                        && !row.getString("column_name").startsWith("_")) {
                    String columnName = row.getString("column_name");
                    Map<String, Object> props = (Map<String, Object>) properties.get(columnName);
                    if (props == null) {
                        props = Maps.newHashMap();
                        properties.put(columnName, props);
                    }
                    int pkOrder = pkColNames.indexOf(columnName);
                    if (pkOrder >= 0) {
                        props.put(TypeParsers.CQL_PRIMARY_KEY_ORDER, pkOrder);
                        if (pkOrder < metadata.partitionKeyColumns().size()) {
                            props.put(TypeParsers.CQL_PARTITION_KEY, true);
                        }
                    }
                    if (metadata.getColumnDefinition(new ColumnIdentifier(columnName, true)).isStatic()) {
                        props.put(TypeParsers.CQL_STATIC_COLUMN, true);
                    }
                    AbstractType<?> atype = TypeParser.parse(row.getString("validator"));
                    buildCollectionMapping(props, atype);
                }
            }
            if (logger.isDebugEnabled())
                logger.debug("mapping {} : {}", cfName, mapping);
            return mapping;
        } catch (IOException | SyntaxException | ConfigurationException e) {
            logger.warn("Failed to build elasticsearch mapping " + ksName + "." + cfName, e);
            throw e;
        }
    }
    return mapping;
}

From source file:org.elasticsearch.search.fetch.FetchPhase.java

License:Apache License

private void loadStoredFields(SearchContext searchContext, LeafReaderContext readerContext,
        FieldsVisitor fieldVisitor, int docId) {
    fieldVisitor.reset();/*w  w  w  .j a  v a2  s . c  om*/
    try {
        readerContext.reader().document(docId, fieldVisitor);
    } catch (IOException e) {
        throw new FetchPhaseExecutionException(searchContext, "Failed to fetch doc id [" + docId + "]", e);
    }

    // load field from cassandra
    if (!(fieldVisitor instanceof JustUidFieldsVisitor)) {
        try {
            DocPrimaryKey docPk = clusterService.parseElasticId(searchContext.request().index(),
                    fieldVisitor.uid().type(), fieldVisitor.uid().id());
            String typeKey = fieldVisitor.uid().type();
            if (docPk.isStaticDocument)
                typeKey += "_static";

            String cqlQuery = searchContext.getCqlFetchQuery(typeKey);
            if (cqlQuery == null) {
                Set<String> requiredColumns = fieldVisitor.requiredColumns(clusterService, searchContext);
                if (requiredColumns.size() > 0) {
                    IndexMetaData indexMetaData = clusterService.state().metaData()
                            .index(searchContext.request().index());
                    if (requiredColumns.contains(NodeFieldMapper.NAME)) {
                        searchContext.includeNode(indexMetaData.getSettings().getAsBoolean(
                                IndexMetaData.SETTING_INCLUDE_NODE_ID,
                                clusterService.settings().getAsBoolean(
                                        InternalCassandraClusterService.SETTING_CLUSTER_DEFAULT_INCLUDE_NODE_ID,
                                        false)));
                        requiredColumns.remove(NodeFieldMapper.NAME);
                    }
                    if (fieldVisitor.loadSource() && searchContext.mapperService()
                            .documentMapper(fieldVisitor.uid().type()).sourceMapper().enabled()) {
                        requiredColumns.add(SourceFieldMapper.NAME);
                    }
                    if (requiredColumns.size() > 0) {
                        cqlQuery = clusterService.buildFetchQuery(indexMetaData.keyspace(),
                                searchContext.request().index(), fieldVisitor.uid().type(),
                                requiredColumns.toArray(new String[requiredColumns.size()]),
                                docPk.isStaticDocument);
                        searchContext.putFetchQuery(typeKey, cqlQuery);
                    }
                }
            }

            if (cqlQuery != null) {
                UntypedResultSet result = QueryProcessor.executeInternal(cqlQuery, docPk.values);
                if (!result.isEmpty()) {
                    Map<String, Object> mapObject = clusterService.rowAsMap(searchContext.request().index(),
                            fieldVisitor.uid().type(), result.one());
                    if (searchContext.includeNode()) {
                        mapObject.put(NodeFieldMapper.NAME, clusterService.state().nodes().localNodeId());
                    }
                    if (fieldVisitor.requestedFields() != null && fieldVisitor.requestedFields().size() > 0) {
                        Map<String, List<Object>> flatMap = new HashMap<String, List<Object>>();
                        clusterService.flattenTree(fieldVisitor.requestedFields(), "", mapObject, flatMap);
                        for (String field : fieldVisitor.requestedFields()) {
                            if (flatMap.get(field) != null && field != IdFieldMapper.NAME)
                                fieldVisitor.setValues(field, flatMap.get(field));
                        }
                    }
                    if (fieldVisitor.loadSource()) {
                        fieldVisitor.source(clusterService.source(
                                searchContext.mapperService().documentMapper(fieldVisitor.uid().type()),
                                mapObject, searchContext.request().index(), fieldVisitor.uid()));
                    }
                }
            } else {
                // when only requesting for field _node
                if (searchContext.includeNode()) {
                    List<Object> values = new ArrayList<Object>(1);
                    values.add(clusterService.state().nodes().localNodeId());
                    fieldVisitor.setValues(NodeFieldMapper.NAME, values);
                }
            }
        } catch (Exception e) {
            Logger.getLogger(FetchPhase.class).error("Fetch failed id=" + fieldVisitor.uid().id(), e);
            throw new FetchPhaseExecutionException(searchContext,
                    "Failed to fetch doc id [" + fieldVisitor.uid().id() + "] from cassandra", e);
        }
    }
}