【Spark学习笔记】1、Java-Maven-Spark项目环境搭建

现在学习个新技术,虽然网上资料多得很,但是质量参差不齐!恕在下愚昧,实在想不懂那些都不验证帖子里技术的正确性就直接转载的那些人,心里是怎么想的!你要是感觉帖子好,请默默的收藏到你的收藏夹里,等你确定帖子内容没错的时候你再转载好不好?你不知道你这样做,对这个技术的新手来说,无疑是灾难!你埋那么多坑,是怕别人学会了超越你吗?

哎,言归正传,以下是我自己的学习总结,如果有哪里有错误、有问题,欢迎批评指正,谢谢!

1、使用Eclipse创建一个简单的Maven项目;

2、在pom文件中引入相关依赖

<dependency>
	<groupId>org.apache.spark</groupId>
	<artifactId>spark-core_2.10</artifactId>
	<version>1.6.3</version>
</dependency>
<dependency>
	<groupId>org.apache.hadoop</groupId>
	<artifactId>hadoop-client</artifactId>
	<version>2.7.3</version>
</dependency>
<dependency>
	<groupId>org.apache.hadoop</groupId>
	<artifactId>hadoop-common</artifactId>
	<version>2.7.3</version>
</dependency>
<dependency>
	<groupId>org.apache.hadoop</groupId>
	<artifactId>hadoop-hdfs</artifactId>
	<version>2.7.3</version>
</dependency>

3、在pom文件中设置编码

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

4、在pom文件中设置编译JDK版本

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
			</configuration>
		</plugin>
	</plugins>
</build>

5、写一个spark的filter算子测试代码,看看搭建的环境是否OK

public class FilterOperator {

	public static void main(String[] args) {
		SparkConf conf = new SparkConf().setAppName("FilterOperator").setMaster("local[2]");
		JavaSparkContext sc = new JavaSparkContext(conf);

		List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
		JavaRDD<Integer> numberRDD = sc.parallelize(numbers);

		/**
		 * Return a new dataset formed by selecting those elements of the source on which func returns true.
		 * 
		 * filter算子是过滤,逻辑返回true保留,false就过滤掉
		 */

		JavaRDD<Integer> results = numberRDD.filter(new Function<Integer, Boolean>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Boolean call(Integer number) throws Exception {
				return number % 2 == 0;
			}
		});
		results.foreach(new VoidFunction<Integer>() {
			private static final long serialVersionUID = 1L;

			@Override
			public void call(Integer result) throws Exception {
				System.out.println(result);
			}
		});
		sc.close();
	}
}

输出结果:

2
4

猜你喜欢

转载自blog.csdn.net/hellboy0621/article/details/88019096