Apache Kafka - Apache Spark ile Twitter Streaming Uygulamasına aşağıdaki adresten ulaşabilirsiniz.
https://github.com/aliturgutbozkurt/kafka-spark-twitter-streaming
https://github.com/aliturgutbozkurt/kafka-spark-twitter-streaming
import org.apache.spark.SparkConf; import org.apache.spark.api.java.JavaRDD; import org.apache.spark.api.java.JavaSparkContext; import scala.Tuple2; import java.util.Arrays; public class Main { public static void main(String[] args) { SparkConf conf = new SparkConf().setAppName("wordCounter").setMaster("local[*]"); JavaSparkContext sc = new JavaSparkContext(conf); JavaRDD<String> inputData = sc.textFile("src/main/resources/input.txt"); inputData.map(value -> value.replaceAll("[^a-zA-z\\s]", "")) .flatMap(value -> Arrays.asList(value.split(" ")).iterator()) .filter(word -> word.length() > 1) .mapToPair(value -> new Tuple2<String,Long>( value, 1L)) .reduceByKey((value1, value2) -> value1 + value2) .mapToPair(value -> new Tuple2<Long,String>(value._2, value._1)).sortByKey(false,1) .foreach(w->System.out.println(w)); sc.close(); } }
4.0.0 com.example myproject 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE org.springframework.boot spring-boot-starter-web
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }“Starters”: Spring boot bir çok starter jar'larına sahiptir. Örnek uygulamamızda pom.xml'de görebileceğiniz gibi spring-boot-starter-parent'a pom'un parent kısmında sahibiz. Bu özel starter Maven için default değerler sağlar. spring-boot-starter-parent ayrıca bağımlılık yönetimi bölümünü sağlar; böylece "kutsanmış" sürüm etiketlerini atlayabilirsiniz. Yani diğer starter bağımlılıklar için sürüm etiketi girmenize gerek kalmaz.
Offical starter listesine bu yazım'dan ulaşabilirsiniz.Fowler-SR2
1.8
Not : spring-boot-starter-parent POM repackage goal'i için konfigurasyonlara sahiptir. Eğer bu parent pom'u pom'a dahil etmediyseniz bu konfigurasyonu kendiniz deklare etmelisizniz. pom.xml'i kaydedip proje dizininde mvn package'i çalıştırırsanız :org.springframework.boot spring-boot-maven-plugin
package com.example.myproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }Configuration Class'ı : Spring boot uygulaması yazarken Java-based konfigurasyon en populer olandır. Elbetteki bu işlem XML temelli olarak yapılabilmekte ise de, konfigurasyon yapılacak (container için bean üretme) işini class'lara @Configuration anatasyonu koyarak gerçekleştirirz.
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.jdbc.*; import org.springframework.context.annotation.*; @Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { }
package com.example.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class DatabaseAccountService implements AccountService { private final RiskAssessor riskAssessor; @Autowired public DatabaseAccountService(RiskAssessor riskAssessor) { this.riskAssessor = riskAssessor; } // ... }
@Service public class DatabaseAccountService implements AccountService { private final RiskAssessor riskAssessor; public DatabaseAccountService(RiskAssessor riskAssessor) { this.riskAssessor = riskAssessor; } // ... }
package com.example.myproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }Uygulamanızı Çalıştırmak : Burada oluşturduğunuz jar dosyalarınızı çalıştırmaktan bahsedeceğiz. Eğer uygulamanız web uygulaması ise içinde gömülü tomcat olacaktır. Eğer war şeklinde paketlemeyi seçtiyseniz uygulama sunucunuzun war'ı nasıl çalıştırdığına bakmanız gerekmektedir.