/subprojects/s4-example/src/main/java/io/s4/example/counter/MyApp.java

https://github.com/adamwojtuniak/s4-piper · Java · 140 lines · 66 code · 23 blank · 51 comment · 1 complexity · a3cbd970a4f512174a26b2b2a6c45646 MD5 · raw file

  1. /*
  2. * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing,
  10. * software distributed under the License is distributed on an
  11. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. * either express or implied. See the License for the specific
  13. * language governing permissions and limitations under the
  14. * License. See accompanying LICENSE file.
  15. */
  16. package io.s4.example.counter;
  17. import com.google.inject.Guice;
  18. import com.google.inject.Inject;
  19. import com.google.inject.Injector;
  20. import com.google.inject.name.Named;
  21. import io.s4.core.App;
  22. import io.s4.core.Stream;
  23. /*
  24. * This is a sample application to test a new S4 API.
  25. * See README file for details.
  26. *
  27. * */
  28. final public class MyApp extends App {
  29. final private int interval;
  30. private GenerateUserEventPE generateUserEventPE;
  31. /*
  32. * We use Guice to pass parameters to the application. This is just a
  33. * trivial example where we get the value for the variable interval from a
  34. * properties file. (Saved under "src/main/resources".) All configuration
  35. * details are done in Module.java.
  36. *
  37. * The application graph itself is created in this Class. However,
  38. * developers may provide tools for creating apps which will generate the
  39. * objects.
  40. *
  41. * IMPORTANT: we create a graph of PE prototypes. The prototype is a class
  42. * instance that is used as a prototype from which all PE instance will be
  43. * created. The prototype itself is not used as an instance. (Except when
  44. * the PE is of type Singleton PE). To create a data structure for each PE
  45. * instance you must do in the method ProcessingElement.initPEInstance().
  46. */
  47. @Inject
  48. public MyApp(@Named("pe.counter.interval") int interval) {
  49. this.interval = interval;
  50. }
  51. /*
  52. * Build the application graph using POJOs. Don't like it? Write a nice
  53. * tool.
  54. *
  55. * @see io.s4.App#init()
  56. */
  57. @SuppressWarnings("unchecked")
  58. @Override
  59. protected void init() {
  60. /* PE that prints counts to console. */
  61. PrintPE printPE = createPE(PrintPE.class);
  62. Stream<CountEvent> userCountStream = createStream("User Count Stream",
  63. new CountKeyFinder(), printPE);
  64. Stream<CountEvent> genderCountStream = createStream(
  65. "Gender Count Stream", new CountKeyFinder(), printPE);
  66. Stream<CountEvent> ageCountStream = createStream("Age Count Stream",
  67. new CountKeyFinder(), printPE);
  68. /* PEs that count events by user, gender, and age. */
  69. CounterPE userCountPE = createPE(CounterPE.class);
  70. userCountPE.setOutputIntervalInEvents(interval);
  71. userCountPE.setCountStream(userCountStream);
  72. CounterPE genderCountPE = createPE(CounterPE.class);
  73. genderCountPE.setOutputIntervalInEvents(interval);
  74. genderCountPE.setCountStream(genderCountStream);
  75. CounterPE ageCountPE = createPE(CounterPE.class);
  76. ageCountPE.setOutputIntervalInEvents(interval);
  77. ageCountPE.setCountStream(ageCountStream);
  78. /* Streams that output user events keyed on user, gender, and age. */
  79. Stream<UserEvent> userStream = createStream("User Stream",
  80. new UserIDKeyFinder(), userCountPE);
  81. Stream<UserEvent> genderStream = createStream("Gender Stream",
  82. new GenderKeyFinder(), genderCountPE);
  83. Stream<UserEvent> ageStream = createStream("Age Stream",
  84. new AgeKeyFinder(), ageCountPE);
  85. generateUserEventPE = createPE(GenerateUserEventPE.class);
  86. generateUserEventPE.setStreams(userStream, genderStream, ageStream);
  87. }
  88. /*
  89. * Create and send 200 dummy events of type UserEvent.
  90. *
  91. * @see io.s4.App#start()
  92. */
  93. @Override
  94. protected void start() {
  95. for (int i = 0; i < 200; i++) {
  96. generateUserEventPE.processOutputEvent(null);
  97. }
  98. try {
  99. Thread.sleep(5000);
  100. } catch (InterruptedException e) {
  101. // TODO Auto-generated catch block
  102. e.printStackTrace();
  103. }
  104. System.out.println("Done. Closing...");
  105. removeAll();
  106. }
  107. @Override
  108. protected void close() {
  109. System.out.println("Bye.");
  110. }
  111. public static void main(String[] args) {
  112. Injector injector = Guice.createInjector(new Module());
  113. MyApp myApp = injector.getInstance(MyApp.class);
  114. myApp.init();
  115. myApp.start();
  116. }
  117. }