Intellij 无法识别 sparksession sparkcontext sqlcontext- Spark 版本 2.4.4

Intellij not recognize sparksession sparkcontext sqlcontext- Spark version 2.4.4

我正在使用 spark 和 scala 在 Intellij 中创建 Maven 项目。但是 Intellij 无法识别 sparksession、sparkcontext 和 sqlcontext。 附上图片。

错误:无法解析符号 getsqlcontext 错误:无法解析符号 getSparkContaxt

我对 POM 的理解是项目对象模型或 POM 是 Maven 中的基本工作单元。它是一个 XML 文件,其中包含有关项目的信息以及 Maven 用于构建项目的配置详细信息。

我是否需要更改 POM 属性或依赖项中的某些内容。

我的 POM 文件如下所示。

<?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>com.IPM</groupId>
    <artifactId>lambda</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>spark-lambda</module>
    </modules>

    <properties>
        <spark.version>2.4.4</spark.version>
        <chill.version>0.7.2</chill.version>
        <algebird.version>0.11.0</algebird.version>
        <kafka.clients.version>0.8.2.1</kafka.clients.version>
        <avro.version>1.7.7</avro.version>
        <spark-cassandra-connector.version>1.6.1</spark-cassandra-connector.version>
        <cassandra-driver-core.version>3.0.1</cassandra-driver-core.version>
        <nscala-time.version>2.6.0</nscala-time.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.typesafe</groupId>
            <artifactId>config</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.datastax.spark</groupId>
            <artifactId>spark-cassandra-connector_2.11</artifactId>
            <version>${spark-cassandra-connector.version}</version>
        </dependency>
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-core</artifactId>
            <version>${cassandra-driver-core.version}</version>
        </dependency>

        <dependency>
            <groupId>com.github.nscala-time</groupId>
            <artifactId>nscala-time_2.11</artifactId>
            <version>${nscala-time.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>${kafka.clients.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>

        <dependency>
            <groupId>com.twitter</groupId>
            <artifactId>chill_2.11</artifactId>
            <version>${chill.version}</version>
        </dependency>
        <dependency>
            <groupId>com.twitter</groupId>
            <artifactId>chill-avro_2.11</artifactId>
            <version>${chill.version}</version>
        </dependency>
        <dependency>
            <groupId>com.twitter</groupId>
            <artifactId>algebird-core_2.11</artifactId>
            <version>${algebird.version}</version>
        </dependency>


        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>compile</scope>
            <!-- provided -->
        </dependency>

        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.11</artifactId>
            <version>${kafka.clients.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>compile</scope>
            <!-- provided -->
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>compile</scope>
            <!-- provided -->
        </dependency>


        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-mllib_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>compile</scope>
            <!-- provided -->
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>compile</scope>
            <!-- provided -->
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.8.1</version>
        </dependency>

        <dependency>
            <groupId>org.joda</groupId>
            <artifactId>joda-convert</artifactId>
            <version>1.7</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.1.6</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <artifactSet>
                                <includes>
                                    <include>*:*</include>
                                </includes>
                            </artifactSet>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>reference.conf</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>




</project>

enter image description here

Spark中没有这样的函数,需要正确创建context,像这样(这里是full example):

  def main(args: Array[String]): Unit = {

    val sc = new SparkContext()
    val spark = SparkSession.builder().config(sc.getConf).getOrCreate()
    import spark.implicits._

  ...
  }

2,您的依赖项不正确 - 您正在使用专为 Spark 1.6 设计的 Spark Cassandra Connector 1.6.1,而您正在尝试使用 Spark 2.4.4。

您需要使用 Spark Cassandra Connector 2.5.0(最新)- 它与 Spark 2.4 兼容,并且还具有 a lot of new functionality.

你还需要删除这个依赖:

<dependency>
   <groupId>com.datastax.cassandra</groupId>
   <artifactId>cassandra-driver-core</artifactId>
   <version>${cassandra-driver-core.version}</version>
</dependency>

而且很可能是这样的:

<dependency>
   <groupId>com.typesafe</groupId>
   <artifactId>config</artifactId>
   <version>1.3.0</version>
</dependency>

如果你想在 Kafka 中使用 Spark,你需要使用正确的包,例如:

    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql-kafka-0-10_2.11</artifactId>
      <version>2.4.4</version>
    </dependency>

在这里您可以 find an example 使用 Kafka + Spark + Cassandra。