Search in sources :

Example 1 with MapFieldValueFilter

use of org.apache.gora.filter.MapFieldValueFilter in project gora by apache.

the class DefaultFactoryTest method createHeadersFilter.

private MapFieldValueFilter<String, WebPage> createHeadersFilter() {
    MapFieldValueFilter<String, WebPage> filter = new MapFieldValueFilter<>();
    filter.setFieldName(WebPage.Field.HEADERS.toString());
    filter.setMapKey(new Utf8("C.T"));
    filter.getOperands().add("text/html");
    return filter;
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Utf8(org.apache.avro.util.Utf8) MapFieldValueFilter(org.apache.gora.filter.MapFieldValueFilter)

Example 2 with MapFieldValueFilter

use of org.apache.gora.filter.MapFieldValueFilter in project gora by apache.

the class DefaultFactory method createFilter.

@Override
public org.apache.hadoop.hbase.filter.Filter createFilter(Filter<K, T> filter, HBaseStore<K, T> store) {
    if (filter instanceof FilterList) {
        FilterList<K, T> filterList = (FilterList<K, T>) filter;
        org.apache.hadoop.hbase.filter.FilterList hbaseFilter = new org.apache.hadoop.hbase.filter.FilterList(Operator.valueOf(filterList.getOperator().name()));
        for (Filter<K, T> rowFitler : filterList.getFilters()) {
            FilterFactory<K, T> factory = getHbaseFitlerUtil().getFactory(rowFitler);
            if (factory == null) {
                LOG.warn("HBase remote filter factory not yet implemented for " + rowFitler.getClass().getCanonicalName());
                return null;
            }
            org.apache.hadoop.hbase.filter.Filter hbaseRowFilter = factory.createFilter(rowFitler, store);
            if (hbaseRowFilter != null) {
                hbaseFilter.addFilter(hbaseRowFilter);
            }
        }
        return hbaseFilter;
    } else if (filter instanceof SingleFieldValueFilter) {
        SingleFieldValueFilter<K, T> fieldFilter = (SingleFieldValueFilter<K, T>) filter;
        HBaseColumn column = store.getMapping().getColumn(fieldFilter.getFieldName());
        CompareOperator compareOp = getCompareOp(fieldFilter.getFilterOp());
        byte[] family = column.getFamily();
        byte[] qualifier = column.getQualifier();
        byte[] value = HBaseByteInterface.toBytes(fieldFilter.getOperands().get(0));
        SingleColumnValueFilter hbaseFilter = new SingleColumnValueFilter(family, qualifier, compareOp, value);
        hbaseFilter.setFilterIfMissing(fieldFilter.isFilterIfMissing());
        return hbaseFilter;
    } else if (filter instanceof MapFieldValueFilter) {
        MapFieldValueFilter<K, T> mapFilter = (MapFieldValueFilter<K, T>) filter;
        HBaseColumn column = store.getMapping().getColumn(mapFilter.getFieldName());
        CompareOperator compareOp = getCompareOp(mapFilter.getFilterOp());
        byte[] family = column.getFamily();
        byte[] qualifier = HBaseByteInterface.toBytes(mapFilter.getMapKey());
        byte[] value = HBaseByteInterface.toBytes(mapFilter.getOperands().get(0));
        SingleColumnValueFilter hbaseFilter = new SingleColumnValueFilter(family, qualifier, compareOp, value);
        hbaseFilter.setFilterIfMissing(mapFilter.isFilterIfMissing());
        return hbaseFilter;
    } else {
        LOG.warn("HBase remote filter not yet implemented for " + filter.getClass().getCanonicalName());
        return null;
    }
}
Also used : HBaseColumn(org.apache.gora.hbase.store.HBaseColumn) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) FilterList(org.apache.gora.filter.FilterList) CompareOperator(org.apache.hadoop.hbase.CompareOperator) SingleFieldValueFilter(org.apache.gora.filter.SingleFieldValueFilter) MapFieldValueFilter(org.apache.gora.filter.MapFieldValueFilter)

Example 3 with MapFieldValueFilter

use of org.apache.gora.filter.MapFieldValueFilter in project gora by apache.

the class InfinispanQuery method build.

public void build() {
    LOG.debug("build()");
    FilterConditionContext context = null;
    if (q != null) {
        LOG.trace("Query already built; ignoring.");
        return;
    }
    QueryBuilder qb = ((InfinispanStore<K, T>) dataStore).getClient().getQueryBuilder();
    if (filter instanceof MapFieldValueFilter) {
        MapFieldValueFilter mfilter = (MapFieldValueFilter) filter;
        if (!(mfilter.getMapKey() instanceof Utf8))
            throw new IllegalAccessError("Invalid map key, must be a string.");
        if (mfilter.getOperands().size() > 1)
            throw new IllegalAccessError("MapFieldValueFilter operand not supported.");
        if (!(mfilter.getOperands().get(0) instanceof String))
            throw new IllegalAccessError("Invalid operand, must be a string.");
        String value = mfilter.getMapKey() + Support.DELIMITER + mfilter.getOperands().get(0).toString();
        switch(mfilter.getFilterOp()) {
            case EQUALS:
                if (value.equals("*")) {
                    context = qb.having(mfilter.getFieldName()).like(value);
                } else {
                    context = qb.having(mfilter.getFieldName()).eq(value);
                }
                if (!((MapFieldValueFilter) filter).isFilterIfMissing()) {
                    LOG.warn("Forcing isFilterMissing to true");
                    ((MapFieldValueFilter) filter).setFilterIfMissing(true);
                }
                break;
            case NOT_EQUALS:
                if (value.equals("*")) {
                    context = qb.not().having(mfilter.getFieldName()).like(value);
                } else {
                    context = qb.not().having(mfilter.getFieldName()).eq(value);
                }
                if (!((MapFieldValueFilter) filter).isFilterIfMissing()) {
                    LOG.warn("Forcing isFilterMissing to false");
                    ((MapFieldValueFilter) filter).setFilterIfMissing(false);
                }
                break;
            default:
                throw new IllegalAccessError("FilterOp not supported..");
        }
    } else if (filter instanceof SingleFieldValueFilter) {
        SingleFieldValueFilter sfilter = (SingleFieldValueFilter) filter;
        if (sfilter.getOperands().size() > 1)
            throw new IllegalAccessError("SingleFieldValueFilter operand not supported.");
        Object value = sfilter.getOperands().get(0);
        switch(sfilter.getFilterOp()) {
            case EQUALS:
                if (value.equals("*")) {
                    context = qb.having(sfilter.getFieldName()).like((String) value);
                } else {
                    context = qb.having(sfilter.getFieldName()).eq(value);
                }
                break;
            case NOT_EQUALS:
                if (value.equals("*")) {
                    context = qb.not().having(sfilter.getFieldName()).like((String) value);
                } else {
                    context = qb.not().having(sfilter.getFieldName()).eq(value);
                }
                break;
            case LESS:
                context = qb.having(sfilter.getFieldName()).lt(value);
                break;
            case LESS_OR_EQUAL:
                context = qb.having(sfilter.getFieldName()).lte(value);
                break;
            case GREATER:
                context = qb.having(sfilter.getFieldName()).gt(value);
                break;
            case GREATER_OR_EQUAL:
                context = qb.having(sfilter.getFieldName()).gte(value);
                break;
            default:
                throw new IllegalAccessError("FilterOp not supported..");
        }
    } else if (filter != null) {
        throw new IllegalAccessError("Filter not supported.");
    }
    if (this.startKey == this.endKey && this.startKey != null) {
        (context == null ? qb : context.and()).having(getPrimaryFieldName()).eq(this.startKey);
    } else {
        if (this.startKey != null && this.endKey != null)
            context = (context == null ? qb : context.and()).having(getPrimaryFieldName()).between(this.startKey, this.endKey);
        else if (this.startKey != null)
            context = (context == null ? qb : context.and()).having(getPrimaryFieldName()).between(this.startKey, null);
        else if (this.endKey != null)
            (context == null ? qb : context.and()).having(getPrimaryFieldName()).between(null, this.endKey);
    }
    // if projection enabled, keep the primary field.
    if (fields != null && fields.length > 0) {
        String[] fieldsWithPrimary;
        List<String> fieldsList = new ArrayList<>(Arrays.asList(fields));
        if (!fieldsList.contains(getPrimaryFieldName())) {
            fieldsWithPrimary = Arrays.copyOf(fields, fields.length + 1);
            fieldsWithPrimary[fields.length] = getPrimaryFieldName();
        } else {
            fieldsWithPrimary = fieldsList.toArray(new String[] {});
        }
        qb.setProjection(fieldsWithPrimary);
    }
    qb.orderBy((getSortingField().equals("")) ? getPrimaryFieldName() : getSortingField(), isAscendant ? SortOrder.ASC : SortOrder.DESC);
    if (this.getOffset() >= 0)
        qb.startOffset(this.getOffset());
    if (this.getLimit() > 0)
        qb.maxResults((int) this.getLimit());
    q = (RemoteQuery) qb.build();
    if (location != null)
        q.setLocation(location);
}
Also used : ArrayList(java.util.ArrayList) QueryBuilder(org.infinispan.avro.hotrod.QueryBuilder) FilterConditionContext(org.infinispan.query.dsl.FilterConditionContext) SingleFieldValueFilter(org.apache.gora.filter.SingleFieldValueFilter) Utf8(org.apache.avro.util.Utf8) MapFieldValueFilter(org.apache.gora.filter.MapFieldValueFilter)

Aggregations

MapFieldValueFilter (org.apache.gora.filter.MapFieldValueFilter)3 Utf8 (org.apache.avro.util.Utf8)2 SingleFieldValueFilter (org.apache.gora.filter.SingleFieldValueFilter)2 ArrayList (java.util.ArrayList)1 WebPage (org.apache.gora.examples.generated.WebPage)1 FilterList (org.apache.gora.filter.FilterList)1 HBaseColumn (org.apache.gora.hbase.store.HBaseColumn)1 CompareOperator (org.apache.hadoop.hbase.CompareOperator)1 SingleColumnValueFilter (org.apache.hadoop.hbase.filter.SingleColumnValueFilter)1 QueryBuilder (org.infinispan.avro.hotrod.QueryBuilder)1 FilterConditionContext (org.infinispan.query.dsl.FilterConditionContext)1