Package org.apache.commons.io

Examples of org.apache.commons.io.LineIterator


  public OutputTextLogger(InputStream inputStream) {
    super(inputStream);
  }

  public void run() {
    LineIterator it = null;

    try {
      it = IOUtils.lineIterator(inputStream, "UTF-8");

      while (it.hasNext()) {
        String line = it.nextLine();
        logger.debug(line);
      }
    } catch (IOException ioe) {
      logger.debug("Error consuming input stream: {}", ioe.getMessage());
    } catch (IllegalStateException ise) {
View Full Code Here


      return;
    }
    //stop submitted hadoop job
    System.out.println("start to kill hadoop job");
    try {
      LineIterator it = BasicUtils.lineIterator(
          new File(getStatusFileLocation(username, timestamp)), BasicUtils.ENCODING, false);
      while(it.hasNext()){
        String line = it.next().toString();
        if(StringUtils.isEmpty(line) || StringUtils.isEmpty(line.trim()))
          continue;
        line = line.trim();
        if(line.startsWith("Kill Command")){
          System.out.println(line);
View Full Code Here

    return s_instance;
  }
 
  public HiveQueryOutput parse(InputStream is, int limit) throws IOException{
    HiveQueryOutput result = new HiveQueryOutput();
    LineIterator it = IOUtils.lineIterator(is, BasicUtils.ENCODING);
    try{
      //the first line is column names
      result.setTitleList(parseOneLine(it.next().toString()));
      int lineNum = 0;
      while(it.hasNext() && lineNum < limit){
        result.addRow(parseOneLine(it.next().toString()));
        lineNum ++;
      }
    }finally{
      it.close();
    }
    return result;
  }
View Full Code Here

        LOG.debug("Sweeping blobs with modified time > than the configured max deleted time ({}). " +
                timestampToString(getLastMaxModifiedTime()));

        ConcurrentLinkedQueue<String> exceptionQueue = new ConcurrentLinkedQueue<String>();

        LineIterator iterator =
                FileUtils.lineIterator(fs.getGcCandidates(), Charsets.UTF_8.name());
        List<String> ids = Lists.newArrayList();

        while (iterator.hasNext()) {
            ids.add(iterator.next());

            if (ids.size() > getBatchCount()) {
                count += ids.size();
                executor.execute(new Sweeper(ids, exceptionQueue));
                ids = Lists.newArrayList();
View Full Code Here

  /**
   * Creates a new LineIterator instance on this reader.
   * @return LineIterator
   */
  public LineIterator iterateLines() {
    return new LineIterator(this);
  }
View Full Code Here

    if (s == null || s.isEmpty()) {
      return new XString("");
    } else {
      XString xs = new XString();
      try (NewLineReader r = new NewLineReader(new StringReader(s))) {
        LineIterator i = r.iterateLines();
        while (i.hasNext()) {
          xs.addLine(i.nextLine());
        }
        // Use the new line strategy of the input, provided we got some.
        // Otherwise we'll just leave the XString's default setting, which
        // is \n (NEW_LINE)
        if (r.getInputNewLine() != null) {
View Full Code Here

 
  private void fetchData(ILineSender sender, PGCopyInputStream inputStream) {
    try {
      Charset charset = Charset.forName(encoding);
        InputStreamReader reader = new InputStreamReader(inputStream,charset);
        LineIterator iterator = new LineIterator(reader);         
        while(iterator.hasNext()) {
          String lineData = iterator.nextLine();
          ILine line = new DefaultLine();
          for(String field:lineData.split(String.valueOf(DEP))){
            if(field.equals("\\N")){
              line.addField(null);
            } else {
View Full Code Here

  private void readFromHdfs(ILineSender lineSender) {
    FSDataInputStream in = null;
    CompressionCodecFactory factory;
    CompressionCodec codec;
    CompressionInputStream cin = null;
    LineIterator itr = null;
    try {
      conf = DFSUtils.getConf(filePath, null);
      fs = DFSUtils.createFileSystem(new URI(filePath), conf);
      in = fs.open(new Path(filePath));
      factory = new CompressionCodecFactory(conf);
      codec = factory.getCodec(new Path(filePath));
      if (codec == null) {
        LOG.info("codec not found, using text file reader");
        itr = new LineIterator(new BufferedReader(
            new InputStreamReader(in)));
      } else {
        LOG.info("found code " + codec.getClass());
        cin = codec.createInputStream(in);
        itr = new LineIterator(new BufferedReader(
            new InputStreamReader(cin)));
      }
      while (itr.hasNext()) {
        ILine oneLine = lineSender.createNewLine();
        String line = itr.nextLine();
        String[] parts = StringUtils
            .splitByWholeSeparatorPreserveAllTokens(line,
                FIELD_SEPARATOR);
        for (int i = 0; i < parts.length; i++) {
          if (HIVE_COLUMN_NULL_VALUE.equals(parts[i])) {
            oneLine.addField(null, i);
          } else {
            oneLine.addField(parts[i], i);
          }
        }
        boolean flag = lineSender.send(oneLine);
        if (flag) {
          getMonitor().increaseSuccessLines();
        } else {
          getMonitor().increaseFailedLines();
          LOG.debug("failed to send line: " + oneLine.toString('\t'));
        }
      }
      lineSender.flush();

    } catch (Exception e) {
      LOG.error(e.getCause());
      throw new WormholeException(e,
          JobStatus.READ_DATA_EXCEPTION.getStatus());
    } finally {
      if (itr != null) {
        itr.close();
      }
      try {
        if (cin != null) {
          cin.close();
        }
View Full Code Here

      return lines;
      }

    private LinkedList<String> populate( File localFile, LinkedList<String> lines ) throws IOException
      {
      LineIterator iterator = FileUtils.lineIterator( localFile, "UTF-8" );

      while( iterator.hasNext() )
        lines.add( iterator.next() );

      return lines;
      }
View Full Code Here

*
*/
public class IOLibTest {
  @Test
  public void lineIterator() {
    LineIterator i = new LineIterator(new StringReader("Line1\n"));
   
    Assert.assertTrue(i.hasNext());
    Assert.assertEquals(i.next(), "Line1");
    Assert.assertFalse(i.hasNext()); // Doesn't return an empty line
  }
View Full Code Here

TOP

Related Classes of org.apache.commons.io.LineIterator

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.