-3

I've been trying to convert this statement from C# to JAVA, without much luck. Yes I've search Azure, stackoverflow, and such, but can't find a good example of READDOCUMENT in java.

 UserInfo response = _DocumentClient.ReadDocument<UserInfo>(UriFactory.CreateDocumentUri(_DBConfiguration.DatabaseID, _DBConfiguration.DocumentCollectionId, pPhoneNumber));

This is as far as I've gotten:

UserInfo returnUserInfo = null;
        try { 

            //todo: document link is incorrect, but reference  pPhoneNumber as key
            ResourceResponse<Document> response = _DocumentClient.readDocument(<<NEED To generate URI>>,null);
            if (response != null) {
                Document returnUserInfoDocument = response.getResource();
                returnUserInfo = <<I have a document, but can't cast it to USERINFO>>;
            }
        }
        catch (DocumentClientException ex) {
            if (!ex.getError().getCode().equals("NotFound")) {
                throw ex;
            }

        }

        return returnUserInfo;
Zhaoxing Lu
  • 6,319
  • 18
  • 41
codeputer
  • 1,987
  • 3
  • 19
  • 45

2 Answers2

0

I don't personally have experience with the Java SDKs but a little bit of googling turned up this. There are plenty of usage examples inside this repo.

The parts you're struggling with are generating the Document URI. It appears that in C# this is abstracted away a bit with some factory methods that build the string for you but you can see in the example linked that in Java the equivalent would be:

String documentLink = String.format("/dbs/%s/colls/%s/docs/%s", databaseName, collectionName, family.getId());
this.client.readDocument(documentLink, new RequestOptions());

So in your case you would just use the phone number field (or whatever your document Id in this format with your document and collection Ids.

As far as converting the response to a UserInfo, you can just treat the responses as a JSON string and use your JSON deserialization library of choice to do the work for you. In C# we use Newtonsoft but there must be plenty of options in Java. Just try googling java json deserialize and I'm sure you'll be able to find lots of resources.

Just for the record I didn't downvote your question, but I can understand why people did and maybe you can take this as an opportunity to grow. I literally typed documentdb java into google and the first two links are GitHub repos full of code samples. You should attempt to search a little bit harder before expecting others to do your research for you.

Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
  • Tks-You sparked a different perspective for me between the SDKs. stackoverflow has lots of "code this for me questions", but my search context was very much different - I was using "Azure JAVA sdk ReadDocument" - which returned a different result set. I should have used a more general search as you did, however, the link you provided shows a technique to determine if a document exists, and manages that via an exception. It does nothing with the return value, which is likely why I dismissed it. However you helped there as well, in ref to JSON, got me on the right track. Thanks for the help! – codeputer Jul 20 '17 at 17:11
0

If you just want to how to use Azure DocumentDB SDK for Java to access the DocumentDB, you can refer to the tutorial Azure Cosmos DB: Build a DocumentDB API app with Java and the Azure portal and the README content of azure-documentdb-java on GitHub.

Hope it helps. Any concern, please feel free to let me know.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Thanks.. rea article but it uses a query over the collection. Im looking for example or clarufication on readdocument method – codeputer Jul 20 '17 at 16:16