Search in sources :

Example 46 with WebPage

use of org.apache.gora.examples.generated.WebPage in project gora by apache.

the class DataStoreTestUtil method assertEqualEmployeeObjects.

/**
   * Simple function which iterates through a before (put) and after (get) object
   * in an attempt to verify if the same field's and values have been obtained.
   * Within the original employee object we iterate from 1 instead of 0 due to the 
   * removal of the '__g__' field at position 0 when we put objects into the datastore. 
   * This field is used to identify whether fields within the object, and 
   * consequently the object itself, are/is dirty however this field is not 
   * required when persisting the object.
   * We explicitly get values from each field as this makes it easier to debug 
   * if tests go wrong.
   * @param employee
   * @param after
   */
private static void assertEqualEmployeeObjects(Employee employee, Employee after) {
    //for (int i = 1; i < employee.SCHEMA$.getFields().size(); i++) {
    //  for (int j = 1; j < after.SCHEMA$.getFields().size(); j++) {
    //    assertEquals(employee.SCHEMA$.getFields().get(i), after.SCHEMA$.getFields().get(j));
    //  }
    //}
    //check name field
    CharSequence beforeName = employee.getName();
    CharSequence afterName = after.getName();
    assertEquals(beforeName, afterName);
    //check dateOfBirth field
    Long beforeDOB = employee.getDateOfBirth();
    Long afterDOB = after.getDateOfBirth();
    assertEquals(beforeDOB, afterDOB);
    //check ssn field
    CharSequence beforeSsn = employee.getSsn();
    CharSequence afterSsn = after.getSsn();
    assertEquals(beforeSsn, afterSsn);
    //check salary field
    Integer beforeSalary = employee.getSalary();
    Integer afterSalary = after.getSalary();
    assertEquals(beforeSalary, afterSalary);
    //check boss field
    if (employee.getBoss() != null) {
        if (employee.getBoss() instanceof Utf8) {
            String beforeBoss = employee.getBoss().toString();
            String afterBoss = after.getBoss().toString();
            assertEquals("Boss String field values in UNION should be the same", beforeBoss, afterBoss);
        } else {
            Employee beforeBoss = (Employee) employee.getBoss();
            Employee afterBoss = (Employee) after.getBoss();
            assertEqualEmployeeObjects(beforeBoss, afterBoss);
        }
    }
    //check webpage field
    if (employee.getWebpage() != null) {
        WebPage beforeWebPage = employee.getWebpage();
        WebPage afterWebPage = after.getWebpage();
        assertEqualWebPageObjects(beforeWebPage, afterWebPage);
    }
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Employee(org.apache.gora.examples.generated.Employee) Utf8(org.apache.avro.util.Utf8)

Example 47 with WebPage

use of org.apache.gora.examples.generated.WebPage in project gora by apache.

the class TestAvroStore method testQueryWebPages.

//AvroStore should be closed so that Hadoop file is completely flushed,
//so below test is copied and modified to close the store after pushing data
public static void testQueryWebPages(DataStore<String, WebPage> store) throws Exception {
    Query<String, WebPage> query = store.newQuery();
    Result<String, WebPage> result = query.execute();
    int i = 0;
    while (result.next()) {
        WebPage page = result.get();
        DataStoreTestUtil.assertWebPage(page, URL_INDEXES.get(page.getUrl().toString()));
        i++;
    }
    assertEquals(i, URLS.length);
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage)

Example 48 with WebPage

use of org.apache.gora.examples.generated.WebPage in project gora by apache.

the class TestMapFieldValueFilter method testSerialization.

@Test
public void testSerialization() throws IOException {
    MapFieldValueFilter<String, WebPage> filter = new MapFieldValueFilter<>();
    //set filter field name as metadata
    filter.setFieldName(WebPage.Field.METADATA.toString());
    filter.setMapKey(new Utf8("fetchTime"));
    filter.setFilterOp(FilterOp.EQUALS);
    filter.setFilterIfMissing(true);
    filter.getOperands().add(new Utf8("http://example.org"));
    byte[] byteArray = WritableUtils.toByteArray(filter);
    MapFieldValueFilter<String, WebPage> filter2 = new MapFieldValueFilter<>();
    filter2.readFields(new DataInputStream(new ByteArrayInputStream(byteArray)));
    assertEquals(filter.getFieldName(), filter2.getFieldName());
    assertEquals(filter.getMapKey(), filter2.getMapKey());
    assertEquals(filter.getFilterOp(), filter2.getFilterOp());
    assertArrayEquals(filter.getOperands().toArray(), filter2.getOperands().toArray());
    assertEquals(filter.isFilterIfMissing(), filter2.isFilterIfMissing());
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) ByteArrayInputStream(java.io.ByteArrayInputStream) Utf8(org.apache.avro.util.Utf8) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 49 with WebPage

use of org.apache.gora.examples.generated.WebPage in project gora by apache.

the class TestMapFieldValueFilter method testFilterBasics.

@Test
public void testFilterBasics() {
    MapFieldValueFilter<String, WebPage> filter = new MapFieldValueFilter<>();
    //set filter field name as outlinks
    filter.setFieldName(WebPage.Field.OUTLINKS.toString());
    filter.setMapKey(new Utf8("example"));
    filter.setFilterOp(FilterOp.EQUALS);
    filter.setFilterIfMissing(true);
    filter.getOperands().add(new Utf8("http://example.org"));
    WebPage page = WebPage.newBuilder().build();
    page.getOutlinks().put(new Utf8("example"), new Utf8("http://example.org"));
    assertFalse(filter.filter("irrelevant", page));
    page.getOutlinks().put(new Utf8("example"), new Utf8("http://example2.com"));
    assertTrue(filter.filter("irrelevant", page));
    page = new WebPage();
    assertTrue(filter.filter("irrelevant", page));
    filter.setFilterIfMissing(false);
    assertFalse(filter.filter("irrelevant", page));
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Utf8(org.apache.avro.util.Utf8) Test(org.junit.Test)

Example 50 with WebPage

use of org.apache.gora.examples.generated.WebPage in project gora by apache.

the class TestMapFieldValueFilter method testFilterEntryInMap.

@Test
public void testFilterEntryInMap() {
    MapFieldValueFilter<String, WebPage> filter = new MapFieldValueFilter<>();
    //set filter field name as outlinks
    filter.setFieldName(WebPage.Field.OUTLINKS.toString());
    filter.setMapKey(new Utf8("foobar.whatever"));
    filter.setFilterOp(FilterOp.EQUALS);
    filter.setFilterIfMissing(true);
    filter.getOperands().add(new Utf8("Click here for foobar!"));
    WebPage page = WebPage.newBuilder().build();
    assertTrue(filter.filter("irrelevant", page));
    page.getOutlinks().put(new Utf8("foobar.whatever"), new Utf8("Mismatch!"));
    assertTrue(filter.filter("irrelevant", page));
    page.getOutlinks().put(new Utf8("foobar.whatever"), new Utf8("Click here for foobar!"));
    assertFalse(filter.filter("irrelevant", page));
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Utf8(org.apache.avro.util.Utf8) Test(org.junit.Test)

Aggregations

WebPage (org.apache.gora.examples.generated.WebPage)67 Test (org.junit.Test)33 Utf8 (org.apache.avro.util.Utf8)32 DBObject (com.mongodb.DBObject)7 Configuration (org.apache.hadoop.conf.Configuration)6 Employee (org.apache.gora.examples.generated.Employee)5 Metadata (org.apache.gora.examples.generated.Metadata)4 BeanFactoryImpl (org.apache.gora.persistency.impl.BeanFactoryImpl)4 ByteBuffer (java.nio.ByteBuffer)3 org.apache.hadoop.hbase.client (org.apache.hadoop.hbase.client)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Field (org.apache.avro.Schema.Field)2 TokenDatum (org.apache.gora.examples.generated.TokenDatum)2 FilterList (org.apache.gora.filter.FilterList)2 TableName (org.apache.hadoop.hbase.TableName)2 Job (org.apache.hadoop.mapreduce.Job)2 Properties (java.util.Properties)1