Package org.apache.flink.test.recordJobs.util

Examples of org.apache.flink.test.recordJobs.util.Tuple


    private final StringValue nationName = new StringValue();
   
    @Override
    public void map(Record record, Collector<Record> out) throws Exception
    {
      final Tuple t = record.getField(1, this.tuple);
     
      this.nationName.setValue(t.getStringValueAt(1));
      record.setField(1, this.nationName);
      out.collect(record);
    }
View Full Code Here


  }

  @Test
  public void testAddAttributeString() {
   
    Tuple t = new Tuple();
   
    Assert.assertTrue(t.getNumberOfColumns() == 0);
   
    t.addAttribute("a");
    Assert.assertTrue(t.getNumberOfColumns() == 1);
    Assert.assertTrue(t.getStringValueAt(0).equals("a"));
    Assert.assertTrue(t.getBytes().length == 256);
   
    t.compact();
    t.addAttribute(123345+"");
    Assert.assertTrue(t.getNumberOfColumns() == 2);
    Assert.assertTrue(t.getLongValueAt(1) == 123345);
    Assert.assertTrue(t.getBytes().length == 9);
   
    t.addAttribute("adfasdfg");
    Assert.assertTrue(t.getNumberOfColumns() == 3);
    byte[] ret = t.getByteArrayValueAt(2);
    Assert.assertTrue(ret.length == "adfasdfg".getBytes().length);
    for(int i=0;i<ret.length;i++) {
      Assert.assertTrue(ret[i] == "adfasdfg".getBytes()[i]);
    }
    Assert.assertTrue(t.getBytes().length == 18);
   
  }
View Full Code Here

    private final DoubleValue d = new DoubleValue();
   
    @Override
    public void join(Record custRecord, Record olRecord, Collector<Record> out) throws Exception
    {
      final Tuple t = olRecord.getField(1, Tuple.class);
      final double extPrice = Double.parseDouble(t.getStringValueAt(0));
      final double discount = Double.parseDouble(t.getStringValueAt(1));
     
      this.d.setValue(extPrice * (1 - discount));
      custRecord.setField(2, this.d);
      out.collect(custRecord);
    }
View Full Code Here

  @Test
  public void testSerialization() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
   
    Tuple t = new Tuple();
    t.addAttribute("Hello world!");
    try {
      t.write(new OutputViewDataOutputStreamWrapper(dos));
    } catch (IOException e1) {
      e1.printStackTrace();
    }
   
    t.addAttribute("2ndAttribute");
    try {
      t.write(new OutputViewDataOutputStreamWrapper(dos));
    } catch (IOException e) {
      e.printStackTrace();
    }
   
    byte[] ba = "attr1|attr2|3|4|attr5|thisdoesnotbelongtothetuple".getBytes();
    int[] of2 = {0,6,12,14,16,22};
    t = new Tuple(ba, of2, 5);
   
    try {
      t.write(new OutputViewDataOutputStreamWrapper(dos));
    } catch (IOException e) {
      e.printStackTrace();
    }
   
    try {
      dos.flush();
      baos.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
   
    byte[] data = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    DataInputStream dis = new DataInputStream(bais);
   
    try {
      dos.close();
      baos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
   
    t = new Tuple();
    try {
      t.read(new InputViewDataInputStreamWrapper(dis));
     
      Assert.assertTrue(t.getNumberOfColumns() == 1);
      Assert.assertTrue(t.getStringValueAt(0).equals("Hello world!"));
      Assert.assertTrue(t.toString().equals("Hello world!|"));
    } catch (IOException e) {
      e.printStackTrace();
    }
   
    t = new Tuple();
    try {
      t.read(new InputViewDataInputStreamWrapper(dis));
     
      Assert.assertTrue(t.getNumberOfColumns() == 2);
      Assert.assertTrue(t.getStringValueAt(0).equals("Hello world!"));
      Assert.assertTrue(t.getStringValueAt(1).equals("2ndAttribute"));
      Assert.assertTrue(t.toString().equals("Hello world!|2ndAttribute|"));
    } catch (IOException e) {
      e.printStackTrace();
    }
   
    t = new Tuple();
    try {
      t.read(new InputViewDataInputStreamWrapper(dis));
     
      Assert.assertTrue(t.getNumberOfColumns() == 5);
      Assert.assertTrue(t.getStringValueAt(0).equals("attr1"));
      Assert.assertTrue(t.getStringValueAt(1).equals("attr2"));
      Assert.assertTrue(t.getLongValueAt(2) == 3);
      Assert.assertTrue(t.getLongValueAt(3) == 4);
      Assert.assertTrue(t.getStringValueAt(4).equals("attr5"));
      Assert.assertTrue(t.toString().equals("attr1|attr2|3|4|attr5|"));
    } catch (IOException e) {
      e.printStackTrace();
    }
   
    try {
View Full Code Here

  @Test
  public void testTupleByteArrayShortArrayInt() {
   
    String tupleStr = "attr1|attr2|3|4|attr5|";
    int[] offsets = {0,6,12,14,16,22};
    Tuple t1 = new Tuple(tupleStr.getBytes(),offsets,5);
   
    Assert.assertTrue(t1.getBytes().length == tupleStr.getBytes().length);
    for(int i=0;i<t1.getBytes().length;i++) {
      Assert.assertTrue(t1.getBytes()[i] == tupleStr.getBytes()[i]);
    }
    Assert.assertTrue(t1.getNumberOfColumns() == 5);
    Assert.assertTrue(t1.getStringValueAt(0).equals("attr1"));
    Assert.assertTrue(t1.getStringValueAt(1).equals("attr2"));
    Assert.assertTrue(t1.getLongValueAt(2) == 3);
    Assert.assertTrue(t1.getLongValueAt(3) == 4);
    Assert.assertTrue(t1.getStringValueAt(4).equals("attr5"));
   
  }
View Full Code Here

  }

  @Test
  public void testGetNumberOfColumns() {
   
    Tuple t = new Tuple();
   
    Assert.assertTrue(t.getNumberOfColumns() == 0);
   
    t.addAttribute("a");
    Assert.assertTrue(t.getNumberOfColumns() == 1);
   
    t.addAttribute("b");
    Assert.assertTrue(t.getNumberOfColumns() == 2);
   
    t.addAttribute("aasdfasd|fasdf");
    Assert.assertTrue(t.getNumberOfColumns() == 3);
   
    String tupleStr = "attr1|attr2|3|4|attr5|";
    int[] offsets = {0,6,12,14,16,22};
    t = new Tuple(tupleStr.getBytes(),offsets,5);
   
    Assert.assertTrue(t.getNumberOfColumns() == 5);
  }
View Full Code Here

  }

  @Test
  public void testGetBytes() {
   
    Tuple t1 = new Tuple();
   
    String[] values = {"a","b","cdefgh","2342432","Adasdfee324324D"};
   
    for(String val : values) {
      t1.addAttribute(val);
    }
   
    String exp1 = "";
    for(int i=0;i<values.length;i++) {
      exp1 += values[i]+"|";
    }
   
    byte[] ret1 = t1.getBytes();
   
    for(int i=0;i<exp1.getBytes().length;i++) {
      Assert.assertTrue(ret1[i] == exp1.getBytes()[i]);
    }
    Assert.assertTrue(ret1[exp1.getBytes().length+1] == 0);
   
    String tupleStr = "attr1|attr2|3|4|attr5|";
    int[] offsets = {0,6,12,14,16,22};
    Tuple t2 = new Tuple(tupleStr.getBytes(),offsets,5);
   
    byte[] ret2 = t2.getBytes();
   
    Assert.assertTrue(ret2.length == tupleStr.getBytes().length);
    for(int i=0;i<tupleStr.getBytes().length;i++) {
      Assert.assertTrue(ret2[i] == tupleStr.getBytes()[i]);
    }
View Full Code Here

    }
  }

  @Test
  public void testGetColumnLength() {
    Tuple t = new Tuple();
   
    Assert.assertTrue(t.getColumnLength(1) == -1);
    Assert.assertTrue(t.getColumnLength(0) == -1);
    Assert.assertTrue(t.getColumnLength(-1) == -1);
   
    t.addAttribute("a");
    Assert.assertTrue(t.getColumnLength(0) == 1);
   
    t.addAttribute("b");
    Assert.assertTrue(t.getColumnLength(1) == 1);
   
    t.addAttribute("aasdfasd|fasdf");
    Assert.assertTrue(t.getColumnLength(2) == 14);
   
    String tupleStr = "attr1|attr2|3|4|attr5|";
    int[] offsets = {0,6,12,14,16,22};
    t = new Tuple(tupleStr.getBytes(),offsets,5);
   
    Assert.assertTrue(t.getColumnLength(0) == 5);
    Assert.assertTrue(t.getColumnLength(1) == 5);
    Assert.assertTrue(t.getColumnLength(2) == 1);
    Assert.assertTrue(t.getColumnLength(3) == 1);
    Assert.assertTrue(t.getColumnLength(4) == 5);
   
  }
View Full Code Here

   
  }

  @Test
  public void testConcatenate() {
    Tuple t0 = new Tuple();
    Tuple t1 = new Tuple();
    Tuple t2 = new Tuple();
   
    // check handling of empty tuples
    t0.concatenate(t1);
    Assert.assertTrue(t0.getNumberOfColumns() == 0);
   
    t1.addAttribute("a");
    t1.concatenate(t0);
    Assert.assertTrue(t1.getNumberOfColumns() == 1);
    Assert.assertTrue(t1.getStringValueAt(0).equals("a"));
   
    t0.concatenate(t1);
    Assert.assertTrue(t0.getNumberOfColumns() == 1);
    Assert.assertTrue(t0.getStringValueAt(0).equals("a"));
   
    t1.addAttribute("b");
    t1.addAttribute("c");
    t2.addAttribute("z");
    t2.addAttribute("y");
    t2.addAttribute("x");
    t2.concatenate(t1);
    // check tuple t2
    Assert.assertTrue(t2.getNumberOfColumns() == 6);
    Assert.assertTrue(t2.getStringValueAt(0).equals("z"));
    Assert.assertTrue(t2.getStringValueAt(1).equals("y"));
    Assert.assertTrue(t2.getStringValueAt(2).equals("x"));
    Assert.assertTrue(t2.getStringValueAt(3).equals("a"));
    Assert.assertTrue(t2.getStringValueAt(4).equals("b"));
    Assert.assertTrue(t2.getStringValueAt(5).equals("c"));
   
    t1.concatenate(t2);
    // check tuple t1
    Assert.assertTrue(t1.getNumberOfColumns() == 9);
    Assert.assertTrue(t1.getStringValueAt(0).equals("a"));
    Assert.assertTrue(t1.getStringValueAt(1).equals("b"));
    Assert.assertTrue(t1.getStringValueAt(2).equals("c"));
    Assert.assertTrue(t1.getStringValueAt(3).equals("z"));
    Assert.assertTrue(t1.getStringValueAt(4).equals("y"));
    Assert.assertTrue(t1.getStringValueAt(5).equals("x"));
    Assert.assertTrue(t1.getStringValueAt(6).equals("a"));
    Assert.assertTrue(t1.getStringValueAt(7).equals("b"));
    Assert.assertTrue(t1.getStringValueAt(8).equals("c"));
    // check tuple t2
    Assert.assertTrue(t2.getNumberOfColumns() == 6);
    Assert.assertTrue(t2.getStringValueAt(0).equals("z"));
    Assert.assertTrue(t2.getStringValueAt(1).equals("y"));
    Assert.assertTrue(t2.getStringValueAt(2).equals("x"));
    Assert.assertTrue(t2.getStringValueAt(3).equals("a"));
    Assert.assertTrue(t2.getStringValueAt(4).equals("b"));
    Assert.assertTrue(t2.getStringValueAt(5).equals("c"));
   
  }
View Full Code Here

  }

  @Test
  public void testProject() {
   
    Tuple t = new Tuple();
   
    t.project(1);
    Assert.assertTrue(t.getNumberOfColumns() == 0);
   
    t.addAttribute("a");
    t.project(0);
    Assert.assertTrue(t.getNumberOfColumns() == 0);

    t.addAttribute("a");
    t.addAttribute("b");
    t.project(2);
    Assert.assertTrue(t.getNumberOfColumns() == 1);
    Assert.assertTrue(t.getStringValueAt(0).equals("b"));
   
    t.addAttribute("c");
    t.addAttribute("d");
    t.project(5);
    Assert.assertTrue(t.getNumberOfColumns() == 2);
    Assert.assertTrue(t.getStringValueAt(0).equals("b"));
    Assert.assertTrue(t.getStringValueAt(1).equals("d"));
   
    t.project(0);
    Assert.assertTrue(t.getNumberOfColumns() == 0);
   
    t.addAttribute("a");
    t.addAttribute("b");
    t.addAttribute("c");
    t.addAttribute("d");
    t.project(11);
    Assert.assertTrue(t.getNumberOfColumns() == 3);
    Assert.assertTrue(t.getStringValueAt(2).equals("d"));
   
    t.project(23);
    Assert.assertTrue(t.getNumberOfColumns() == 3);
    Assert.assertTrue(t.getStringValueAt(0).equals("a"));
    Assert.assertTrue(t.getStringValueAt(1).equals("b"));
    Assert.assertTrue(t.getStringValueAt(2).equals("d"));
   
    t.project(-1);
    Assert.assertTrue(t.getNumberOfColumns() == 3);
    Assert.assertTrue(t.getStringValueAt(0).equals("a"));
    Assert.assertTrue(t.getStringValueAt(1).equals("b"));
    Assert.assertTrue(t.getStringValueAt(2).equals("d"));
  }
View Full Code Here

TOP

Related Classes of org.apache.flink.test.recordJobs.util.Tuple

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.