Search in sources :

Example 96 with Utf8

use of org.apache.avro.util.Utf8 in project gora by apache.

the class TestCouchDBStore method testCreateAndDeleteSchema.

@Test
public void testCreateAndDeleteSchema() throws IOException {
    WebPage page = webPageStore.newPersistent();
    // Write webpage data
    page.setUrl(new Utf8("http://example.com"));
    webPageStore.put("com.example/http", page);
    webPageStore.flush();
    assertEquals("WebPage isn't created.", page.getUrl(), webPageStore.get("com.example/http").getUrl());
    webPageStore.deleteSchema();
    assertNull(webPageStore.get("com.example/http"));
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Utf8(org.apache.avro.util.Utf8) Test(org.junit.Test)

Example 97 with Utf8

use of org.apache.avro.util.Utf8 in project gora by apache.

the class TestCouchDBStore method testPutAndGet.

@Test
public void testPutAndGet() {
    WebPage page = webPageStore.newPersistent();
    // Write webpage data
    page.setUrl(new Utf8("http://example.com"));
    byte[] contentBytes = "example content in example.com".getBytes(Charset.defaultCharset());
    ByteBuffer buff = ByteBuffer.wrap(contentBytes);
    page.setContent(buff);
    webPageStore.put("com.example/http", page);
    webPageStore.flush();
    WebPage storedPage = webPageStore.get("com.example/http");
    assertNotNull(storedPage);
    assertEquals(page.getUrl(), storedPage.getUrl());
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Utf8(org.apache.avro.util.Utf8) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 98 with Utf8

use of org.apache.avro.util.Utf8 in project gora by apache.

the class TestHBaseByteInterface method testEncodingDecoding.

@Test
public void testEncodingDecoding() throws Exception {
    for (int i = 0; i < 1000; i++) {
        //employer
        CharSequence name = new Utf8("john");
        long dateOfBirth = System.currentTimeMillis();
        int salary = 1337;
        CharSequence ssn = new Utf8(String.valueOf(RANDOM.nextLong()));
        Employee e = Employee.newBuilder().build();
        e.setName(name);
        e.setDateOfBirth(dateOfBirth);
        e.setSalary(salary);
        e.setSsn(ssn);
        byte[] employerBytes = HBaseByteInterface.toBytes(e, Employee.SCHEMA$);
        Employee e2 = (Employee) HBaseByteInterface.fromBytes(Employee.SCHEMA$, employerBytes);
        assertEquals(name, e2.getName());
        assertEquals(dateOfBirth, e2.getDateOfBirth().longValue());
        assertEquals(salary, e2.getSalary().intValue());
        assertEquals(ssn, e2.getSsn());
        //metadata
        CharSequence key = new Utf8("theKey");
        CharSequence value = new Utf8("theValue " + RANDOM.nextLong());
        HashMap<CharSequence, CharSequence> data = new HashMap<>();
        data.put(key, value);
        Metadata m = Metadata.newBuilder().build();
        m.setData(data);
        byte[] datumBytes = HBaseByteInterface.toBytes(m, Metadata.SCHEMA$);
        Metadata m2 = (Metadata) HBaseByteInterface.fromBytes(Metadata.SCHEMA$, datumBytes);
        assertEquals(value, m2.getData().get(key));
    }
}
Also used : Employee(org.apache.gora.examples.generated.Employee) HashMap(java.util.HashMap) Metadata(org.apache.gora.examples.generated.Metadata) Utf8(org.apache.avro.util.Utf8) Test(org.junit.Test)

Example 99 with Utf8

use of org.apache.avro.util.Utf8 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)

Example 100 with Utf8

use of org.apache.avro.util.Utf8 in project gora by apache.

the class HBaseByteInterface method fromBytes.

/**
   * Deserializes an array of bytes matching the given schema to the proper basic 
   * (enum, Utf8,...) or complex type (Persistent/Record).
   * 
   * Does not handle <code>arrays/maps</code> if not inside a <code>record</code> type.
   * 
   * @param schema Avro schema describing the expected data
   * @param val array of bytes with the data serialized
   * @return Enum|Utf8|ByteBuffer|Integer|Long|Float|Double|Boolean|Persistent|Null
   * @throws IOException
   */
@SuppressWarnings({ "rawtypes" })
public static Object fromBytes(Schema schema, byte[] val) throws IOException {
    Type type = schema.getType();
    switch(type) {
        case ENUM:
            return AvroUtils.getEnumValue(schema, val[0]);
        case STRING:
            return new Utf8(Bytes.toString(val));
        case BYTES:
            return ByteBuffer.wrap(val);
        case INT:
            return Bytes.toInt(val);
        case LONG:
            return Bytes.toLong(val);
        case FLOAT:
            return Bytes.toFloat(val);
        case DOUBLE:
            return Bytes.toDouble(val);
        case BOOLEAN:
            return val[0] != 0;
        case UNION:
            // if 'val' is empty we ignore the special case (will match Null in "case RECORD")  
            if (schema.getTypes().size() == 2) {
                // schema [type0, type1]
                Type type0 = schema.getTypes().get(0).getType();
                Type type1 = schema.getTypes().get(1).getType();
                // Check if types are different and there's a "null", like ["null","type"] or ["type","null"]
                if (!type0.equals(type1) && (type0.equals(Schema.Type.NULL) || type1.equals(Schema.Type.NULL))) {
                    if (type0.equals(Schema.Type.NULL))
                        schema = schema.getTypes().get(1);
                    else
                        schema = schema.getTypes().get(0);
                    // Deserialize as if schema was ["type"] 
                    return fromBytes(schema, val);
                }
            }
        case RECORD:
            // For UNION schemas, must use a specific SpecificDatumReader
            // from the readerMap since unions don't have own name
            // (key name in map will be "UNION-type-type-...")
            String schemaId = schema.getType().equals(Schema.Type.UNION) ? String.valueOf(schema.hashCode()) : schema.getFullName();
            SpecificDatumReader<?> reader = readerMap.get(schemaId);
            if (reader == null) {
                // ignore dirty bits
                reader = new SpecificDatumReader(schema);
                SpecificDatumReader localReader = null;
                if ((localReader = readerMap.putIfAbsent(schemaId, reader)) != null) {
                    reader = localReader;
                }
            }
            // initialize a decoder, possibly reusing previous one
            BinaryDecoder decoderFromCache = decoders.get();
            BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(val, null);
            // put in threadlocal cache if the initial get was empty
            if (decoderFromCache == null) {
                decoders.set(decoder);
            }
            return reader.read(null, decoder);
        default:
            throw new RuntimeException("Unknown type: " + type);
    }
}
Also used : Type(org.apache.avro.Schema.Type) Utf8(org.apache.avro.util.Utf8) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) BinaryDecoder(org.apache.avro.io.BinaryDecoder)

Aggregations

Utf8 (org.apache.avro.util.Utf8)123 Test (org.junit.Test)34 WebPage (org.apache.gora.examples.generated.WebPage)32 GenericRecord (org.apache.avro.generic.GenericRecord)17 Schema (org.apache.avro.Schema)14 GenericData (org.apache.avro.generic.GenericData)13 ByteBuffer (java.nio.ByteBuffer)12 HashMap (java.util.HashMap)12 Map (java.util.Map)12 Employee (org.apache.gora.examples.generated.Employee)11 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Field (org.apache.avro.Schema.Field)6 Record (org.apache.avro.generic.GenericData.Record)5 File (java.io.File)4 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)4 Metadata (org.apache.gora.examples.generated.Metadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Iterator (java.util.Iterator)3 List (java.util.List)3