从地图保存到json和redis

时间:2016-04-01 09:28:18

标签: java redis

我试图将一些嵌套信息同时存储在redis服务器和json中。

我希望结构在redis中看起来像这样,然后将值(eid12345)作为键访问。

{
"mCat" : [eid1 : ["123", "234"], eid2 : ["1234", "234"], eid3 : ["2", "0", "1"]]
,
"fCat" : [eid1: ["986", "876"], eid3 : ["a", "hx"], eid31 : ["1"]]

}

json显然需要在双引号内包含所有内容。

{
"mCat" : ["eid1" : ["123", "234"], "eid2" : ["1234", "234"], "eid3" : ["2", "0", "1"]]
    ,
"fCat" : ["eid1": ["986", "876"], "eid3" : ["a", "hx"], "eid31" : ["1"]]

}

这是我的代码:

public static String getAllListsJSON() {
        String query = "MATCH (t:MALECAT) with t match (t)<-[r:CHILD_OF]-(subtag:MALECAT) WITH t,collect(subtag) as subtags return t.eid as eid, t.level as level,  REDUCE(relations = \"\", rel IN subtags| relations + \",\" + rel.eid) AS relations;";

        Iterable<Map<String, Object>> itr = Neo4j.queryLagData(query, null);

        ConcurrentHashMap<String, Object> mapOfMaleCatChildren = new ConcurrentHashMap<String, Object>();

for (Map map : itr) {
            String tagID = (String) map.get("eid");
            String[] immediateChildren = ((String) map.get("relations")).split(",");
            List<String> listOfImmediateChildren = new ArrayList<>();
            for (String eachChildTagEID : immediateChildren) {
                if (!"".equals(eachChildTagEID)) {
                    listOfImmediateChildren.add(eachChildTagEID);
                }
            }
            Collections.sort(listOfImmediateChildren);
            mapOfMaleCatChildren.put(tagID, new JSONArray(listOfImmediateChildren));
        }
        RedisCacheManager.setWithInfiniteRedisTTL(maleCatListsKey, mapOfMaleCatChildren);
        return mapOfMaleCatChildren.toString();
    }

请建议我如何将eids用作哈希值,同时以正确的形式保存json。

0 个答案:

没有答案