简体   繁体   English

如何 map 将 Google 云数据存储实体 (com.google.cloud.datastore.Entity) 提取到自定义 Java object

[英]How to map fetched Google cloud datastore Entity (com.google.cloud.datastore.Entity) to Custom Java object

I'm fetching a list of Datastore Entity (com.google.cloud.datastore.Entity) using below query (com.google.cloud.datastore.Query).我正在使用以下查询 (com.google.cloud.datastore.Query) 获取数据存储实体 (com.google.cloud.datastore.Entity) 的列表。 I'm unable to map the Datastore Entity object to my custom Java object.我无法将数据存储实体 object map 到我的自定义 Java ZA8CFDE6331BD59EB62AC96ZF8911 Any suggestions would be of great help.任何建议都会有很大帮助。 Thank you.谢谢你。

Query<Entity> query = Query.newEntityQueryBuilder()
                .setNamespace("abc")
                .setKind("ca")
                .setFilter(StructuredQuery.PropertyFilter.eq("name", "xyz"))
                .build();

QueryResults<Entity> tasks = datastore.run(query);

for (QueryResults<Entity> it = tasks; it.hasNext(); ) {
            Entity entity = it.next();
            User user = entity.toUser();//How to map to my custom Java class
        }

Not sure what is relation between User and entity in Datastore, and what exactly you want to do, however there is getPropertyMap method ( reference ) in the Datastore Java API.不确定数据存储区中User和实体之间的关系是什么,以及您到底想做什么,但是数据存储区 Java API 中有getPropertyMap方法( 参考)。 The method seems to be ideal for you task as it returns Map<String,Value> .该方法似乎非常适合您的任务,因为它返回Map<String,Value>

All you need to do is to implement new constructor in User class that will create User instance from this Map .您需要做的就是在User class 中实现新的构造函数,它将从此Map创建User实例。

Than the line should look like this: User user = new User(entity.getPropertyMap());该行应该如下所示: User user = new User(entity.getPropertyMap());

I am sure this is just one of tones of ways to do it:)我相信这只是其中一种方法:)

I hope it will help!我希望它会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM