Spark example
pom.xml
SparkTest.java
可以單獨執行,也可以提交到spark叢集: spark-submit.cmd --class SparkTest D:\workspace\spark-test\target\spark-test-0.0.1-SNAPSHOT.jar
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0</modelVersion>
-
<groupId>active </groupId>
-
<artifactId>spark-test</artifactId>
-
<version>0.0.1-SNAPSHOT</version>
-
-
<dependencies>
-
<dependency>
-
<groupId>org.apache.spark</groupId>
-
<artifactId>spark-core_2.10</artifactId>
-
<version>2.1.0</version>
-
</dependency>
-
</dependencies>
- </project>
-
import java.util.Arrays;
-
-
import org.apache.spark.SparkConf;
-
import org.apache.spark.api.java.JavaPairRDD;
-
import org.apache.spark.api.java.JavaRDD;
-
import org.apache.spark.api.java.JavaSparkContext;
-
-
import scala.Tuple2;
-
-
public class SparkTest {
-
-
public static void main(String[] args) {
-
SparkConf conf = new SparkConf().setAppName("Test").setMaster("local");
-
JavaSparkContext sc = new JavaSparkContext(conf);
-
JavaRDD<String> file = sc.parallelize(Arrays.asList("Hello test", "Hello test2", "dds"));
-
JavaRDD<String> words = file.flatMap(s -> Arrays.asList(s.split(" |\t|\n|\r")).iterator());
-
JavaPairRDD<String, Integer> counts = words.mapToPair(s -> new Tuple2<String, Integer>(s, 1));
-
counts = counts.reduceByKey((x, y) -> x + y);
-
-
System.out.println(counts.collect());
-
sc.close();
-
}
-
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10742815/viewspace-2134860/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle exampleOracle
- An example of polybase for OracleOracle
- angular 2 by exampleAngular
- Oracle By Example (OBE)Oracle
- simd example code
- An example about git hookGitHook
- react router animation exampleReact
- An Example of How Oracle WorksOracle
- [Typescript] Query builder exampleTypeScriptUI
- A example that using JQuery clonejQuery
- a simple example for spring AOPSpring
- An Application Context exampleAPPContext
- 【RabbitMQ】direct type exchange example in golangMQGolang
- 【RabbitMQ】topic type exchange example in golangMQGolang
- 【RabbitMQ】fanout type exchange example in golangMQGolang
- openni example NiViewer opengl blockViewBloC
- Common threads: Awk by examplethread
- Amazing Algorithms with NoSQL: A MongoDB ExampleSQLMongoDB
- JDBC Connection Pool Example (轉)JDBC
- Bitmap Index Example (223)Index
- Example: How to Packetize a TCP Stream (轉)TCP
- For example, when you want to get the ball to the ground
- DB2 PL/SQL Example: RunstatsDB2SQL
- Spring 3 MVC hello world exampleSpringMVC
- Spark之spark shellSpark
- 【Spark篇】---Spark初始Spark
- vue.js axios call api exampleVue.jsiOSAPI
- [轉]Angular 4|5 Material Dialog with ExampleAngular
- DB2 PL/SQL Example: Sleep ProcedureDB2SQL
- DNS and DHCP Setup Example for Grid Infrastructure GNSDNSASTStruct
- DBMS_REPAIR example (Doc ID 68013.1)AI
- Amazing Algorithms with NoSQL: A MongoDB Example Part 2SQLMongoDB
- sim3d multiple component example3D
- Spark on Yarn 和Spark on MesosSparkYarn
- Spark系列 - (3) Spark SQLSparkSQL
- Spark學習進度-Spark環境搭建&Spark shellSpark
- Fescar example解析 - TM傳送邏輯
- Java Scheduler ScheduledExecutorService ScheduledThreadPoolExecutor Example(ScheduledThreadPoolExecu...Javathread