Source:
public void execute(DatastoreService ds, String[] args) throws Exception { final Query q = new Query("SurveyAssignment"); final PreparedQuery pq = ds.prepare(q); final List<Entity> toBeCreated = new ArrayList<>(); System.out.println("Processing SurveyAssignments"); for (Entity sl : pq.asIterable(FetchOptions.Builder.withChunkSize(500))) { final Long surveyAssignmentId = sl.getKey().getId(); final List<Long> deviceIds = (List<Long>) sl.getProperty("deviceIds"); final Long surveyId = (Long) sl.getProperty("surveyId"); if (deviceIds == null) { continue; } for (Long deviceId : deviceIds) { final Entity dataPointAssignement = getDataPointAssignment(ds, deviceId, surveyAssignmentId, surveyId); if (dataPointAssignement == null) { Entity newDataPointAssignment = new Entity("DataPointAssignment"); List<Long> dataPointIds = new ArrayList<Long>(); dataPointIds.add(new Long(0)); newDataPointAssignment.setProperty("dataPointIds", dataPointIds); newDataPointAssignment.setProperty("deviceId", deviceId); newDataPointAssignment.setProperty("surveyId", surveyId); newDataPointAssignment.setProperty("surveyAssignmentId", surveyAssignmentId); assignEntityProp(sl, newDataPointAssignment, "createUserId"); assignEntityProp(sl, newDataPointAssignment, "lastUpdateUserId"); newDataPointAssignment.setProperty("ancestorIds", null); final Date date = new Date(); newDataPointAssignment.setProperty("createdDateTime", date); newDataPointAssignment.setProperty("lastUpdateDateTime", date); newDataPointAssignment.setProperty("lastUpdateUserId", 0); newDataPointAssignment.setProperty("createUserId", 0); System.out.println("DataPointAssignment to be created: " + newDataPointAssignment); toBeCreated.add(newDataPointAssignment); } } } System.out.println("DataPointAssignment that should be create: " + toBeCreated.size()); if (toBeCreated.size() > 0) { ds.put(toBeCreated); } }

Target:
public void execute(DatastoreService ds, String[] args) throws Exception { final Query q = new Query("SurveyAssignment"); final PreparedQuery pq = ds.prepare(q); final List<Entity> toBeCreated = new ArrayList<>(); System.out.println("Processing SurveyAssignments"); for (Entity sl : pq.asIterable(FetchOptions.Builder.withChunkSize(500))) { final Long surveyAssignmentId = sl.getKey().getId(); final List<Long> deviceIds = (List<Long>) sl.getProperty("deviceIds"); final Long surveyId = (Long) sl.getProperty("surveyId"); if (deviceIds == null) { continue; } for (Long deviceId : deviceIds) { try { final Entity dataPointAssignement = getDataPointAssignment(ds, deviceId, surveyAssignmentId, surveyId); if (dataPointAssignement == null) { Entity newDataPointAssignment = new Entity("DataPointAssignment"); newDataPointAssignment.setProperty("dataPointIds", Collections.singletonList(0L)); newDataPointAssignment.setProperty("deviceId", deviceId); newDataPointAssignment.setProperty("surveyId", surveyId); newDataPointAssignment.setProperty("surveyAssignmentId", surveyAssignmentId); assignEntityProp(sl, newDataPointAssignment, "createUserId"); assignEntityProp(sl, newDataPointAssignment, "lastUpdateUserId"); newDataPointAssignment.setProperty("ancestorIds", null); final Date date = new Date(); newDataPointAssignment.setProperty("createdDateTime", date); newDataPointAssignment.setProperty("lastUpdateDateTime", date); newDataPointAssignment.setProperty("lastUpdateUserId", 0); newDataPointAssignment.setProperty("createUserId", 0); System.out.println("DataPointAssignment to be created: " + newDataPointAssignment); toBeCreated.add(newDataPointAssignment); } } catch (Exception e) { System.out.println("Error trying to process following values, deviceId: " + deviceId + " surveyAssignmentId: " + surveyAssignmentId + " surveyId: " + surveyId); System.out.println(e.getMessage()); } } } System.out.println("DataPointAssignment that should be created: " + toBeCreated.size()); if (toBeCreated.size() > 0) { ds.put(toBeCreated); } }