Search in sources :

Example 31 with Utf8

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

the class TestHBaseStore method assertTopLevelUnions.

/**
   * Checks that when writing a top level union <code>['null','type']</code> 
   * the value is written in raw format
   * @throws Exception
   */
@Test
public void assertTopLevelUnions() throws Exception {
    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();
    // Read directly from HBase
    HTable table = new HTable(conf, "WebPage");
    Get get = new Get(Bytes.toBytes("com.example/http"));
    org.apache.hadoop.hbase.client.Result result = table.get(get);
    byte[] bytesRead = result.getValue(Bytes.toBytes("content"), null);
    assertNotNull(bytesRead);
    assertTrue(Arrays.equals(bytesRead, contentBytes));
    table.close();
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Utf8(org.apache.avro.util.Utf8) org.apache.hadoop.hbase.client(org.apache.hadoop.hbase.client) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 32 with Utf8

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

the class BSONDecorator method getUtf8String.

/**
   * Access field as a Utf8 string.
   *
   * @param fieldName fully qualified name of the field to be accessed
   * @return value of the field as a {@link Utf8} string
   */
public Utf8 getUtf8String(String fieldName) {
    BasicDBObject parent = getFieldParent(fieldName);
    String value = parent.getString(getLeafName(fieldName));
    return (value != null) ? new Utf8(value) : null;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Utf8(org.apache.avro.util.Utf8)

Example 33 with Utf8

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

the class TestPersistentSerialization method testSerdeEmployeeTwoFields.

/**
   * Creates an Employee object setting only two fields as dirty.
   * We then do (de)serialization and check 'before' and 'after'
   * states.
   * @throws Exception
   */
@Test
public void testSerdeEmployeeTwoFields() throws Exception {
    Employee employee = Employee.newBuilder().build();
    employee.setSsn(new Utf8("11111"));
    employee.setSalary(100);
    TestIOUtils.testSerializeDeserialize(employee);
}
Also used : Employee(org.apache.gora.examples.generated.Employee) Utf8(org.apache.avro.util.Utf8) Test(org.junit.Test)

Example 34 with Utf8

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

the class TestPersistentSerialization method testSerdeMultipleWebPages.

/**
   * Creates multiple WebPage objects setting several fields to dirty.
   * Asserts that the data can be serialized and
   * deserialzed without loosing data. We do this by asserting
   * what we get 'before' and 'after' (de)serialization processes.
   * @throws Exception
   */
@Test
public void testSerdeMultipleWebPages() throws Exception {
    WebPage page1 = WebPage.newBuilder().build();
    WebPage page2 = WebPage.newBuilder().build();
    WebPage page3 = WebPage.newBuilder().build();
    page1.setUrl(new Utf8("foo"));
    page2.setUrl(new Utf8("baz"));
    page3.setUrl(new Utf8("bar"));
    page1.setParsedContent(new ArrayList<CharSequence>());
    page1.getParsedContent().add(new Utf8("coo"));
    page2.setOutlinks(new HashMap<CharSequence, CharSequence>());
    page2.getOutlinks().put(new Utf8("a"), new Utf8("b"));
    TestIOUtils.testSerializeDeserialize(page1, page2, page3);
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Utf8(org.apache.avro.util.Utf8) Test(org.junit.Test)

Example 35 with Utf8

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

the class TestPersistentBase method testClear.

/**
   * First we create a new WebPage object, to which we add some
   * field values. This makes the fields dirty as we have not 
   * flushed them to the datastore. We then clear the dirty
   * fields and assert that the values DO NOT exist for the 
   * field we previously made dirty.
   * We then set new values for fields, consequently making them 
   * dirty, before testing the clearing of an entirely new object
   * has all fields as null as they should be clean.
   */
@Test
public void testClear() {
    //test clear all fields
    WebPage page = WebPage.newBuilder().build();
    page.setUrl(new Utf8("http://foo.com"));
    page.getParsedContent().add(new Utf8("foo"));
    page.getOutlinks().put(new Utf8("foo"), new Utf8("bar"));
    page.setContent(ByteBuffer.wrap("foo baz bar".getBytes(Charset.defaultCharset())));
    page.clear();
    assertNull(page.getUrl());
    assertEquals(0, page.getParsedContent().size());
    assertEquals(0, page.getOutlinks().size());
    assertNull(page.getContent());
    //set fields again
    page.setUrl(new Utf8("http://bar.com"));
    page.getParsedContent().add(new Utf8("bar"));
    page.getOutlinks().put(new Utf8("bar"), new Utf8("baz"));
    page.setContent(ByteBuffer.wrap("foo baz bar barbaz".getBytes(Charset.defaultCharset())));
    //test clear new object
    page = WebPage.newBuilder().build();
    page.clear();
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Utf8(org.apache.avro.util.Utf8) Test(org.junit.Test)

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