我创建了一个简单的 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/