Java | Maven| IntelliJ |蔚蓝宇宙|构建和运行失败

问题描述 投票:0回答:1

我正在尝试连接 Azure Cosmos 并尝试检索数据,但由于以下问题(编译时间)而失败。

============开始1.POM.xml========================

<?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.beginsecure</groupId>
    <artifactId>CosmosUtility</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-sdk-bom</artifactId>
                <version>1.2.23</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-cosmos</artifactId>
            <version>4.58.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.32</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20240303</version>
        </dependency>

    </dependencies>
</project>

============结束1.POM.xml========================

============启动2.CosmosHelper.java====================

package com.beginsecure;
import com.azure.identity.ChainedTokenCredential;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.ManagedIdentityCredential;
import com.azure.core.credential.TokenCredential;
import com.azure.cosmos.*;
import com.azure.cosmos.CosmosClient;

import com.azure.identity.DefaultAzureCredentialBuilder;
public class CosmosHelper {
    private CosmosAsyncClient client;
    private String databaseName;

    public CosmosHelper(String cosmosEndPoint, String managedIdentity, String dbName) {
        System.out.printf(dbName);
        DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
                .managedIdentityClientId(managedIdentity)
                .build();
        this.client = new CosmosClientBuilder()
                .endpoint(cosmosEndPoint)
                .credential(defaultCredential)
                .buildAsyncClient();
        this.databaseName = dbName;
    }


}

============结束2.CosmosHelper.java====================

============启动3.main.java============================

package com.beginsecure;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args) {
        // Press Alt+Enter with your caret at the highlighted text to see how
        // IntelliJ IDEA suggests fixing it.
        System.out.printf("Hello and welcome!");
        CosmosHelper tst=new CosmosHelper("https://testdb.documents.azure.com:443/","<Synapse-Work-Space-Identity>","ToDoList");

        // Press Shift+F10 or click the green arrow button in the gutter to run the code.
        for (int i = 1; i <= 5; i++) {

            // Press Shift+F9 to start debugging your code. We have set one breakpoint
            // for you, but you can always add more by pressing Ctrl+F8.
            System.out.println("i = " + i);
        }
    }
}

============ 3.main.java结束============================

使用 IntelliJ maven 运行上述项目文件时,Java 代码出现以下错误

java:com.azure.cosmos 包不存在

它应该成功运行,没有错误。

java maven intellij-idea azure-cosmosdb
1个回答
0
投票

我同意你的观点,必须安装并重新加载特定 SDK 支持的所有模块才能与 Azure Cosmos DB 一起使用。

我正在尝试连接 Azure Cosmos 并尝试检索数据,但由于以下问题(编译时间)而失败。

下面是连接 Azure Cosmos DB 并从 Cosmos DB 检索数据的另一种方法。它使用

query
检索数据,如下面的输出所示。确保某些软件包及其版本在
pom.xml
中是否可用。

public static void main(String[] args) {
        CosmosClientBuilder clientBuilder = new CosmosClientBuilder()
                .endpoint(ENDPOINT)
                .key(KEY);

        CosmosClient cosmosClient = clientBuilder.buildClient();

        CosmosDatabase cosmosDatabase = cosmosClient.getDatabase(DATABASE_NAME);
        CosmosContainer cosmosContainer = cosmosDatabase.getContainer(CONTAINER_NAME);

        String query = "SELECT * FROM c"; 

        CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
        options.setQueryMetricsEnabled(true);

        cosmosContainer.queryItems(query, options, CosmosItemProperties.class)
                .byPage()
                .flatMap(cosmosItemFeedResponse -> cosmosItemFeedResponse.getResults().stream())
                .forEach(cosmosItemProperties -> {
                    System.out.println(cosmosItemProperties.toJson());
                });

        cosmosClient.close();
    }

输出:

{
    "id": "1",
    "name": "Sai",
    "age": 10,
    "class": "7th",
},
{
    "id": "2",
    "name": "Pavan",
    "age": 15,
    "class": "10th",
},
{
    "id": "3",
    "name": "Balaji",
    "age": 13,
    "class": "8th",
}
© www.soinside.com 2019 - 2024. All rights reserved.