C# 到 Java 使用相同的 Azure SDK,只是语言不同

C# to Java using the SAME Azure SDK, just different languages

我一直在尝试将此语句从 C# 转换为 JAVA,但运气不佳。是的,我搜索过 Azure、Whosebug 等,但在 java.

中找不到 RADDOCUMENT 的好例子
 UserInfo response = _DocumentClient.ReadDocument<UserInfo>(UriFactory.CreateDocumentUri(_DBConfiguration.DatabaseID, _DBConfiguration.DocumentCollectionId, pPhoneNumber));

据我所知:

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;

我个人没有使用 Java SDK 的经验,但有一点谷歌搜索 turned up this。这个 repo 中有很多使用示例。

您遇到困难的部分是生成文档 URI。在 C# 中,这似乎被一些为您构建字符串的工厂方法抽象了一点,但您可以在链接的示例中看到,在 Java 中,等效项是:

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

因此,在您的情况下,您只需使用 phone 数字字段(或任何此格式的文档 ID 以及您的文档和集合 ID。

就将响应转换为 UserInfo 而言,您可以将响应视为 JSON 字符串并使用您选择的 JSON 反序列化库为您完成这项工作。在 C# 中,我们使用 Newtonsoft,但 Java 中必须有很多选项。试试谷歌搜索 java json deserialize,我相信您一定能找到很多资源。

郑重声明,我没有对您的问题投反对票,但我能理解人们这样做的原因,也许您可​​以将此视为成长的机会。我直接将 documentdb java 键入 google,前两个链接是 GitHub repos full 代码示例。在期望其他人为您进行研究之前,您应该尝试更努力地搜索。

如果只是想知道如何使用Java的Azure DocumentDB SDK访问DocumentDB,可以参考GitHub上的教程Azure Cosmos DB: Build a DocumentDB API app with Java and the Azure portal and the README content of azure-documentdb-java

希望对您有所帮助。如有任何疑问,请随时告诉我。