0

I've created a simple Java project. And added the JARs to the classpath. In the file BulkImport.java, I'm trying to connect to the Azure Cosmos DB. However, I'm running into the following error.

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();
    }
}

Please help me understand the issue.

8
  • Apparently Java can't resolve the host name. Have you tried other ways? Like dig <xxxx>.documents.azure.com? Is there maybe a proxy you have to go through? Quite often the case in corporate environments. Commented Dec 11, 2019 at 7:26
  • How can I use dig? Also, yes I thought about proxy. But I tried with/without proxy, it's giving the same result. Commented Dec 11, 2019 at 7:35
  • dig <the address you'd like to connect to>. Just google it. linode.com/docs/networking/dns/… Commented Dec 11, 2019 at 7:37
  • Check if you can resolve the address you're connecting to manually. Commented Dec 11, 2019 at 7:38
  • I'm on windows so I did nslookup. It is returning back the IP Address! Commented Dec 11, 2019 at 8:17

1 Answer 1

1

I had similar problem because I was trying to connect from an on-prem environment. Setting proxy helped:

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

This code works with:

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

If you have other version of azure-java-sdk you need to find similar solution to set your proxy.

Sign up to request clarification or add additional context in comments.

1 Comment

As of now I just changed the environment. I was trying from a virtual machine and also the version of the eclipse was Neon whereas I switched to other version. However this could be of help if I switch back to the previous environment. Will check, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.