Search in sources :

Example 1 with SolrDocumentCallback

use of org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback in project teiid by teiid.

the class SolrUpdateExecution method performInsert.

private void performInsert(Insert insert) throws TranslatorException {
    // build insert
    List<ColumnReference> columns = insert.getColumns();
    if (insert.getParameterValues() == null) {
        final UpdateRequest request = new UpdateRequest();
        SolrInputDocument doc = new SolrInputDocument();
        List<Expression> values = ((ExpressionValueSource) insert.getValueSource()).getValues();
        for (int i = 0; i < columns.size(); i++) {
            String columnName = SolrSQLHierarchyVistor.getColumnName(columns.get(i));
            Object value = values.get(i);
            if (value instanceof Literal) {
                doc.addField(columnName, ((Literal) value).getValue());
            } else {
                throw new TranslatorException(SolrPlugin.Event.TEIID20002, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20002));
            }
        }
        this.updateCount++;
        request.add(doc);
        // check if the row already exists
        Select q = buildSelectQuery(insert);
        SolrQueryExecution query = new SolrQueryExecution(ef, q, this.executionContext, this.metadata, this.connection);
        query.execute();
        query.walkDocuments(new SolrDocumentCallback() {

            @Override
            public void walk(SolrDocument doc) {
                request.clear();
            }
        });
        if (request.getDocuments().isEmpty()) {
            throw new TranslatorException(SolrPlugin.Event.TEIID20007, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20007));
        }
        // write the mutation
        UpdateResponse response = this.connection.update(request);
        if (response.getStatus() != 0) {
            throw new TranslatorException(SolrPlugin.Event.TEIID20003, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20003, response.getStatus()));
        }
    } else {
        UpdateRequest request = new UpdateRequest();
        int batchSize = 1024;
        // bulk insert; should help
        Iterator<? extends List<?>> args = insert.getParameterValues();
        while (args.hasNext()) {
            List<?> arg = args.next();
            SolrInputDocument doc = new SolrInputDocument();
            for (int i = 0; i < columns.size(); i++) {
                String columnName = SolrSQLHierarchyVistor.getColumnName(columns.get(i));
                doc.addField(columnName, arg.get(i));
            }
            this.updateCount++;
            request.add(doc);
            if ((this.updateCount % batchSize) == 0) {
                UpdateResponse response = this.connection.update(request);
                if (response.getStatus() != 0) {
                    throw new TranslatorException(SolrPlugin.Event.TEIID20003, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20003, response.getStatus()));
                }
                request = new UpdateRequest();
            }
        }
        if (request.getDocuments() != null && !request.getDocuments().isEmpty()) {
            // write the mutation
            UpdateResponse response = this.connection.update(request);
            if (response.getStatus() != 0) {
                throw new TranslatorException(SolrPlugin.Event.TEIID20003, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20003, response.getStatus()));
            }
        }
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) SolrDocumentCallback(org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback) TranslatorException(org.teiid.translator.TranslatorException)

Example 2 with SolrDocumentCallback

use of org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback in project teiid by teiid.

the class SolrUpdateExecution method performUpdate.

private void performUpdate(Delete obj) throws TranslatorException {
    Table table = obj.getTable().getMetadataObject();
    KeyRecord pk = table.getPrimaryKey();
    final String id = getRecordName(pk.getColumns().get(0));
    if (obj.getParameterValues() != null) {
        throw new TranslatorException(SolrPlugin.Event.TEIID20008, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20008));
    }
    SolrQueryExecution query = new SolrQueryExecution(ef, obj, this.executionContext, this.metadata, this.connection);
    query.execute();
    final UpdateRequest request = new UpdateRequest();
    query.walkDocuments(new SolrDocumentCallback() {

        @Override
        public void walk(SolrDocument doc) {
            SolrUpdateExecution.this.updateCount++;
            request.deleteById(doc.getFieldValue(id).toString());
        }
    });
    UpdateResponse response = this.connection.update(request);
    if (response.getStatus() != 0) {
        throw new TranslatorException(SolrPlugin.Event.TEIID20005, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20005, response.getStatus()));
    }
}
Also used : KeyRecord(org.teiid.metadata.KeyRecord) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) Table(org.teiid.metadata.Table) SolrDocument(org.apache.solr.common.SolrDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrDocumentCallback(org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback) TranslatorException(org.teiid.translator.TranslatorException)

Example 3 with SolrDocumentCallback

use of org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback in project teiid by teiid.

the class SolrUpdateExecution method performUpdate.

/**
 * Did not find any other suitable way to pass the query through solrj otherthan walking the documents,
 * all the examples were at the passing xml based query. so that would be a good update if the below does
 * not performs or gets into OOM
 * @param obj
 * @throws TranslatorException
 */
private void performUpdate(final Update obj) throws TranslatorException {
    if (obj.getParameterValues() != null) {
        throw new TranslatorException(SolrPlugin.Event.TEIID20009, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20009));
    }
    SolrQueryExecution query = new SolrQueryExecution(ef, obj, this.executionContext, this.metadata, this.connection);
    query.execute();
    final UpdateRequest request = new UpdateRequest();
    query.walkDocuments(new SolrDocumentCallback() {

        @Override
        public void walk(SolrDocument doc) {
            SolrUpdateExecution.this.updateCount++;
            Table table = obj.getTable().getMetadataObject();
            SolrInputDocument updateDoc = new SolrInputDocument();
            for (String name : doc.getFieldNames()) {
                if (table.getColumnByName(name) != null) {
                    updateDoc.setField(name, doc.getFieldValue(name));
                }
            }
            int elementCount = obj.getChanges().size();
            for (int i = 0; i < elementCount; i++) {
                String columnName = SolrSQLHierarchyVistor.getColumnName(obj.getChanges().get(i).getSymbol());
                Literal value = (Literal) obj.getChanges().get(i).getValue();
                updateDoc.setField(columnName, value.getValue());
            }
            request.add(updateDoc);
        }
    });
    if (request.getDocuments() != null && !request.getDocuments().isEmpty()) {
        UpdateResponse response = this.connection.update(request);
        if (response.getStatus() != 0) {
            throw new TranslatorException(SolrPlugin.Event.TEIID20004, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20004, response.getStatus()));
        }
    }
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) Table(org.teiid.metadata.Table) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrDocumentCallback(org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback) TranslatorException(org.teiid.translator.TranslatorException)

Aggregations

UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)3 UpdateResponse (org.apache.solr.client.solrj.response.UpdateResponse)3 SolrDocument (org.apache.solr.common.SolrDocument)3 TranslatorException (org.teiid.translator.TranslatorException)3 SolrDocumentCallback (org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)2 Table (org.teiid.metadata.Table)2 KeyRecord (org.teiid.metadata.KeyRecord)1