Search in sources :

Example 11 with Person

use of org.apache.crunch.test.Person in project crunch by cloudera.

the class AvroDeepCopierTest method testDeepCopyReflect.

@Test
public void testDeepCopyReflect() {
    Person person = new Person();
    person.setName("John Doe");
    person.setAge(42);
    person.setSiblingnames(Lists.<CharSequence>newArrayList());
    Person deepCopyPerson = new AvroDeepCopier.AvroReflectDeepCopier<Person>(Person.class, Person.SCHEMA$).deepCopy(person);
    assertEquals(person, deepCopyPerson);
    assertNotSame(person, deepCopyPerson);
}
Also used : Person(org.apache.crunch.test.Person) Test(org.junit.Test)

Example 12 with Person

use of org.apache.crunch.test.Person in project crunch by cloudera.

the class AvroTableTypeTest method testGetDetachedValue.

@Test
public void testGetDetachedValue() {
    Integer integerValue = 42;
    Person person = new Person();
    person.setName("John Doe");
    person.setAge(42);
    person.setSiblingnames(Lists.<CharSequence>newArrayList());
    Pair<Integer, Person> pair = Pair.of(integerValue, person);
    AvroTableType<Integer, Person> tableType = Avros.tableOf(Avros.ints(), Avros.reflects(Person.class));
    Pair<Integer, Person> detachedPair = tableType.getDetachedValue(pair);
    assertSame(integerValue, detachedPair.first());
    assertEquals(person, detachedPair.second());
    assertNotSame(person, detachedPair.second());
}
Also used : Person(org.apache.crunch.test.Person) Test(org.junit.Test)

Example 13 with Person

use of org.apache.crunch.test.Person in project crunch by cloudera.

the class AvroTypeSortTest method testSortAvroTypesBySelectedFields.

@Test
public void testSortAvroTypesBySelectedFields() throws Exception {
    MRPipeline pipeline = new MRPipeline(AvroTypeSortTest.class);
    Person ccc10 = createPerson("CCC", 10);
    Person bbb20 = createPerson("BBB", 20);
    Person aaa30 = createPerson("AAA", 30);
    writeAvroFile(Lists.newArrayList(ccc10, bbb20, aaa30), avroFile);
    PCollection<Person> unsorted = pipeline.read(At.avroFile(avroFile.getAbsolutePath(), records(Person.class)));
    // Sort by Name
    MapFn<Person, String> nameExtractor = new MapFn<Person, String>() {

        @Override
        public String map(Person input) {
            return input.getName().toString();
        }
    };
    PCollection<Person> sortedByName = unsorted.by(nameExtractor, strings()).groupByKey().ungroup().values();
    List<Person> sortedByNameList = Lists.newArrayList(sortedByName.materialize());
    assertEquals(3, sortedByNameList.size());
    assertEquals(aaa30, sortedByNameList.get(0));
    assertEquals(bbb20, sortedByNameList.get(1));
    assertEquals(ccc10, sortedByNameList.get(2));
    // Sort by Age
    MapFn<Person, Integer> ageExtractor = new MapFn<Person, Integer>() {

        @Override
        public Integer map(Person input) {
            return input.getAge();
        }
    };
    PCollection<Person> sortedByAge = unsorted.by(ageExtractor, ints()).groupByKey().ungroup().values();
    List<Person> sortedByAgeList = Lists.newArrayList(sortedByAge.materialize());
    assertEquals(3, sortedByAgeList.size());
    assertEquals(ccc10, sortedByAgeList.get(0));
    assertEquals(bbb20, sortedByAgeList.get(1));
    assertEquals(aaa30, sortedByAgeList.get(2));
    pipeline.done();
}
Also used : MRPipeline(org.apache.crunch.impl.mr.MRPipeline) Person(org.apache.crunch.test.Person) MapFn(org.apache.crunch.MapFn) Test(org.junit.Test)

Example 14 with Person

use of org.apache.crunch.test.Person in project crunch by cloudera.

the class AvroTypeSortTest method writeAvroFile.

private void writeAvroFile(List<Person> people, File avroFile) throws IOException {
    FileOutputStream outputStream = new FileOutputStream(avroFile);
    SpecificDatumWriter<Person> writer = new SpecificDatumWriter<Person>(Person.class);
    DataFileWriter<Person> dataFileWriter = new DataFileWriter<Person>(writer);
    dataFileWriter.create(Person.SCHEMA$, outputStream);
    for (Person person : people) {
        dataFileWriter.append(person);
    }
    dataFileWriter.close();
    outputStream.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) DataFileWriter(org.apache.avro.file.DataFileWriter) Person(org.apache.crunch.test.Person) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 15 with Person

use of org.apache.crunch.test.Person in project crunch by cloudera.

the class AvroFileSourceTargetTest method testSpecific.

@Test
public void testSpecific() throws IOException {
    GenericRecord savedRecord = new GenericData.Record(Person.SCHEMA$);
    savedRecord.put("name", "John Doe");
    savedRecord.put("age", 42);
    savedRecord.put("siblingnames", Lists.newArrayList("Jimmy", "Jane"));
    populateGenericFile(Lists.newArrayList(savedRecord), Person.SCHEMA$);
    Pipeline pipeline = new MRPipeline(AvroFileSourceTargetTest.class);
    PCollection<Person> genericCollection = pipeline.read(At.avroFile(avroFile.getAbsolutePath(), Avros.records(Person.class)));
    List<Person> personList = Lists.newArrayList(genericCollection.materialize());
    Person expectedPerson = new Person();
    expectedPerson.setName("John Doe");
    expectedPerson.setAge(42);
    List<CharSequence> siblingNames = Lists.newArrayList();
    siblingNames.add("Jimmy");
    siblingNames.add("Jane");
    expectedPerson.setSiblingnames(siblingNames);
    assertEquals(Lists.newArrayList(expectedPerson), Lists.newArrayList(personList));
}
Also used : MRPipeline(org.apache.crunch.impl.mr.MRPipeline) GenericRecord(org.apache.avro.generic.GenericRecord) Record(org.apache.avro.generic.GenericData.Record) GenericRecord(org.apache.avro.generic.GenericRecord) PojoPerson(org.apache.crunch.io.avro.AvroFileReaderFactoryTest.PojoPerson) Person(org.apache.crunch.test.Person) Pipeline(org.apache.crunch.Pipeline) MRPipeline(org.apache.crunch.impl.mr.MRPipeline) Test(org.junit.Test)

Aggregations

Person (org.apache.crunch.test.Person)15 Test (org.junit.Test)11 DataFileWriter (org.apache.avro.file.DataFileWriter)3 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)3 MRPipeline (org.apache.crunch.impl.mr.MRPipeline)3 FileOutputStream (java.io.FileOutputStream)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 Pipeline (org.apache.crunch.Pipeline)2 Employee (org.apache.crunch.test.Employee)2 Path (org.apache.hadoop.fs.Path)2 Record (org.apache.avro.generic.GenericData.Record)1 MapFn (org.apache.crunch.MapFn)1 Pair (org.apache.crunch.Pair)1 PojoPerson (org.apache.crunch.io.avro.AvroFileReaderFactoryTest.PojoPerson)1 Builder (org.apache.crunch.test.Person.Builder)1 Configuration (org.apache.hadoop.conf.Configuration)1 Before (org.junit.Before)1