Package org.apache.pig.data

Examples of org.apache.pig.data.DataBag


    m2.put("x", m3);
    m2.put("y", m4);
    tuple.set(9, m2);

    // c:collection(f13:double, f14:float, f15:bytes)
    DataBag bagColl = TypesUtils.createBag();
    Schema schColl = schema.getColumn(10).getSchema();
    Tuple tupColl1 = TypesUtils.createTuple(schColl);
    Tuple tupColl2 = TypesUtils.createTuple(schColl);
    byte[] abs1 = new byte[3];
    byte[] abs2 = new byte[4];
    tupColl1.set(0, 3.1415926);
    tupColl1.set(1, 1.6);
    abs1[0] = 11;
    abs1[1] = 12;
    abs1[2] = 13;
    tupColl1.set(2, new DataByteArray(abs1));
    bagColl.add(tupColl1);
    tupColl2.set(0, 123.456789);
    tupColl2.set(1, 100);
    abs2[0] = 21;
    abs2[1] = 22;
    abs2[2] = 23;
    abs2[3] = 24;
    tupColl2.set(2, new DataByteArray(abs2));
    bagColl.add(tupColl2);
    tuple.set(10, bagColl);

    int row = 0;
    inserter.insert(new BytesWritable(String.format("k%d%d", part + 1, row + 1)
        .getBytes()), tuple);

    // row 2
    row++;
    TypesUtils.resetTuple(tuple);
    TypesUtils.resetTuple(tupRecord1);
    TypesUtils.resetTuple(tupRecord2);
    TypesUtils.resetTuple(tupRecord3);
    m1.clear();
    m2.clear();
    m3.clear();
    m4.clear();
    tuple.set(0, false);
    tuple.set(1, 2); // int
    tuple.set(2, 1002L); // long
    tuple.set(3, 3.1); // float
    tuple.set(4, "hello world 2"); // string
    tuple.set(5, new DataByteArray("hello byte 2")); // byte

    // r1:record(f1:int, f2:long
    tupRecord1.set(0, 2);
    tupRecord1.set(1, 1002L);
    tuple.set(6, tupRecord1);

    // r2:record(r3:record(f3:float, f4))
    tupRecord2.set(0, tupRecord3);
    tupRecord3.set(0, 2.3);
    tupRecord3.set(1, new DataByteArray("r3 row2  byte array"));
    tuple.set(7, tupRecord2);

    // m1:map(string)
    m1.put("a2", "A2");
    m1.put("b2", "B2");
    m1.put("c2", "C2");
    tuple.set(8, m1);

    // m2:map(map(int))
    m3.put("m321", 321);
    m3.put("m322", 322);
    m3.put("m323", 323);
    m2.put("z", m3);
    tuple.set(9, m2);

    // c:collection(f13:double, f14:float, f15:bytes)
    bagColl.clear();
    TypesUtils.resetTuple(tupColl1);
    TypesUtils.resetTuple(tupColl2);
    tupColl1.set(0, 7654.321);
    tupColl1.set(1, 0.0001);
    abs1[0] = 31;
    abs1[1] = 32;
    abs1[2] = 33;
    tupColl1.set(2, new DataByteArray(abs1));
    bagColl.add(tupColl1);
    tupColl2.set(0, 0.123456789);
    tupColl2.set(1, 0.3333);
    abs2[0] = 41;
    abs2[1] = 42;
    abs2[2] = 43;
    abs2[3] = 44;
    tupColl2.set(2, new DataByteArray(abs2));
    bagColl.add(tupColl2);
    tuple.set(10, bagColl);

    inserter.insert(new BytesWritable(String.format("k%d%d", part + 1, row + 1)
        .getBytes()), tuple);
View Full Code Here


    Tuple row = TypesUtils.createTuple(schema);
    // schema for "r:record..."
    Schema schRecord = writer.getSchema().getColumn(1).getSchema();
    Tuple tupRecord = TypesUtils.createTuple(schRecord);
    Schema schColl = schema.getColumn(3).getSchema();
    DataBag bagColl = TypesUtils.createBag();
    Tuple tupColl1 = TypesUtils.createTuple(schColl);
    Tuple tupColl2 = TypesUtils.createTuple(schColl);
    Map<String, String> map = new HashMap<String, String>();
    byte[] abs1 = new byte[3];
    byte[] abs2 = new byte[4];

    // row 1
    tupRecord.set(0, 1);
    tupRecord.set(1, 1001L);
    map.put("a", "x");
    map.put("b", "y");
    map.put("c", "z");
    tupColl1.set(0, 3.1415926);
    tupColl1.set(1, 1.6);
    abs1[0] = 11;
    abs1[1] = 12;
    abs1[2] = 13;
    tupColl1.set(2, new DataByteArray(abs1));
    bagColl.add(tupColl1);
    tupColl2.set(0, 123.456789);
    tupColl2.set(1, 100);
    abs2[0] = 21;
    abs2[1] = 22;
    abs2[2] = 23;
    abs2[3] = 24;
    tupColl2.set(2, new DataByteArray(abs2));
    bagColl.add(tupColl2);
    row.set(0, true);
    row.set(1, tupRecord);
    row.set(2, map);
    row.set(3, bagColl);
    ins.insert(new BytesWritable("k1".getBytes()), row);

    // row 2
    TypesUtils.resetTuple(row);
    TypesUtils.resetTuple(tupRecord);
    map.clear();
    tupRecord.set(0, 2);
    tupRecord.set(1, 1002L);
    map.put("boy", "girl");
    map.put("adam", "amy");
    map.put("bob", "becky");
    map.put("carl", "cathy");
    bagColl.clear();
    TypesUtils.resetTuple(tupColl1);
    TypesUtils.resetTuple(tupColl2);
    tupColl1.set(0, 7654.321);
    tupColl1.set(1, 0.0001);
    abs1[0] = 31;
    abs1[1] = 32;
    abs1[2] = 33;
    tupColl1.set(2, new DataByteArray(abs1));
    bagColl.add(tupColl1);
    tupColl2.set(0, 0.123456789);
    tupColl2.set(1, 0.3333);
    abs2[0] = 41;
    abs2[1] = 42;
    abs2[2] = 43;
    abs2[3] = 44;
    tupColl2.set(2, new DataByteArray(abs2));
    bagColl.add(tupColl2);
    row.set(0, false);
    row.set(1, tupRecord);
    row.set(2, map);
    row.set(3, bagColl);
    ins.insert(new BytesWritable("k2".getBytes()), row);
View Full Code Here

    Assert.assertEquals(1001L, tupRecord1.get(1));
    Map<String, String> map1 = (Map<String, String>) row.get(2);
    Assert.assertEquals("x", map1.get("a"));
    Assert.assertEquals("y", map1.get("b"));
    Assert.assertEquals("z", map1.get("c"));
    DataBag bagColl = (DataBag) row.get(3);
    Iterator<Tuple> itorColl = bagColl.iterator();
    Tuple tupColl = itorColl.next();
    Assert.assertEquals(3.1415926, tupColl.get(0));
    Assert.assertEquals(1.6, tupColl.get(1));
    DataByteArray dba = (DataByteArray) tupColl.get(2);
    Assert.assertEquals(11, dba.get()[0]);
    Assert.assertEquals(12, dba.get()[1]);
    Assert.assertEquals(13, dba.get()[2]);
    tupColl = itorColl.next();
    Assert.assertEquals(123.456789, tupColl.get(0));
    Assert.assertEquals(100, tupColl.get(1));
    dba = (DataByteArray) tupColl.get(2);
    Assert.assertEquals(21, dba.get()[0]);
    Assert.assertEquals(22, dba.get()[1]);
    Assert.assertEquals(23, dba.get()[2]);
    Assert.assertEquals(24, dba.get()[3]);

    // move to next row
    scanner.advance();
    scanner.getKey(key);
    Assert.assertEquals(key, new BytesWritable("k2".getBytes()));
    TypesUtils.resetTuple(row);
    scanner.getValue(row);
    Assert.assertEquals(false, row.get(0));
    Tuple tupRecord2 = (Tuple) row.get(1);
    Assert.assertEquals(2, tupRecord2.get(0));
    Assert.assertEquals(1002L, tupRecord2.get(1));
    Map<String, String> map2 = (Map<String, String>) row.get(2);
    Assert.assertEquals("girl", map2.get("boy"));
    Assert.assertEquals("amy", map2.get("adam"));
    Assert.assertEquals("cathy", map2.get("carl"));
    bagColl = (DataBag) row.get(3);
    itorColl = bagColl.iterator();
    tupColl = itorColl.next();
    Assert.assertEquals(7654.321, tupColl.get(0));
    Assert.assertEquals(0.0001, tupColl.get(1));
    tupColl = itorColl.next();
    Assert.assertEquals(0.123456789, tupColl.get(0));
View Full Code Here

    BytesWritable key = new BytesWritable();

    scanner.getKey(key);
    Assert.assertEquals(key, new BytesWritable("k1".getBytes()));
    scanner.getValue(row);
    DataBag bagColl = (DataBag) row.get(0);

    boolean f1 = (Boolean) (row.get(1));
    Assert.assertEquals(true, f1);

    Iterator<Tuple> itorColl = bagColl.iterator();
    Tuple tupColl = itorColl.next();
    Assert.assertEquals(3.1415926, tupColl.get(0));
    try {
      Object obj = tupColl.get(1);
      Assert.fail("Failed to catch 'out of boundary' exceptions.");
    } catch (IndexOutOfBoundsException e) {
      // no op, expecting out of bounds exceptions
    }
    tupColl = itorColl.next();
    Assert.assertEquals(123.456789, tupColl.get(0));
    Assert.assertNull(row.get(2));
    try {
      row.get(3);
      Assert.fail("Failed to catch 'out of boundary' exceptions.");
    } catch (IndexOutOfBoundsException e) {
      // no op, expecting out of bounds exceptions
    }

    scanner.advance();
    scanner.getKey(key);
    Assert.assertEquals(key, new BytesWritable("k2".getBytes()));
    TypesUtils.resetTuple(row);
    scanner.getValue(row);

    f1 = (Boolean) row.get(1);
    Assert.assertEquals(false, f1);

    bagColl.clear();
    bagColl = (DataBag) row.get(0);
    itorColl = bagColl.iterator();
    tupColl = itorColl.next();
    Assert.assertEquals(7654.321, tupColl.get(0));
    tupColl = itorColl.next();
    Assert.assertEquals(0.123456789, tupColl.get(0));
    Assert.assertNull(row.get(2));
View Full Code Here

    BytesWritable key = new BytesWritable();

    scanner.getKey(key);
    Assert.assertEquals(key, new BytesWritable("k1".getBytes()));
    scanner.getValue(row);
    DataBag bagColl = (DataBag) row.get(0);
    Iterator<Tuple> itorColl = bagColl.iterator();
    Tuple tupColl = itorColl.next();
    Assert.assertEquals(1.6, tupColl.get(0));
    // make sure only 1 element returned in the collection
    try {
      Object obj = tupColl.get(1);
      Assert.fail("Failed to catch 'out of boundary' exceptions.");
    } catch (IndexOutOfBoundsException e) {
      // no op, expecting out of bounds exceptions
    }
    tupColl = itorColl.next();
    Assert.assertEquals(100, tupColl.get(0));
    try {
      Object obj = tupColl.get(1);
      Assert.fail("Failed to catch 'out of boundary' exceptions.");
    } catch (IndexOutOfBoundsException e) {
      // no op, expecting out of bounds exceptions
    }
    Assert.assertFalse(itorColl.hasNext());
    Assert.assertEquals(true, row.get(1));
    Assert.assertEquals(1, row.get(2));
    Assert.assertNull(row.get(3));
    try {
      Object obj = row.get(4);
      Assert.fail("Failed to catch 'out of boundary' exceptions.");
    } catch (IndexOutOfBoundsException e) {
      // no op, expecting out of bounds exceptions
    }

    scanner.advance();
    scanner.getKey(key);
    Assert.assertEquals(key, new BytesWritable("k2".getBytes()));
    TypesUtils.resetTuple(row);
    scanner.getValue(row);
    bagColl.clear();
    bagColl = (DataBag) row.get(0);
    itorColl = bagColl.iterator();
    tupColl = itorColl.next();
    Assert.assertEquals(0.0001, tupColl.get(0));
    tupColl = itorColl.next();
    Assert.assertEquals(0.3333, tupColl.get(0));
    Assert.assertEquals(false, row.get(1));
View Full Code Here

            Schema schema = op.getSchema();
            if(schema == null) {
                throw new ExecException("Example Generator requires a schema. Please provide a schema while loading data.");
            }
           
            DataBag opBaseData = BagFactory.getInstance().newDefaultBag();

            POLoad poLoad = (POLoad) LogToPhyMap.get(op);
//            PigContext oldPC = poLoad.getPc();
//            poLoad.setPc(pigContext);

            poLoad.setLineageTracer(new LineageTracer());

            Tuple t = null;
            int count = 0;
            for (Result res = poLoad.getNext(t); res.returnStatus != POStatus.STATUS_EOP
                    && count < MAX_RECORDS; res = poLoad.getNext(t)) {
                if (res.returnStatus == POStatus.STATUS_NULL)
                    continue;
                if (res.returnStatus == POStatus.STATUS_ERR) {
                    log.error("Error reading Tuple");
                } else {
                    opBaseData.add((Tuple) res.result);
                    count++;
                }

            }
            baseData.put((LOLoad) op, opBaseData);
View Full Code Here

    m2.put("x", m3);
    m2.put("y", m4);
    tuple.set(9, m2);

    // c:collection(f13:double, f14:float, f15:bytes)
    DataBag bagColl = TypesUtils.createBag();
    Schema schColl = schema.getColumn(10).getSchema();
    Tuple tupColl1 = TypesUtils.createTuple(schColl);
    Tuple tupColl2 = TypesUtils.createTuple(schColl);
    byte[] abs1 = new byte[3];
    byte[] abs2 = new byte[4];
    tupColl1.set(0, 3.1415926);
    tupColl1.set(1, 1.6);
    abs1[0] = 11;
    abs1[1] = 12;
    abs1[2] = 13;
    tupColl1.set(2, new DataByteArray(abs1));
    bagColl.add(tupColl1);
    tupColl2.set(0, 123.456789);
    tupColl2.set(1, 100);
    abs2[0] = 21;
    abs2[1] = 22;
    abs2[2] = 23;
    abs2[3] = 24;
    tupColl2.set(2, new DataByteArray(abs2));
    bagColl.add(tupColl2);
    tuple.set(10, bagColl);

    int row = 0;
    inserter.insert(new BytesWritable(String.format("k%d%d", part + 1, row + 1)
        .getBytes()), tuple);

    // row 2
    row++;
    TypesUtils.resetTuple(tuple);
    TypesUtils.resetTuple(tupRecord1);
    TypesUtils.resetTuple(tupRecord2);
    TypesUtils.resetTuple(tupRecord3);
    m1.clear();
    m2.clear();
    m3.clear();
    m4.clear();
    tuple.set(0, false);
    tuple.set(1, 2); // int
    tuple.set(2, 1002L); // long
    tuple.set(3, 3.1); // float
    tuple.set(4, "hello world 2"); // string
    tuple.set(5, new DataByteArray("hello byte 2")); // byte

    // r1:record(f1:int, f2:long
    tupRecord1.set(0, 2);
    tupRecord1.set(1, 1002L);
    tuple.set(6, tupRecord1);

    // r2:record(r3:record(f3:float, f4))
    tupRecord2.set(0, tupRecord3);
    tupRecord3.set(0, 2.3);
    tupRecord3.set(1, new DataByteArray("r3 row2  byte array"));
    tuple.set(7, tupRecord2);

    // m1:map(string)
    m1.put("a2", "A2");
    m1.put("b2", "B2");
    m1.put("c2", "C2");
    tuple.set(8, m1);

    // m2:map(map(int))
    m3.put("m321", 321);
    m3.put("m322", 322);
    m3.put("m323", 323);
    m2.put("z", m3);
    tuple.set(9, m2);

    // c:collection(f13:double, f14:float, f15:bytes)
    bagColl.clear();
    TypesUtils.resetTuple(tupColl1);
    TypesUtils.resetTuple(tupColl2);
    tupColl1.set(0, 7654.321);
    tupColl1.set(1, 0.0001);
    abs1[0] = 31;
    abs1[1] = 32;
    abs1[2] = 33;
    tupColl1.set(2, new DataByteArray(abs1));
    bagColl.add(tupColl1);
    tupColl2.set(0, 0.123456789);
    tupColl2.set(1, 0.3333);
    abs2[0] = 41;
    abs2[1] = 42;
    abs2[2] = 43;
    abs2[3] = 44;
    tupColl2.set(2, new DataByteArray(abs2));
    bagColl.add(tupColl2);
    tuple.set(10, bagColl);

    inserter.insert(new BytesWritable(String.format("k%d%d", part + 1, row + 1)
        .getBytes()), tuple);
View Full Code Here

public class TestBagFormat extends TestCase {

    public static void testBagFormat() {
        try {
            DataBag bag = BagFactory.getInstance().newDefaultBag();

            Tuple tuple_1 = TupleFactory.getInstance().newTuple(1);
            tuple_1.set(0, 12);
            bag.add(tuple_1);

            Tuple tuple_2 = TupleFactory.getInstance().newTuple(1);
            DataBag innerBag = BagFactory.getInstance().newDefaultBag();
            innerBag.add(tuple_1);
            tuple_2.set(0, (innerBag));
            bag.add(tuple_2);

            System.out.println(BagFormat.format(bag));
            assertEquals("{(12),({(12)})}", BagFormat.format(bag));
View Full Code Here

    @Test
    public void testBag() throws Exception{
        String myBag = "{(1,a),(2,b)}";
        Object o = ps.getLoadCaster().bytesToBag(myBag.getBytes(), getBagFieldSchema());
        assertTrue(o instanceof DataBag);
        DataBag b = (DataBag)o;
        DataBag expectedBag = bagFactory.newDefaultBag();
        Tuple expectedTuple = tupleFactory.newTuple(2);
        expectedTuple.set(0, 1);
        expectedTuple.set(1, "a");
        expectedBag.add(expectedTuple);
        expectedTuple = tupleFactory.newTuple(2);
        expectedTuple.set(0, 2);
        expectedTuple.set(1, "b");
        expectedBag.add(expectedTuple);
        assertTrue(b.equals(expectedBag));
    }
View Full Code Here

    @Test
    public void testEmptyBag() throws Exception{
        String myBag = "{}";
        Object o = ps.getLoadCaster().bytesToBag(myBag.getBytes(), getBagFieldSchema());
        assertTrue(o instanceof DataBag);
        DataBag b = (DataBag)o;
        assertTrue(b.size()==0);
    }   
View Full Code Here

TOP

Related Classes of org.apache.pig.data.DataBag

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.