• 在 IntelliJ IDEA 中配置 Spark(Java API) 运行环境


    1. 新建Maven项目




    初始Maven项目完成后,初始的配置(pom.xml)如下:

    2. 配置Maven

    向项目里新建Spark Core库

    <?xml version="1.0" encoding="UTF-8"?>
    <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>net.libaoquan</groupId>
        <artifactId>TestSpark</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency> <!-- Spark dependency -->
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-core_2.11</artifactId>
                <version>2.2.1</version>
            </dependency>
        </dependencies>
    </project>
    

    3.新建Java类

    新建Java类,写入Spark(Java API)代码:

    import org.apache.spark.api.java.*;
    import org.apache.spark.SparkConf;
    import org.apache.spark.api.java.function.Function;
    
    public class TestSparkJava {
        public static void main(String[] args) {
            String logFile = "D:\ab.txt";
            SparkConf conf = new SparkConf().setMaster("local").setAppName("TestSpark");
            JavaSparkContext sc = new JavaSparkContext(conf);
    
            JavaRDD<String> logData = sc.textFile(logFile).cache();
    
            long numAs = logData.filter(new Function<String, Boolean>() {
                public Boolean call(String s) { return s.contains("0"); }
            }).count();
    
            long numBs = logData.filter(new Function<String, Boolean>() {
                public Boolean call(String s) { return s.contains("1"); }
            }).count();
    
            System.out.println("Lines with 0: " + numAs + ", lines with 1: " + numBs);
    
            sc.stop();
        }
    }
    

    运行项目,结果如下:

  • 相关阅读:
    单元测试
    英语学习app案列分析
    基于GUI的四则运算
    个人作业1——四则运算题目生成程序(基于控制台)
    一种新体验———构建之法
    个人作业3——个人总结(Alpha阶段)
    单元测试
    英语学习APP案例分析
    结对编程(70,73)
    软件工程HW1-四则运算软件
  • 原文地址:https://www.cnblogs.com/libaoquan/p/9004531.html
Copyright © 2020-2023  润新知