Guest User

Stream Source

a guest
Jun 1st, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. package org.biplob.thesis.streamReader;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;
  8. import org.biplob.thesis.DataTypes.Point;
  9.  
  10. public class DataStreamGenerator extends RichParallelSourceFunction<Point> {
  11.    
  12.     private String filePath;
  13.     private float streamSpeed;
  14.     private volatile boolean running = true;
  15.    
  16.     public DataStreamGenerator()
  17.     {
  18.         filePath = "default file location";
  19.         streamSpeed = 1.0f;
  20.     }
  21.    
  22.     public DataStreamGenerator(String filePath, float streamSpeed)
  23.     {
  24.         this.filePath = filePath;
  25.         this.streamSpeed = streamSpeed;
  26.     }
  27.    
  28.     public void run(SourceContext<Point> ctx) throws IOException
  29.     {
  30.         //read dataset
  31.         String record;
  32.         long timeStamp = System.nanoTime();
  33.         FileInputStream fis = new FileInputStream(filePath);
  34.         BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  35.  
  36.         try
  37.         {
  38.             while((record = br.readLine())!= null && running == true)
  39.             {
  40.                 RecordReader recordObject = RecordReader.parseRecord(record, timestamp);
  41.                 Point readPoint = new Point(recordObject.getItems());
  42.                 ctx.collect(readPoint);
  43.                 Thread.sleep(100);
  44.                 timestamp = System.nanoTime();
  45.  
  46.             }
  47.         } catch (InterruptedException e) {
  48.             e.printStackTrace();
  49.         }
  50.  
  51.     }
  52.  
  53.     @Override
  54.     public void cancel() {
  55.         running = false;
  56.        
  57.     }
  58.  
  59. }
Add Comment
Please, Sign In to add comment