@Override public void saveOrUpdate(Collection<User> users) { if (users == null || users.size() == 0) { return; } HashMap<Long, User> uid2user = new HashMap<Long, User>(); Collection<Long> uids = new ArrayList<Long>(); for (User user : users) { uid2user.put(user.getUid(), user); uids.add(user.getUid()); } List<User> existsUsers = createQuery().filter("uid in", uids).asList(); HashSet<Long> existsUids = new HashSet<Long>(); List<User> newUsers = new ArrayList<User>(); for (User existsUser : existsUsers) { existsUids.add(existsUser.getUid()); User updateUser = uid2user.get(existsUser.getUid()); existsUser.setScreenName(updateUser.getScreenName()); existsUser.setName(updateUser.getName()); existsUser.setProfileImageUrl(updateUser.getProfileImageUrl()); existsUser.setFollowersSize(updateUser.getFollowersSize()); existsUser.setTweetsSize(updateUser.getTweetsSize()); existsUser.setDetail(updateUser.getDetail()); newUsers.add(existsUser); updateUser.setId(existsUser.getId()); } for (User user : users) { if (!existsUids.contains(user.getUid())) { newUsers.add(user); } } ds.save(newUsers); }
@Override public void saveOrUpdate(User user) { Query<User> q = createQuery().filter("uid", user.getUid()); User pUser = q.get(); if (pUser == null) save(user); else { UpdateOperations<User> uo = createUpdateOperations(); uo.set("screenName", user.getScreenName()); uo.set("name", user.getName()); uo.set("profileImageUrl", user.getProfileImageUrl()); uo.set("followersSize", user.getFollowersSize()); uo.set("tweetsSize", user.getTweetsSize()); uo.set("detail", user.getDetail()); update(q, uo); user.setId(pUser.getId()); } }
public static void main(String[] args) throws IOException { Injector injector = Guice.createInjector(new MyModule()); Datastore ds = injector.getInstance(Datastore.class); UserDao userDao = injector.getInstance(UserDao.class); FetchInfoDao fiDao = new FetchInfoDao(ds); ArrayList<FetchInfo> fis = new ArrayList<FetchInfo>(); InputStream is = ClassLoader.getSystemResourceAsStream("sina_data.txt"); BufferedReader r = new BufferedReader(new InputStreamReader(is)); String line = r.readLine(); while (line != null) { String[] fields = line.split("\\s+"); if (fields.length < 4 || fields[3].startsWith("http")) { System.out.println("Error :" + line); line = r.readLine(); continue; } try { FetchInfo fi = new FetchInfo(); fi.name = fields[1]; fi.uid = fields[3]; fis.add(fi); } catch (Exception e) { System.err.println(e.getMessage()); } line = r.readLine(); } fiDao.batchUpdate(fis); List<User> leziUsers = userDao.createQuery().filter("isLeziUser", true).asList(); fis = new ArrayList<FetchInfo>(); for (User u : leziUsers) { FetchInfo fi = new FetchInfo(); fi.uid = String.valueOf(u.getUid()); fi.name = u.getScreenName(); fi.score = 1; fis.add(fi); } fiDao.batchUpdate(fis); }