You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@metamodel.apache.org by kaspersorensen <gi...@git.apache.org> on 2015/05/04 09:22:38 UTC

[GitHub] metamodel pull request: Fresh PR for Sor

Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/19#discussion_r29569223
  
    --- Diff: solr/src/main/java/org/apache/metamodel/solr/SolrDataContext.java ---
    @@ -0,0 +1,403 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.metamodel.solr;
    +
    +import java.io.InputStreamReader;
    +import java.io.IOException;
    +import java.lang.reflect.Method;
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.Comparator;
    +import java.util.HashMap;
    +import java.util.Iterator;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.apache.http.HttpResponse;
    +import org.apache.http.client.HttpClient;
    +import org.apache.http.client.methods.HttpGet;
    +import org.apache.http.impl.client.DefaultHttpClient;
    +import org.apache.metamodel.DataContext;
    +import org.apache.metamodel.MetaModelException;
    +import org.apache.metamodel.query.Query;
    +import org.apache.metamodel.QueryPostprocessDataContext;
    +import org.apache.metamodel.data.DataSet;
    +import org.apache.metamodel.data.DataSetHeader;
    +import org.apache.metamodel.data.Row;
    +import org.apache.metamodel.data.SimpleDataSetHeader;
    +import org.apache.metamodel.query.FilterClause;
    +import org.apache.metamodel.query.FilterItem;
    +import org.apache.metamodel.query.GroupByItem;
    +import org.apache.metamodel.query.OrderByItem;
    +import org.apache.metamodel.query.SelectItem;
    +import org.apache.metamodel.query.LogicalOperator;
    +import org.apache.metamodel.query.OperatorType;
    +import org.apache.metamodel.query.FunctionType;
    +import org.apache.metamodel.schema.Column;
    +import org.apache.metamodel.schema.ColumnType;
    +import org.apache.metamodel.schema.MutableColumn;
    +import org.apache.metamodel.schema.MutableSchema;
    +import org.apache.metamodel.schema.MutableTable;
    +import org.apache.metamodel.schema.Schema;
    +import org.apache.metamodel.schema.Table;
    +import org.apache.metamodel.util.SimpleTableDef;
    +import org.apache.solr.client.solrj.SolrQuery;
    +import org.apache.solr.client.solrj.SolrQuery.ORDER;
    +import org.apache.solr.client.solrj.SolrServerException;
    +import org.apache.solr.client.solrj.impl.HttpSolrServer;
    +import org.apache.solr.client.solrj.response.QueryResponse;
    +import org.apache.solr.client.solrj.response.FacetField;
    +import org.apache.solr.common.SolrDocumentList;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.fasterxml.jackson.databind.ObjectMapper;
    +import com.fasterxml.jackson.databind.ObjectReader;
    +
    +/**
    + * DataContext implementation for ElasticSearch analytics engine.
    + * 
    + * ElasticSearch has a data storage structure hierarchy that briefly goes like
    + * this:
    + * <ul>
    + * <li>Index</li>
    + * <li>Document type (short: Type) (within an index)</li>
    + * <li>Documents (of a particular type)</li>
    + * </ul>
    + * 
    + * When instantiating this DataContext, an index name is provided. Within this
    + * index, each document type is represented as a table.
    + * 
    + * This implementation supports either automatic discovery of a schema or manual
    + * specification of a schema, through the {@link SimpleTableDef} class.
    + */
    +
    +public class SolrDataContext extends QueryPostprocessDataContext implements
    +        DataContext {
    +
    +    private static final Logger logger = LoggerFactory
    +            .getLogger(SolrDataContext.class);
    +
    +    public static final String FIELD_ID = "id";
    +
    +    private final SimpleTableDef tableDef;
    +    private final String indexName;
    +    private final String url;
    +
    +    private SolrQuery query = new SolrQuery();
    +
    +    private static enum queryTypes {
    +        ROW, FACET
    +    };
    +
    +    private static final int MAX_RETRIES = 0;
    +    private static final int SOCK_TIMEOUT = 1000;
    +    private static final int CONN_TIMEOUT = 5000;
    +    private static final int DEFAULT_LIMIT = 1000;
    +
    +    public SolrDataContext(String url, String indexName) {
    +        this.url = url;
    +        this.indexName = indexName;
    +        this.tableDef = detectSchema(url, indexName);
    +    }
    +
    +    private SimpleTableDef detectSchema(String url, String indexName) {
    +        SimpleTableDef tableDef = null;
    +
    +        url += "/schema";
    +
    +        try {
    +            HttpClient httpclient = new DefaultHttpClient();
    +            HttpGet request = new HttpGet(url);
    +            HttpResponse response = httpclient.execute(request);
    +
    +            InputStreamReader inputStream = new InputStreamReader(response
    +                    .getEntity().getContent());
    +
    +            ObjectReader reader = new ObjectMapper().reader(HashMap.class);
    +            Map<String, Object> outerMap = reader.readValue(inputStream);
    +
    +            Map<String, Object> schemaMap = null;
    +
    +            if (outerMap != null) {
    +                schemaMap = (Map) outerMap.get("schema");
    +            }
    +
    +            if (schemaMap != null) {
    +                List<Map<String, String>> fieldMapList = (List) schemaMap
    +                        .get("fields");
    +                List<String> columnNames = new ArrayList<String>();
    +                List<ColumnType> columnTypes = new ArrayList<ColumnType>();
    +
    +                for (int i = 0; i < fieldMapList.size(); i++) {
    +                    Map<String, String> fieldMap = fieldMapList.get(i);
    +
    +                    columnNames.add(fieldMap.get("name"));
    +                    columnTypes.add(ColumnType.STRING);
    +                }
    +
    +                String[] columnNamesArr = new String[columnNames.size()];
    +                columnNamesArr = columnNames.toArray(columnNamesArr);
    +
    +                ColumnType[] columnTypesArr = new ColumnType[columnTypes.size()];
    +                columnTypesArr = columnTypes.toArray(columnTypesArr);
    +
    +                tableDef = new SimpleTableDef(indexName, columnNamesArr,
    +                        columnTypesArr);
    +            }
    +        } catch (IOException e) {
    +            logger.error("Failed to parse schema", "", e);
    +        }
    +
    +        return tableDef;
    +    }
    +
    +    @Override
    +    protected Schema getMainSchema() throws MetaModelException {
    +        try {
    +            final MutableSchema theSchema = new MutableSchema(
    +                    getMainSchemaName());
    +
    +            final MutableTable table = tableDef.toTable().setSchema(theSchema);
    +            final Column idColumn = table.getColumnByName(FIELD_ID);
    +            if (idColumn != null && idColumn instanceof MutableColumn) {
    +                final MutableColumn mutableColumn = (MutableColumn) idColumn;
    +                mutableColumn.setPrimaryKey(true);
    +            }
    +            theSchema.addTable(table);
    +
    +            return theSchema;
    +        } catch (Exception e) {
    +            throw new MetaModelException("Schema retrieval failed " + e);
    +        }
    +    }
    +
    +    @Override
    +    protected Number executeCountQuery(Table table,
    +            List<FilterItem> whereItems, boolean functionApproximationAllowed) {
    +        String queryStr = "*:*";
    +
    +        if (whereItems != null) {
    +            for (int i = 0; i < whereItems.size(); i++) {
    +                FilterItem whereItem = whereItems.get(i);
    +                queryStr = whereItem.toString();
    +            }
    +        }
    +
    +        QueryResponse response = selectRows(table, null, queryStr, 0, 0);
    +        return response.getResults().getNumFound();
    +    }
    +
    +    @Override
    +    public DataSet executeQuery(Query q) throws MetaModelException {
    --- End diff --
    
    The SQL interface works well if you do not override this method at all. The method is already implemented in the superclass and will delegate to some of the protected methods that you can choose to override. It is also possible to override this method, but it comes at a much greater risk becase the Query object is so multi-facetted that your code really has to take a ton of scenarios into account. I'll try and make line-notes below so you can see...


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---