org.apache.flink.streaming.api.functions.source.RichSourceFunction Java Examples

The following examples show how to use org.apache.flink.streaming.api.functions.source.RichSourceFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParameterToolGetPropertiesMain.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromPropertiesFile(ParameterToolGetPropertiesMain.class.getResourceAsStream("/application.properties")));
    env.addSource(new RichSourceFunction<String>() {
        @Override
        public void run(SourceContext<String> sourceContext) throws Exception {
            while (true) {
                ParameterTool parameterTool = (ParameterTool) getRuntimeContext().getExecutionConfig().getGlobalJobParameters();
                sourceContext.collect(System.currentTimeMillis() + parameterTool.get("metrics.topic"));
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("ParameterTool Get config from SystemProperties");
}
 
Example #2
Source File: ParameterToolGetArgsMain.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.addSource(new RichSourceFunction<String>() {
        @Override
        public void run(SourceContext<String> sourceContext) throws Exception {
            while (true) {
                ParameterTool parameterTool = (ParameterTool) getRuntimeContext().getExecutionConfig().getGlobalJobParameters();
                sourceContext.collect(System.currentTimeMillis() + parameterTool.get("name") + parameterTool.get("username"));
            }
        }
        @Override
        public void cancel() {
        }
    }).print();

    env.execute("ParameterTool Get config from Args");
}
 
Example #3
Source File: ParameterToolGetSystemMain.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromSystemProperties());
    env.addSource(new RichSourceFunction<String>() {
        @Override
        public void run(SourceContext<String> sourceContext) throws Exception {
            while (true) {
                ParameterTool parameterTool = (ParameterTool) getRuntimeContext().getExecutionConfig().getGlobalJobParameters();
                sourceContext.collect(System.currentTimeMillis() + parameterTool.get("os.name") + parameterTool.get("user.home"));
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("ParameterTool Get config from SystemProperties");
}
 
Example #4
Source File: ParameterToolGetPropertiesMain.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromPropertiesFile(ParameterToolGetPropertiesMain.class.getResourceAsStream("/application.properties")));
    env.addSource(new RichSourceFunction<String>() {
        @Override
        public void run(SourceContext<String> sourceContext) throws Exception {
            while (true) {
                ParameterTool parameterTool = (ParameterTool) getRuntimeContext().getExecutionConfig().getGlobalJobParameters();
                sourceContext.collect(System.currentTimeMillis() + parameterTool.get("metrics.topic"));
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("ParameterTool Get config from SystemProperties");
}
 
Example #5
Source File: ParameterToolGetArgsMain.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.addSource(new RichSourceFunction<String>() {
        @Override
        public void run(SourceContext<String> sourceContext) throws Exception {
            while (true) {
                ParameterTool parameterTool = (ParameterTool) getRuntimeContext().getExecutionConfig().getGlobalJobParameters();
                sourceContext.collect(System.currentTimeMillis() + parameterTool.get("name") + parameterTool.get("username"));
            }
        }
        @Override
        public void cancel() {
        }
    }).print();

    env.execute("ParameterTool Get config from Args");
}
 
Example #6
Source File: ParameterToolGetSystemMain.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromSystemProperties());
    env.addSource(new RichSourceFunction<String>() {
        @Override
        public void run(SourceContext<String> sourceContext) throws Exception {
            while (true) {
                ParameterTool parameterTool = (ParameterTool) getRuntimeContext().getExecutionConfig().getGlobalJobParameters();
                sourceContext.collect(System.currentTimeMillis() + parameterTool.get("os.name") + parameterTool.get("user.home"));
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("ParameterTool Get config from SystemProperties");
}
 
Example #7
Source File: FlinkEntranceProcessingItem.java    From incubator-samoa with Apache License 2.0 5 votes vote down vote up
@Override
public void initialise() {
	final EntranceProcessor proc = getProcessor();
	final String streamId = getOutputStream().getStreamId();
	final int compID = getComponentId();

	
	outStream = env.addSource(new RichSourceFunction() {

		private volatile boolean isCancelled;
		
		@Override
		public void run(SourceContext sourceContext) throws Exception {
			while(!isCancelled && entrProc.hasNext())
			{
				sourceContext.collect(SamoaType.of(entrProc.nextEvent(), id));
			}
		}

		@Override
		public void cancel() {
			isCancelled = true;
		}

		EntranceProcessor entrProc = proc;
		String id = streamId;

		@Override
		public void open(Configuration parameters) throws Exception {
			super.open(parameters);
			entrProc.onCreate(compID);
		}
		
	}).returns(Utils.tempTypeInfo);

	((FlinkStream) getOutputStream()).initialise();
}
 
Example #8
Source File: FlinkApolloTest.java    From flink-learning with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.setParallelism(1);

    env.addSource(new RichSourceFunction<String>() {

        private Config config;

        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            config = ConfigService.getAppConfig();
            config.addChangeListener(new ConfigChangeListener() {
                @Override
                public void onChange(ConfigChangeEvent configChangeEvent) {
                    for (String key : configChangeEvent.changedKeys()) {
                        ConfigChange change = configChangeEvent.getChange(key);
                        log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                                change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                                change.getChangeType());
                    }
                }
            });
        }

        @Override
        public void run(SourceContext<String> ctx) throws Exception {
            while (true) {
                ctx.collect(config.getProperty("name", "zhisheng"));
                Thread.sleep(3000);
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("zhisheng flink Apollo");
}
 
Example #9
Source File: FlinkApolloTest.java    From flink-learning with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.setParallelism(1);

    env.addSource(new RichSourceFunction<String>() {

        private Config config;

        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            config = ConfigService.getAppConfig();
            config.addChangeListener(new ConfigChangeListener() {
                @Override
                public void onChange(ConfigChangeEvent configChangeEvent) {
                    for (String key : configChangeEvent.changedKeys()) {
                        ConfigChange change = configChangeEvent.getChange(key);
                        log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                                change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                                change.getChangeType());
                    }
                }
            });
        }

        @Override
        public void run(SourceContext<String> ctx) throws Exception {
            while (true) {
                ctx.collect(config.getProperty("name", "zhisheng"));
                Thread.sleep(3000);
            }
        }

        @Override
        public void cancel() {
        }
    }).print();

    env.execute("zhisheng flink Apollo");
}
 
Example #10
Source File: WaitingSource.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void setRuntimeContext(RuntimeContext t) {
	if (source instanceof RichSourceFunction) {
		((RichSourceFunction<T>) source).setRuntimeContext(t);
	}
}
 
Example #11
Source File: WaitingSource.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void open(Configuration parameters) throws Exception {
	if (source instanceof RichSourceFunction) {
		((RichSourceFunction<T>) source).open(parameters);
	}
}
 
Example #12
Source File: WaitingSource.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void close() throws Exception {
	if (source instanceof RichSourceFunction) {
		((RichSourceFunction<T>) source).close();
	}
}