GAE数据存储区JOIN + GROUP BY

时间:2014-02-24 20:28:52

标签: java google-cloud-datastore

  

注意:我已经在GAE相关帖子中看到了JOIN ,但在我的网站上看到了GROUP BY   要求让我做了一个新的帖子,我已经找到了我自己的解决方案,我想检查它有多好


数据存储区实体属性和类型(数据库)

  1. 实体:PartyEntity 允许将其称为

    • String partyName
    • String partyId
    • BlobKey图像
  2. 实体:InsertEntity 允许将其称为b

    • String partyIdentifier
    • 字符串名称
    • String constituency

  3. 目标

    SELECT * FROM a JOIN b on a.partyId = b.partyIdentifier GROUP BY b.constituency;
    

    下面我将回答我的问题,代码工作精细,建议我做任何改变或更好的方式(我只是一名学生)

1 个答案:

答案 0 :(得分:1)

我制作的解决方案如下建议任何更改或更好的方法

  

// SELECT * FROM a(PartyEntity拥有唯一的partyId)   每个partyId SELECT * FROM b

private List<ListDisplay> initData() {

        DatastoreService datastore = DatastoreServiceFactory
                .getDatastoreService();

        List<ListDisplay> data = new ArrayList<ListDisplay>();

        Query query = new Query("PartyEntity");
        PreparedQuery preparedQuery = datastore.prepare(query);
        List<Entity> entities = preparedQuery.asList(FetchOptions.Builder
                .withDefaults());

        for (Entity entity : entities) {

            String PARTY_ID = (String) entity.getProperty("partyId");

            // fetch from another
            Query query2 = new Query("InsertEntity");
            Filter filterByPartyId = new Query.FilterPredicate(
                    "partyIdentifier", FilterOperator.EQUAL, PARTY_ID);
            query2.setFilter(filterByPartyId);
            PreparedQuery preparedQuery2 = datastore.prepare(query2);

            List<Entity> entities2 = preparedQuery2.asList(FetchOptions.Builder
                    .withDefaults());

            for (Entity entity2 : entities2) {
                ListDisplay display = new ListDisplay();
                display.setPartyId((String) entity.getProperty("partyId"));
                display.setPartyName((String) entity.getProperty("partyName"));
                display.setImage((BlobKey) entity.getProperty("image"));
                display.setName((String) entity2.getProperty("name"));
                display.setConstituency((String) entity2
                        .getProperty("constituency"));
                data.add(display);
            } // end loop entity2

        } // end loop entity

        return data;
    }

  

// GROUP BY选区子句

public HashMap<String, List<ListDisplay>> getData() {
        HashMap<String, List<ListDisplay>> data = new HashMap<>();
        List<ListDisplay> list = initData();

        for (ListDisplay d : list) {
            // if null add
            if (data.get(d.getConstituency()) == null) {
                List<ListDisplay> internal = new ArrayList<>();
                internal.add(d);
                data.put(d.getConstituency(), internal);
            }
            // else modify
            else {
                List<ListDisplay> hashlist = data.get(d.getConstituency());
                hashlist.add(d);
                data.remove(d.getConstituency());
                data.put(d.getConstituency(), hashlist);
            }
        }

        return data;
    }