Package com.csvreader

Examples of com.csvreader.CsvReader$RawRecordBuffer


    public void map(LongWritable key, Text text,
                    OutputCollector<IntWritable, Letter> collector,
                    Reporter reporter) throws IOException {

        /* the input is a string from a csv file - so let's parse it */
        CsvReader csvReader = new CsvReader( new StringReader(text.toString()), ',' );

        /* 1. split data into chuncks and then - */
        while (csvReader.readRecord())  {

            /* a letter is identified by ID */
            Integer id = Integer.parseInt(csvReader.get(0));

            /* attributes for each letter */
            String name = csvReader.get(1);
            String letterText = csvReader.get(2);

            int outputKey = (id % 2 == 0) ? 1 : 2;

            /* 2. pass the results to the collector: the key-value pair */
            collector.collect( new IntWritable(outputKey), new Letter(name, letterText));
View Full Code Here


    reader.close();
  }

  @Test
  public void test144() throws Exception {
    CsvReader reader = CsvReader.parse("\"" + generateString('a', 100000)
        + "\"");
    Assert.assertTrue(reader.readRecord());
    Assert.assertEquals(generateString('a', 100000), reader.get(0));
    Assert.assertEquals("\"" + generateString('a', 100000) + "\"", reader
        .getRawRecord());
    Assert.assertFalse(reader.readRecord());
    reader.close();
  }
View Full Code Here

    reader.close();
  }

  @Test
  public void test145() throws Exception {
    CsvReader reader = CsvReader.parse("\"" + generateString('a', 100001)
        + "\"");
    reader.setSafetySwitch(false);
    Assert.assertTrue(reader.readRecord());
    Assert.assertEquals(generateString('a', 100001), reader.get(0));
    Assert.assertFalse(reader.readRecord());
    reader.close();
  }
View Full Code Here

  }

  @Test
  public void test146() throws Exception {
    // testing SkipLine's buffer
    CsvReader reader = CsvReader.parse("\"" + generateString('a', 10000)
        + "\r\nb");
    Assert.assertEquals("", reader.getRawRecord());
    Assert.assertTrue(reader.skipLine());
    Assert.assertEquals("", reader.getRawRecord());
    Assert.assertTrue(reader.readRecord());
    Assert.assertEquals("b", reader.get(0));
    Assert.assertFalse(reader.readRecord());
    reader.close();
  }
View Full Code Here

    StringBuilder data = new StringBuilder(20000);
    for (int i = 0; i < 10000; i++) {
      data.append("\\b");
    }

    CsvReader reader = CsvReader.parse(data.toString());
    reader.setUseTextQualifier(false);
    reader.setEscapeMode(CsvReader.ESCAPE_MODE_BACKSLASH);
    Assert.assertTrue(reader.readRecord());
    Assert.assertEquals(generateString('\b', 10000), reader.get(0));
    Assert.assertFalse(reader.readRecord());
    reader.close();
  }
View Full Code Here

  @Test
  public void test148() throws Exception {
    // testing a specific case in GetRawRecord where the result is what's in
    // the data buffer
    // plus what's in the raw buffer
    CsvReader reader = CsvReader.parse("\"" + generateString('a', 100000)
        + "\"\r\n" + generateString('a', 100000));
    Assert.assertTrue(reader.readRecord());
    Assert.assertEquals(generateString('a', 100000), reader.get(0));
    Assert.assertEquals("\"" + generateString('a', 100000) + "\"", reader
        .getRawRecord());
    Assert.assertTrue(reader.readRecord());
    Assert.assertEquals(generateString('a', 100000), reader.get(0));
    Assert.assertEquals(generateString('a', 100000), reader.getRawRecord());
    Assert.assertFalse(reader.readRecord());
    reader.close();
  }
View Full Code Here

  @Test
  public void test149() throws Exception {
    try
    {
      CsvReader reader = new CsvReader("C:\\somefilethatdoesntexist.csv");
    }
    catch (Exception ex)
    {
      assertException(new FileNotFoundException("File C:\\somefilethatdoesntexist.csv does not exist."), ex);
    }
View Full Code Here

  @Test
  public void test173() throws Exception {
    FailingReader fail = new FailingReader();

    CsvReader reader = new CsvReader(fail);
    boolean exceptionThrown = false;

    Assert.assertFalse(fail.DisposeCalled);
    try {
      // need to test IO exception block logic while trying to read
      reader.readRecord();
    } catch (IOException ex) {
      // make sure stream that caused exception
      // has been sent a dipose call
      Assert.assertTrue(fail.DisposeCalled);
      exceptionThrown = true;
      Assert.assertEquals("Read failed.", ex.getMessage());
    } finally {
      reader.close();
    }

    Assert.assertTrue(exceptionThrown);

    // test to make sure object has been marked
    // internally as disposed
    try
    {
      reader.getHeaders();
    }
    catch (Exception ex)
    {
      assertException(new IOException("This instance of the CsvReader class has already been closed."), ex);
    }
View Full Code Here

    Assert.assertEquals("1\r\n#blah\r\n2\r\n", data);
  }

  @Test
  public void test76() throws Exception {
    CsvReader reader = CsvReader.parse("user_id,name\r\n1,Bruce");
    Assert.assertEquals(null, reader.getHeaders());
    Assert.assertEquals(-1, reader.getIndex("user_id"));
    Assert.assertEquals("", reader.getHeader(0));
    Assert.assertTrue(reader.readHeaders());
    Assert.assertEquals(0, reader.getIndex("user_id"));
    Assert.assertEquals("user_id", reader.getHeader(0));
    String[] headers = reader.getHeaders();
    Assert.assertEquals(2, headers.length);
    Assert.assertEquals("user_id", headers[0]);
    Assert.assertEquals("name", headers[1]);
    reader.setHeaders(null);
    Assert.assertEquals(null, reader.getHeaders());
    Assert.assertEquals(-1, reader.getIndex("user_id"));
    Assert.assertEquals("", reader.getHeader(0));
    reader.close();
  }
View Full Code Here

    }
  }

  @Test
  public void test78() throws Exception {
    CsvReader reader = CsvReader.parse("1,Bruce");
    Assert.assertTrue(reader.readRecord());
    Assert.assertFalse(reader.isQualified(999));
    reader.close();
  }
View Full Code Here

TOP

Related Classes of com.csvreader.CsvReader$RawRecordBuffer

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.