java - 从 Java 代码连接到 Azure Cosmos DB 时出现 UnknownHostException

标签 java azure azure-cosmosdb

我创建了一个简单的 Java 项目。并将 JAR 添加到 classpath 中。在文件 BulkImport.java 中,我尝试连接到 Azure Cosmos DB。但是,我遇到了以下错误。

Warning : [main] WARN com.microsoft.azure.documentdb.GlobalEndpointManager - Failed to retrieve database account information. com.microsoft.azure.documentdb.DocumentClientException: java.net.UnknownHostException: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server.

BulkImport.java

public class BulkImport {

    //public static final Logger LOGGER = LoggerFactory.getLogger(BulkImport.class);
    public static final String ENDPOINT = "https://<xxxx>.documents.azure.com:443/";
    public static final String PRIMARY_KEY = xxxx
    public static final String DATABASE = "xxxx"
    public static final String CONTAINER = "xxxx";
    public static final String PARTITION_KEY = "/id";
    public static final int THROUGHPUT = 10000;
    public static void main(String args[]) throws Exception {

        executeBulkImport();
        System.out.print("Done");
    }

    public static void executeBulkImport() throws Exception
    {
        ConnectionPolicy connectionPolicy = new ConnectionPolicy();
        connectionPolicy.setMaxPoolSize(1000);

        // Below line is giving the error
        DocumentClient client = new DocumentClient(ENDPOINT, PRIMARY_KEY, connectionPolicy, ConsistencyLevel.Session);  

        DocumentCollection collection = Utilities.createEmptyCollectionIfNotExists(client, DATABASE, CONTAINER, PARTITION_KEY, THROUGHPUT);
        ArrayList<String> list = new ArrayList<String>();
        JSONParser jsonParser = new JSONParser();
        try (FileReader reader = new FileReader("C:\\samplejson.json")) {

            Object obj = jsonParser.parse(reader);

            JSONArray jsonArray  = (JSONArray) obj;
            System.out.println(jsonArray);
            if (jsonArray  != null) {
                int len = jsonArray.size();
                for (int i=0;i<len;i++){
                    list.add(jsonArray.get(i).toString());
                }
            }
            System.out.println(list.get(0));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        client.getConnectionPolicy().getRetryOptions().setMaxRetryWaitTimeInSeconds(30);
        client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(9);

       // Builder pattern
        DocumentBulkExecutor.Builder bulkExecutorBuilder = DocumentBulkExecutor.builder().from(client, DATABASE, CONTAINER,
                collection.getPartitionKey(), 20000);
        try {
            DocumentBulkExecutor bulkExecutor = bulkExecutorBuilder.build();
            client.getConnectionPolicy().getRetryOptions().setMaxRetryWaitTimeInSeconds(0);
            client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(0);
            BulkImportResponse bulkImportResponse = bulkExecutor.importAll(list, false, false, null);
            System.out.println(bulkImportResponse.getNumberOfDocumentsImported());
        } catch (Exception e) {
            e.printStackTrace();
        }
        client.close();
    }
}

请帮我理解这个问题。

最佳答案

我遇到了类似的问题,因为我尝试从本地环境进行连接。设置代理有帮助:

 ConnectionPolicy defaultPolicy = ConnectionPolicy.GetDefault();
 HttpHost proxy = HttpHost.create("your-proxy-host:8080");
 defaultPolicy.setProxy(proxy);
 defaultPolicy.setConnectionMode(ConnectionMode.Gateway);

此代码适用于:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-documentdb</artifactId>
    <version>2.4.7</version>
</dependency>

如果您有其他版本的 azure-java-sdk,您需要找到类似的解决方案来设置代理。

关于java - 从 Java 代码连接到 Azure Cosmos DB 时出现 UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59280517/

相关文章:

java - 无法解析注释参数

java - 制作实例化列表时 <?> 是什么类型?

c# - Azure Cosmos 数据库集合未获取分区键

azure-cosmosdb - 宇宙数据库 : How to apply concurrency while inserting a document (in parallel requests)

用于 10-100MB json 文件的 Azure 存储选项作为 HDInsigt Hadoop/Spark 的源

java - 使用 CDI 生产者会导致不明确的依赖项异常

java - 使数组按升序或降序排列所需的最少更改

Azure Apple 推送通知错误 : 400 Bad Request

python - 仅在 azure ml studio 中使用 python 笔记本创建 azure ml 实验

azure - 如何解决 iOS 版 ANH 中的无效凭据错误?