【问题标题】:How to solve "Exception in thread "main" java.lang.Error:Unresolved compilation problems:SparkSession cannot be resolved to a type" in java spark如何解决java spark中的“线程“main”java.lang.Error中的异常:未解决的编译问题:SparkSession无法解析为类型”
【发布时间】:2021-08-02 16:48:13
【问题描述】:

我在 Azure Windows 虚拟机中安装了 “VScode、jdk 8、python 3.8 和 databricks-connect==8.1.*”。之后,我创建了一个 databricks 集群并使用 cmd配置了 Databricks-connect。设置完所有路径变量后,我按照 Java 代码执行:

'''

import java.util.ArrayList;
import java.util.List;
import java.sql.Date;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.*;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;
import org.apache.spark.sql.Dataset;

public class test_{
    public static void main(String[] args) throws Exception {
        SparkSession spark = SparkSession.builder().appName("Temps Demo").config("spark.master", "local").getOrCreate();


        // Create a Spark DataFrame consisting of high and low temperatures
        // by airport code and date.
        StructType schema = new StructType(new StructField[] {
            new StructField("AirportCode", DataTypes.StringType, false, Metadata.empty()),
            new StructField("Date", DataTypes.DateType, false, Metadata.empty()),
            new StructField("TempHighF", DataTypes.IntegerType, false, Metadata.empty()),
            new StructField("TempLowF", DataTypes.IntegerType, false, Metadata.empty()),
        });
}}'''

================================================ ========== 在 VScode 终端内,我得到低于输出 '''

PS C:\Users\gaurav>  & 'c:\Users\gaurav\.vscode\extensions\vscjava.vscode-java-debug-0.35.0\scripts\launcher.bat' 'C:\Program Files\AdoptOpenJDK\jdk-11.0.11.9-hotspot\bin\java.exe' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\gaurav\AppData\Local\Temp\vscodesws_8933a\jdt_ws\jdt.ls-java-project\bin' 'App' 
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
            SparkSession cannot be resolved to a type
            SparkSession cannot be resolved
            StructType cannot be resolved to a type
            StructType cannot be resolved to a type
            StructField cannot be resolved to a type
            StructField cannot be resolved to a type
            StructField cannot be resolved to a type
            DataTypes cannot be resolved to a variable
            Metadata cannot be resolved
            StructField cannot be resolved to a type
            StructField cannot be resolved to a type
            DataTypes cannot be resolved to a variable
            Metadata cannot be resolved
    PS C:\Users\gaurav>  & 'c:\Users\gaurav\.vscode\extensions\vscjava.vscode-java-debug-0.35.0\scripts\launcher.bat' 'C:\Program Files\AdoptOpenJDK\jdk-11.0.11.9-hotspot\bin\java.exe' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\gaurav\AppData\Local\Temp\vscodesws_8933a\jdt_ws\jdt.ls-java-project\bin' 'test_'
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
            SparkSession cannot be resolved to a type
            SparkSession cannot be resolved
            StructType cannot be resolved to a type
            StructType cannot be resolved to a type
            StructField cannot be resolved to a type
            StructField cannot be resolved to a type
            StructField cannot be resolved to a type
            DataTypes cannot be resolved to a variable
            Metadata cannot be resolved
            StructField cannot be resolved to a type
            StructField cannot be resolved to a type
            DataTypes cannot be resolved to a variable
            Metadata cannot be resolved
            StructField cannot be resolved to a type
            StructField cannot be resolved to a type
            DataTypes cannot be resolved to a variable
            Metadata cannot be resolved
            StructField cannot be resolved to a type
            StructField cannot be resolved to a type
            DataTypes cannot be resolved to a variable
            Metadata cannot be resolved
    
            at test_.main(testjava.java:22)

'''

当我在配置 databricks 连接后在 cmd 中运行“databricks-connect 测试”时。我得到以下输出... [1]:https://i.stack.imgur.com/p5WG6.png

【问题讨论】:

    标签: java apache-spark visual-studio-code azure-databricks databricks-connect


    【解决方案1】:

    org.apache.spark.sql.types.StructType 可以在 spark-catalyst 中实现,将 spark-catalyst.jarspark-sql.jar 添加到 Referenced Libraries

    此外,java 扩展需要 jdk11 或最新 版本来支持。

    满足以上条件,我的项目中没有显示StructField cannot be resolved to a type

    【讨论】:

    • 感谢您的宝贵回复。让我检查一下这些改进。 :)
    • @AbhayPratapSingh。如果有帮助,你可以accept and upvote我的回答:)
    • @AbhayPratapSingh。您的问题解决了吗?
    • 否 @Molly 我们放弃了使用 DB connect 的想法,现在寻找其他方式。顺便说一句,谢谢你的帮助。 :D
    猜你喜欢
    • 1970-01-01
    • 2014-05-08
    • 2017-07-16
    • 1970-01-01
    • 2015-09-01
    • 2012-06-28
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多