[英]UnknownHostException while connecting to Azure Cosmos DB from Java code
我創建了一個簡單的Java
項目。 並將JARs
添加到classpath
。 在文件BulkImport.java 中,我嘗試連接到Azure Cosmos DB
。 但是,我遇到了以下錯誤。
警告:[main] WARN com.microsoft.azure.documentdb.GlobalEndpointManager - 無法檢索數據庫帳戶信息。 com.microsoft.azure.documentdb.DocumentClientException:java.net.UnknownHostException:這通常是主機名解析期間的臨時錯誤,意味着本地服務器未收到來自權威服務器的響應。
批量導入.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,您需要找到類似的解決方案來設置您的代理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.