Search in sources :

Example 16 with StreamApplication

use of org.apache.samza.application.StreamApplication in project samza by apache.

the class TestTaskFactoryUtil method testCreateStreamApplication.

@Test
public void testCreateStreamApplication() throws Exception {
    Config config = new MapConfig(new HashMap<String, String>() {

        {
            this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.TestStreamApplication");
        }
    });
    StreamApplication streamApp = TaskFactoryUtil.createStreamApplication(config);
    assertNotNull(streamApp);
    Object retFactory = TaskFactoryUtil.createTaskFactory(config, streamApp, mockRunner);
    assertTrue(retFactory instanceof StreamTaskFactory);
    assertTrue(((StreamTaskFactory) retFactory).createInstance() instanceof StreamOperatorTask);
    config = new MapConfig(new HashMap<String, String>() {

        {
            this.put(ApplicationConfig.APP_CLASS, "org.apache.samza.testUtils.InvalidStreamApplication");
        }
    });
    try {
        TaskFactoryUtil.createStreamApplication(config);
        fail("Should have failed w/ no.such.class");
    } catch (ConfigException ce) {
    // expected
    }
    config = new MapConfig(new HashMap<String, String>() {

        {
            this.put(ApplicationConfig.APP_CLASS, "no.such.class");
        }
    });
    try {
        TaskFactoryUtil.createStreamApplication(config);
        fail("Should have failed w/ no.such.class");
    } catch (ConfigException ce) {
    // expected
    }
    config = new MapConfig(new HashMap<String, String>() {

        {
            this.put(ApplicationConfig.APP_CLASS, "");
        }
    });
    streamApp = TaskFactoryUtil.createStreamApplication(config);
    assertNull(streamApp);
    config = new MapConfig(new HashMap<>());
    streamApp = TaskFactoryUtil.createStreamApplication(config);
    assertNull(streamApp);
}
Also used : HashMap(java.util.HashMap) ApplicationConfig(org.apache.samza.config.ApplicationConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) StreamApplication(org.apache.samza.application.StreamApplication) ConfigException(org.apache.samza.config.ConfigException) MapConfig(org.apache.samza.config.MapConfig) Test(org.junit.Test)

Example 17 with StreamApplication

use of org.apache.samza.application.StreamApplication in project samza by apache.

the class LocalContainerRunner method main.

public static void main(String[] args) throws Exception {
    Thread.setDefaultUncaughtExceptionHandler(new SamzaContainerExceptionHandler(() -> {
        log.info("Exiting process now.");
        System.exit(1);
    }));
    String containerId = System.getenv(ShellCommandConfig.ENV_CONTAINER_ID());
    log.info(String.format("Got container ID: %s", containerId));
    String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL());
    log.info(String.format("Got coordinator URL: %s", coordinatorUrl));
    int delay = new Random().nextInt(SamzaContainer.DEFAULT_READ_JOBMODEL_DELAY_MS()) + 1;
    JobModel jobModel = SamzaContainer.readJobModel(coordinatorUrl, delay);
    Config config = jobModel.getConfig();
    JobConfig jobConfig = new JobConfig(config);
    if (jobConfig.getName().isEmpty()) {
        throw new SamzaException("can not find the job name");
    }
    String jobName = jobConfig.getName().get();
    String jobId = jobConfig.getJobId().getOrElse(ScalaToJavaUtils.defaultValue("1"));
    MDC.put("containerName", "samza-container-" + containerId);
    MDC.put("jobName", jobName);
    MDC.put("jobId", jobId);
    StreamApplication streamApp = TaskFactoryUtil.createStreamApplication(config);
    LocalContainerRunner localContainerRunner = new LocalContainerRunner(jobModel, containerId);
    localContainerRunner.run(streamApp);
}
Also used : Random(java.util.Random) JobConfig(org.apache.samza.config.JobConfig) ShellCommandConfig(org.apache.samza.config.ShellCommandConfig) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) SamzaContainerExceptionHandler(org.apache.samza.container.SamzaContainerExceptionHandler) JobModel(org.apache.samza.job.model.JobModel) SamzaException(org.apache.samza.SamzaException) JobConfig(org.apache.samza.config.JobConfig)

Example 18 with StreamApplication

use of org.apache.samza.application.StreamApplication in project samza by apache.

the class TaskFactoryUtil method createStreamApplication.

/**
   * Returns {@link StreamApplication} if it's configured, otherwise null.
   * @param config Config
   * throws {@link ConfigException} if there is misconfiguration of StreamApp.
   * @return {@link StreamApplication} instance
   */
public static StreamApplication createStreamApplication(Config config) {
    ApplicationConfig appConfig = new ApplicationConfig(config);
    if (appConfig.getAppClass() != null && !appConfig.getAppClass().isEmpty()) {
        TaskConfig taskConfig = new TaskConfig(config);
        if (taskConfig.getTaskClass() != null && !taskConfig.getTaskClass().isEmpty()) {
            throw new ConfigException("High level StreamApplication API cannot be used together with low-level API using task.class.");
        }
        String appClassName = appConfig.getAppClass();
        try {
            Class<?> builderClass = Class.forName(appClassName);
            return (StreamApplication) builderClass.newInstance();
        } catch (Throwable t) {
            String errorMsg = String.format("Failed to create StreamApplication class from the config. %s = %s", ApplicationConfig.APP_CLASS, appConfig.getAppClass());
            log.error(errorMsg, t);
            throw new ConfigException(errorMsg, t);
        }
    } else {
        return null;
    }
}
Also used : ApplicationConfig(org.apache.samza.config.ApplicationConfig) StreamApplication(org.apache.samza.application.StreamApplication) TaskConfig(org.apache.samza.config.TaskConfig) ConfigException(org.apache.samza.config.ConfigException)

Example 19 with StreamApplication

use of org.apache.samza.application.StreamApplication in project samza by apache.

the class ApplicationRunnerMain method main.

public static void main(String[] args) throws Exception {
    ApplicationRunnerCommandLine cmdLine = new ApplicationRunnerCommandLine();
    OptionSet options = cmdLine.parser().parse(args);
    Config orgConfig = cmdLine.loadConfig(options);
    Config config = Util.rewriteConfig(orgConfig);
    ApplicationRunnerOperation op = cmdLine.getOperation(options);
    if (config.containsKey(STREAM_APPLICATION_CLASS_CONFIG)) {
        ApplicationRunner runner = ApplicationRunner.fromConfig(config);
        StreamApplication app = (StreamApplication) Class.forName(config.get(STREAM_APPLICATION_CLASS_CONFIG)).newInstance();
        switch(op) {
            case RUN:
                runner.run(app);
                break;
            case KILL:
                runner.kill(app);
                break;
            case STATUS:
                System.out.println(runner.status(app));
                break;
            default:
                throw new IllegalArgumentException("Unrecognized operation: " + op);
        }
    } else {
        JobRunner$.MODULE$.main(args);
    }
}
Also used : Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) OptionSet(joptsimple.OptionSet)

Aggregations

StreamApplication (org.apache.samza.application.StreamApplication)19 Test (org.junit.Test)16 Config (org.apache.samza.config.Config)14 StreamSpec (org.apache.samza.system.StreamSpec)12 List (java.util.List)10 ImmutableList (com.google.common.collect.ImmutableList)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 Duration (java.time.Duration)8 ArrayList (java.util.ArrayList)8 Collection (java.util.Collection)8 HashMap (java.util.HashMap)8 Function (java.util.function.Function)8 Assert (junit.framework.Assert)8 Partition (org.apache.samza.Partition)8 MapConfig (org.apache.samza.config.MapConfig)8 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)8 FiringType (org.apache.samza.operators.triggers.FiringType)8 Trigger (org.apache.samza.operators.triggers.Trigger)8 Triggers (org.apache.samza.operators.triggers.Triggers)8 AccumulationMode (org.apache.samza.operators.windows.AccumulationMode)8