Java Code Examples for java.util.Map.replace()

The following are Jave code examples for showing how to use replace() of the java.util.Map class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: eagle-oj-api   File: ContestController.java   View Source Code Vote up 6 votes
@ApiOperation("获取比赛列表")
@GetMapping
public ResponseEntity getContests(@RequestParam("page") int page,
                                  @RequestParam("page_size") int pageSize) {
    PageRowBounds pager = new PageRowBounds(page, pageSize);
    Map<String, Object> data = new HashMap<>();
    List<Map<String, Object>> contests = contestService.getValidContests(pager);
    for (Map<String, Object> contest: contests) {
        if (contest.get("password") != null) {
            contest.replace("password", "You can't see it!");
        }
    }
    data.put("data", contests);
    data.put("total", pager.getTotal());
    return new ResponseEntity(data);
}
 
Example 2
Project: MineIDE   File: Translation.java   View Source Code Vote up 6 votes
public boolean setTranslation(final String key, final String lang, final String value)
{
    if (lang == null || lang.isEmpty())
        throw new IllegalArgumentException("Lang not valid");
    if (value == null)
        throw new IllegalArgumentException("Value not valid");

    final Map<String, String> trans = this.getKeyTranslation(key);

    if (!trans.keySet().contains(lang))
        throw new IllegalArgumentException("Lang not valid");

    if (trans.get(lang).equals(value))
        return false;

    return trans.replace(lang, trans.get(lang), value);
}
 
Example 3
Project: Squid   File: AbstractTopsoilPlot.java   View Source Code Vote up 5 votes
public void setSelectedAllData(boolean selected) {
    for (Map<String, Object> datum : plot.getData()) {
        datum.replace("Selected", selected);
    }

    plot.setData(plot.getData());
}
 
Example 4
Project: openjdk-jdk10   File: InPlaceOpsCollisions.java   View Source Code Vote up 5 votes
@Test(dataProvider = "mapsWithObjectsAndStrings")
void testReplaceIfMapped(String desc, Supplier<Map<Object, Object>> ms, Object val) {
    // remove odd keys
    // call replace for all keys[]
    // odd keys should remain absent, even keys should be mapped to EXTRA, no value from keys[] should be in map
    Map<Object, Object> map = ms.get();
    Object[] keys = map.keySet().toArray();
    int expectedSize1 = 0;
    removeOddKeys(map, keys);
    int expectedSize2 = map.size();

    for (int i = 0; i < keys.length; i++) {
        Object retVal = map.replace(keys[i], val);
        if (i % 2 == 0) { // even: still in map, should be replaced
            assertEquals(retVal, keys[i],
                    String.format("replaceIfMapped: retVal(%s[%d])", desc, i));
            assertEquals(val, map.get(keys[i]),
                    String.format("replaceIfMapped: get(%s[%d])", desc, i));
            assertTrue(map.containsKey(keys[i]),
                    String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));
            expectedSize1++;
        } else { // odd: was removed, should not be replaced
            assertNull(retVal,
                    String.format("replaceIfMapped: retVal(%s[%d])", desc, i));
            assertNull(map.get(keys[i]),
                    String.format("replaceIfMapped: get(%s[%d])", desc, i));
            assertFalse(map.containsKey(keys[i]),
                    String.format("replaceIfMapped: containsKey(%s[%d])", desc, i));
        }
        assertFalse(map.containsValue(keys[i]),
                String.format("replaceIfMapped: !containsValue(%s[%d])", desc, i));
    }
    assertTrue(map.containsValue(val),
            String.format("replaceIfMapped: containsValue(%s[%s])", desc, val));
    assertEquals(map.size(), expectedSize1,
            String.format("map expected size#1 m%d != k%d", map.size(), expectedSize1));
    assertEquals(map.size(), expectedSize2,
            String.format("map expected size#2 m%d != k%d", map.size(), expectedSize2));

}
 
Example 5
Project: eagle-oj-api   File: ContestService.java   View Source Code Vote up 5 votes
private void checkContestValid(Map<String, Object> contest) {
    if ((int)contest.get("status") == 1) {
        BigInteger bigInteger = (BigInteger)contest.get("end_time");
        if (bigInteger.longValue()<System.currentTimeMillis()) {
            Long cid = (Long)contest.get("cid");
            int official = (int) contest.get("official");
            closeContest(cid.intValue(), official);
            contest.replace("status", 2);
        }
    }
}
 
Example 6
Project: openjdk-jdk10   File: InPlaceOpsCollisions.java   View Source Code Vote up 5 votes
@Test(dataProvider = "mapsWithObjectsAndStrings")
void testReplaceOldValue(String desc, Supplier<Map<Object, Object>> ms, Object val) {
    // remap odds to val
    // call replace to replace for val, for all keys
    // check that all keys map to value from keys array
    Map<Object, Object> map = ms.get();
    Object[] keys = map.keySet().toArray();
    boolean replaced;
    remapOddKeys(map, keys, val);

    for (int i = 0; i < keys.length; i++) {
        replaced = map.replace(keys[i], val, keys[i]);
        if (i % 2 == 0) { // even: original mapping, should not be replaced
            assertFalse(replaced,
                    String.format("replaceOldValue: retVal(%s[%d])", desc, i));
        } else { // odd: new mapping, should be replaced
            assertTrue(replaced,
                    String.format("replaceOldValue: get(%s[%d])", desc, i));
        }
        assertEquals(keys[i], map.get(keys[i]),
                String.format("replaceOldValue: get(%s[%d])", desc, i));
        assertTrue(map.containsKey(keys[i]),
                String.format("replaceOldValue: containsKey(%s[%d])", desc, i));
        assertTrue(map.containsValue(keys[i]),
                String.format("replaceOldValue: containsValue(%s[%d])", desc, i));
    }
    assertFalse(map.containsValue(val),
            String.format("replaceOldValue: !containsValue(%s[%s])", desc, val));
    assertEquals(map.size(), keys.length,
            String.format("map expected size m%d != k%d", map.size(), keys.length));
}
 
Example 7
Project: jpl-framework   File: EvaluationsOutputGenerator.java   View Source Code Vote up 5 votes
/**
 * Returns one row of losses for {@link IMetric}s for all the {@link ILearningAlgorithm}s
 * evaluated for one {@link DatasetFile} to be printed for in the console in from of a string.
 * 
 * @param file the {@link DatasetFile}
 * @param datasetFileIndex the index of the dataset file in the list of {@link DatasetFile}s.
 * @param valuesColumnWidth the column width
 * @param firstColumnWidth the first column width
 * @param totalWidth the total width of the table
 * @return the string output to be printed for all the algorithms
 */
private String getLearningAlgorithmsMetricResultsForOneDatasetFile(IDataset<?, ?, ?> dataset, int datasetFileIndex,
      int valuesColumnWidth, int firstColumnWidth, int totalWidth) {
   StringBuilder generatedEvaluationOutput = new StringBuilder();
   generatedEvaluationOutput.append(StringUtils.LINE_BREAK);
   generatedEvaluationOutput.append(StringUtils.repeat(StringUtils.DASH, totalWidth));
   generatedEvaluationOutput.append(StringUtils.LINE_BREAK);

   generatedEvaluationOutput.append(getCenteredString(DATASET, firstColumnWidth));
   generatedEvaluationOutput.append(getCenteredString(dataset.getDatasetFile().getFile().getName(), totalWidth - firstColumnWidth));
   generatedEvaluationOutput.append(StringUtils.LINE_BREAK);
   for (IMetric<?, ?> metric : metrics) {
      generatedEvaluationOutput.append(getCenteredString(metric.toString(), firstColumnWidth));
      for (ILearningAlgorithm learningAlgorithm : learningAlgorithms) {
         EvaluationResult result = getEvaluationResultForSet(learningAlgorithm, dataset);
         Map<IMetric<?, ?>, double[]> metricsWithLossArray = meanAndStandardDeviation.get(learningAlgorithm);
         if (result != null) {
            double[] losses = metricsWithLossArray.get(metric);
            losses[datasetFileIndex] = (double) result.getLossForMetric(metric);
            metricsWithLossArray.replace(metric, losses);
            generatedEvaluationOutput.append(getCenteredString(String.valueOf(result.getLossForMetric(metric)), valuesColumnWidth));
         } else {
            generatedEvaluationOutput.append(getCenteredString(NOT_EVALUATED, valuesColumnWidth));
         }
         meanAndStandardDeviation.replace(learningAlgorithm, metricsWithLossArray);
      }
      generatedEvaluationOutput.append(StringUtils.LINE_BREAK);
   }
   generatedEvaluationOutput.append(getEvaluationStringResult(dataset, valuesColumnWidth, firstColumnWidth));
   generatedEvaluationOutput.append(getCenteredString(StringUtils.EMPTY_STRING, firstColumnWidth));
   for (int i = 0; i < learningAlgorithms.size(); i++) {
      generatedEvaluationOutput.append(getCenteredString(StringUtils.EMPTY_STRING, valuesColumnWidth));
   }
   return generatedEvaluationOutput.toString();
}
 
Example 8
Project: cakes   File: MapNewFuturesDemo.java   View Source Code Vote up 5 votes
/**
 * 新增
 * replace(key,value)
 * replace(key,oldValue,newValue)
 * replaceAll(BiFunction fun)
 */
@Test
public void testReplace() {
    Map<String, String> hashMap = Maps.newHashMap();
    hashMap.put("k1", "v1");
    hashMap.put("k3", "v3");
    hashMap.put("k6", "v6");

    // replace 替换指定key的value
    String replaceValue = hashMap.replace("k6", "haha6");
    System.out.println("map replace key k6 then map=" + hashMap + ", and the return value=" + replaceValue);

    System.out.println("\n-------------------------------------------\n");

    // replace 替换指定key和oldValue的数据,如果key和oldValue对应不上则替换失败
    boolean isReplaceOldValue = hashMap.replace("k6", "what?", "heng");
    System.out.println("replace map by key and oldValue first try isReplaceOldValue=" + isReplaceOldValue);
    boolean isReplaceOldValue2 = hashMap.replace("k6", "haha6", "heng");
    System.out.println("replace map by key and oldValue second try isReplaceOldValue2=" + isReplaceOldValue2);

    System.out.println("\n-------------------------------------------\n");

    // replaceAll, 根据给定的key和value计算要替换的数据
    hashMap.replaceAll((k, v) -> {
        if (k.equals("k3") && v.equals("v3")) {
            return "hahahahaha";
        }
        return v;
    });

    System.out.println("biMap replaceAll = " + hashMap);
}
 
Example 9
Project: popular-movie-store   File: ShoppingCartController.java   View Source Code Vote up 5 votes
/**
 * @param movieId
 * @param qty
 * @param session
 * @return
 */
@GetMapping("/cart/add")
public @ResponseBody
String addItemToCart(@RequestParam("movieId") String movieId, @RequestParam("quantity") int qty,
                     HttpSession session) {

    MovieCart movieCart;

    if (session.getAttribute(SESSION_ATTR_MOVIE_CART) == null) {
        log.info("No Cart Exists for the session, creating one");
        movieCart = new MovieCart();
        movieCart.setOrderId(UUID.randomUUID().toString());
    } else {
        log.info("Cart Exists for the session, will be updated");
        movieCart = (MovieCart) session.getAttribute(SESSION_ATTR_MOVIE_CART);
    }

    log.info("Adding/Updating {} with Quantity {} to cart ", movieId, qty);

    Map<String, Integer> movieItems = movieCart.getMovieItems();

    if (movieItems.containsKey(movieId)) {
        movieItems.replace(movieId, qty);
    } else {
        movieItems.put(movieId, qty);
    }

    log.info("Movie Cart:{}", movieCart);

    //update the session back
    session.setAttribute(SESSION_ATTR_MOVIE_CART, movieCart);

    return String.valueOf(movieCart.getMovieItems().size());
}
 
Example 10
Project: drinkwater-java   File: RoutingTest.java   View Source Code Vote up 4 votes
@Test
public void shouldRouteCorrectly() throws Exception {

    try (DrinkWaterApplication app = DrinkWaterApplication.create("routing-test",
            options().use(RoutingApplicationFromConfigFile.class).autoStart())) {

        String port = (String)app.getServiceProperty("frontService", RestService.REST_PORT_KEY);

        String frontHost = String.format("http://localhost:%s/frontService/data", port);
        Map<String, String> headers = new HashMap<>();

        //Route to A
        headers.put("ROUTINGHEADER", "A");
        String result = httpGetString(frontHost, headers).result();

        assertEquals("propertyFromA", result);

        //Route to B
        headers.replace("ROUTINGHEADER", "B");
        result = httpGetString(frontHost, headers).result();
        assertEquals("propertyFromB", result);

        //Route to C
        headers.replace("ROUTINGHEADER", "C");
        result = httpGetString(frontHost, headers).result();
        assertEquals("propertyFromC", result);

        //Route to B again
        headers.replace("ROUTINGHEADER", "B");
        result = httpGetString(frontHost, headers).result();
        assertEquals("propertyFromB", result);

        //Route with no headers => default
        result = httpGetString(frontHost).result();
        assertEquals("propertyFromdefault", result);

        //Route to B again with Options
        headers.replace("ROUTINGHEADER", "B");
        result = httpOptions(frontHost, headers).result();
        assertEquals("OK", result);

        //Route to SubRouting to Y
        headers.replace("ROUTINGHEADER", "sub");
        headers.put("SUBROUTINGHEADER", "Y");
        result = httpGetString(frontHost, headers).result();
        assertEquals("propertyFromY", result);

        //Route to SubRouting to X
        headers.replace("ROUTINGHEADER", "sub");
        headers.replace("SUBROUTINGHEADER", "X");
        result = httpGetString(frontHost, headers).result();
        assertEquals("propertyFromX", result);

        //Route to SubRouting to default
        headers.replace("ROUTINGHEADER", "sub");
        headers.remove("SUBROUTINGHEADER");
        result = httpGetString(frontHost, headers).result();
        assertEquals("propertyFromdefault", result);

    }
}
 
Example 11
Project: jpl-framework   File: RankingRegressionTransformerUtils.java   View Source Code Vote up 4 votes
/**
 * The function to transform the part of the provided object ranking dataset to base learner
 * dataset so that linear regression model can be learned on it. This function transform each
 * rank of each object to expected rank of the object and add pair of the expected rank and
 * feature value of the item and the context under which it is ranked in the dataset.
 * 
 * @param objectRankingDataset the dataset to be transformed
 * @return the transformed base learner dataset
 */
public static BaselearnerDataset transformDatasetToBaseLearnerRegressionDataset(ObjectRankingDataset objectRankingDataset) {
   Map<Integer, ArrayList<Pair<Double, double[]>>> objectAndExpectedRanks = new HashMap<>();
   int numberOfInstances = 0;

   for (int i = 0; i < objectRankingDataset.getNumberOfInstances(); i++) {
      Ranking ranking = objectRankingDataset.getInstance(i).getRating();
      int numOfObjectsInPartialOrder = ranking.getObjectList().length;
      double[] contextVector = objectRankingDataset.getInstance(i).getContextFeatureVector();
      double rank = 1;
      for (int obj : ranking.getObjectList()) {
         double expectedRank = rank / (numOfObjectsInPartialOrder + 1);
         ArrayList<Pair<Double, double[]>> expectedRanks = new ArrayList<>();
         if (!objectAndExpectedRanks.containsKey(obj)) {
            expectedRanks.add(Pair.of(expectedRank, contextVector));
            objectAndExpectedRanks.put(obj, expectedRanks);
         } else {
            expectedRanks = (ArrayList<Pair<Double, double[]>>) CollectionsUtils.getDeepCopyOf(objectAndExpectedRanks.get(obj));
            expectedRanks.add(Pair.of(expectedRank, contextVector));
            objectAndExpectedRanks.replace(obj, expectedRanks);
         }
         rank++;
         numberOfInstances++;
      }
   }

   BaselearnerDataset transformedDataset = new BaselearnerDataset(numberOfInstances,
         objectRankingDataset.getNumofItemFeatures() + objectRankingDataset.getNumofContextFeatures());
   Iterator<Entry<Integer, ArrayList<Pair<Double, double[]>>>> iterator = objectAndExpectedRanks.entrySet().iterator();
   while (iterator.hasNext()) {
      Entry<Integer, ArrayList<Pair<Double, double[]>>> entry = iterator.next();
      int object = entry.getKey();
      for (Pair<Double, double[]> rankAndContextVector : entry.getValue()) {
         double[] features = ArrayUtils.addAll(rankAndContextVector.getSecond(), objectRankingDataset.getItemVector(object));
         BaselearnerInstance instance = new BaselearnerInstance(features, rankAndContextVector.getFirst());
         transformedDataset.addInstance(instance);
      }
   }
   return transformedDataset;

}
 
Example 12
Project: connectors   File: SqlStoredConnectorVerifierExtension.java   View Source Code Vote up 4 votes
@Override
protected Result verifyParameters(Map<String, Object> parameters) {
    ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS)
        .error(ResultErrorHelper.requiresOption("url", parameters))
        .error(ResultErrorHelper.requiresOption("user", parameters))
        .error(ResultErrorHelper.requiresOption("password", parameters));

    if (builder.build().getErrors().isEmpty()) {
        try (Connection connection = 
            DriverManager.getConnection(
                    parameters.get("url").toString(), 
                    String.valueOf(parameters.get("user")), 
                    String.valueOf(parameters.get("password")))) {
            // just try to get the connection
        } catch (SQLException e) {
            final Map<String, Object> redacted = new HashMap<>(parameters);
            redacted.replace("password", "********");
            LOG.warn("Unable to connecto to database with parameters {}, SQLSTATE: {}, error code: {}",
                redacted, e.getSQLState(), e.getErrorCode(), e);

            final String sqlState = e.getSQLState();
            if (sqlState == null || sqlState.length() < 2) {
                unsupportedDatabase(builder);
            } else {
                switch (sqlState.substring(0, 2)) {
                case "28":
                    builder.error(ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, e.getMessage())
                        .parameterKey("user")
                        .parameterKey("password")
                        .build());
                    break;
                case "08":
                case "3D":
                    builder.error(ResultErrorBuilder.withCodeAndDescription(
                        VerificationError.StandardCode.ILLEGAL_PARAMETER_VALUE, e.getMessage())
                        .parameterKey("url")
                        .build());
                    break;
                default:
                    builder.error(ResultErrorBuilder.withCodeAndDescription(
                        VerificationError.StandardCode.GENERIC, e.getMessage())
                        .build());
                    break;
                }
            }
        }
    }
    return builder.build();
}
 
Example 13
Project: database-transform-tool   File: ManageTable.java   View Source Code Vote up 4 votes
/**
	 * @decription 数据同步(Cassandra-->Cassandra)
	 * @author yi.zhang
	 * @time 2017年8月4日 下午5:26:59
	 * @param source	数据源
	 * @param target	目标库
	 * @param mapper	表映射
	 * @param filter_columns	字段过滤
	 */
	protected void cassandra2cassandra(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){
		if(source==null||target==null){
			return;
		}
		CassandraFactory factory = new CassandraFactory();
		factory.init(source.getServers(), source.getKeyspace(), source.getUsername(), source.getPassword());
		Map<String,String> mapping = new HashMap<String,String>();
		if(mapper==null||mapper.size()==0){
			List<String> tables = factory.queryTables();
			for (String table : tables) {
				mapping.put(table, table);
			}
		}else{
			mapping = mapper;
		}
		CassandraFactory tfactory = new CassandraFactory();
		tfactory.init(target.getServers(), target.getKeyspace(), target.getUsername(), target.getPassword());
		for(String stable : mapping.keySet()){
			String ttable = mapping.get(stable);
//			if(!(stables.contains(stable)&&ttables.contains(ttable))){
//				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]不存在--");
//				continue;
//			}
			Map<String,String> reflect = new LinkedHashMap<String,String>();
			Map<String, String> scolumns = factory.queryColumns(stable);
			Map<String, String> tcolumns = tfactory.queryColumns(ttable);
			if(scolumns==null||scolumns.isEmpty()||tcolumns==null||tcolumns.isEmpty()){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无合适字段--");
				continue;
			}
			for(String scolumn:scolumns.keySet()){
				String s_column = scolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
				if(filter_columns!=null&&(filter_columns.contains(scolumn)||filter_columns.contains(s_column))){
					continue;
				}
				for(String tcolumn:tcolumns.keySet()){
					String t_column = tcolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
					if(filter_columns!=null&&(filter_columns.contains(tcolumn)||filter_columns.contains(t_column))){
						continue;
					}
					if(scolumn.equalsIgnoreCase(tcolumn)||scolumn.equalsIgnoreCase(t_column)||s_column.equalsIgnoreCase(tcolumn)||s_column.equalsIgnoreCase(t_column)){
						reflect.put(scolumn, tcolumn);
					}
				}
			}
			if(reflect.isEmpty()){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无对应字段--");
				continue;
			}
			String cql = "select "+StringUtils.join(reflect.keySet(), ",")+" from "+stable;
			List<?> datas = factory.executeQuery(cql, null);
			System.out.println("--目标表["+stable+"]数据量:"+datas.size());
			for (Object data : datas) {
				Map<String,Object> tdata = new LinkedHashMap<String,Object>();
				JSONObject json = (JSONObject)data;
				for(String key:json.keySet()){
					Object value = json.get(key);
					if(value instanceof Date){
						value = DateUtil.formatDateTimeStr((Date)value);
					}
					if(value instanceof String){
						value = "\""+json.getString(key)+"\"";
					}
					tdata.replace(reflect.get(key), value);
				}
//				tfactory.save(tdata);
				String sql = "insert into "+ttable+"("+StringUtils.join(tdata.keySet(), ",")+")values("+StringUtils.join(tdata.values(), ",")+")";
				tfactory.executeUpdate(sql);
			}
		}
	}
 
Example 14
Project: database-transform-tool   File: ManageTable.java   View Source Code Vote up 4 votes
/**
	 * @decription 数据同步(Cassandra-->MongoDB)
	 * @author yi.zhang
	 * @time 2017年8月4日 下午5:26:59
	 * @param source	数据源
	 * @param target	目标库
	 * @param mapper	表映射
	 * @param filter_columns	字段过滤
	 */
	protected void cassandra2mongodb(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){
		if(source==null||target==null){
			return;
		}
		CassandraFactory factory = new CassandraFactory();
		factory.init(source.getServers(), source.getKeyspace(), source.getUsername(), source.getPassword());
		Map<String,String> mapping = new HashMap<String,String>();
		if(mapper==null||mapper.size()==0){
			List<String> tables = factory.queryTables();
			for (String table : tables) {
				mapping.put(table, table);
			}
		}else{
			mapping = mapper;
		}
		MongoDBFactory tfactory = new MongoDBFactory();
		tfactory.init(target.getServers(), target.getDatabase(), target.getSchema(), target.getUsername(), target.getPassword());
//		List<String> stables = factory.queryTables();
		List<String> ttables = tfactory.queryTables();
		for(String stable : mapping.keySet()){
			String ttable = mapping.get(stable);
			if(!(ttables.contains(ttable))){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]不存在--");
				continue;
			}
			Map<String,String> reflect = new LinkedHashMap<String,String>();
			Map<String, String> scolumns = factory.queryColumns(stable);
			Map<String, String> tcolumns = tfactory.queryColumns(ttable);
			if(scolumns==null||scolumns.isEmpty()||tcolumns==null||tcolumns.isEmpty()){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无合适字段--");
				continue;
			}
			for(String scolumn:scolumns.keySet()){
				String s_column = scolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
				if(filter_columns!=null&&(filter_columns.contains(scolumn)||filter_columns.contains(s_column))){
					continue;
				}
				for(String tcolumn:tcolumns.keySet()){
					String t_column = tcolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
					if(filter_columns!=null&&(filter_columns.contains(tcolumn)||filter_columns.contains(t_column))){
						continue;
					}
					if(scolumn.equalsIgnoreCase(tcolumn)||scolumn.equalsIgnoreCase(t_column)||s_column.equalsIgnoreCase(tcolumn)||s_column.equalsIgnoreCase(t_column)){
						reflect.put(scolumn, tcolumn);
					}
				}
			}
			if(reflect.isEmpty()){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无对应字段--");
				continue;
			}
			String cql = "select "+StringUtils.join(reflect.keySet(), ",")+" from "+stable;
			List<?> datas = factory.executeQuery(cql, null);
			System.out.println("--目标表["+stable+"]数据量:"+datas.size());
			for (Object data : datas) {
				Map<String,Object> tdata = new LinkedHashMap<String,Object>();
				JSONObject json = (JSONObject)data;
				for(String key:json.keySet()){
					Object value = json.get(key);
					if(!key.matches("(\\w+)")){
						continue;
					}
					tdata.replace(reflect.get(key), value);
				}
				tfactory.save(ttable, tdata);
			}
		}
	}
 
Example 15
Project: database-transform-tool   File: ManageTable.java   View Source Code Vote up 4 votes
/**
	 * @decription 数据同步(MongoDB-->Cassandra)
	 * @author yi.zhang
	 * @time 2017年8月4日 下午5:26:59
	 * @param source	数据源
	 * @param target	目标库
	 * @param mapper	表映射
	 * @param filter_columns	字段过滤
	 */
	protected void mongodb2cassandra(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){
		if(source==null||target==null){
			return;
		}
		MongoDBFactory factory = new MongoDBFactory();
		factory.init(source.getServers(), source.getDatabase(), source.getSchema(), source.getUsername(), source.getPassword());
		Map<String,String> mapping = new HashMap<String,String>();
		if(mapper==null||mapper.size()==0){
			List<String> tables = factory.queryTables();
			for (String table : tables) {
				mapping.put(table, table);
			}
		}else{
			mapping = mapper;
		}
		CassandraFactory tfactory = new CassandraFactory();
		tfactory.init(target.getServers(), target.getKeyspace(), target.getUsername(), target.getPassword());
		List<String> stables = factory.queryTables();
//		List<String> ttables = tfactory.queryTables();
		for(String stable : mapping.keySet()){
			String ttable = mapping.get(stable);
			tfactory.queryColumns(ttable);
			if(!(stables.contains(stable))){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]不存在--");
				continue;
			}
			Map<String,String> reflect = new LinkedHashMap<String,String>();
			Map<String, String> scolumns = factory.queryColumns(stable);
			Map<String, String> tcolumns = tfactory.queryColumns(ttable);
			if(scolumns==null||scolumns.isEmpty()||tcolumns==null||tcolumns.isEmpty()){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无合适字段--");
				continue;
			}
			for(String scolumn:scolumns.keySet()){
				String s_column = scolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
				if(filter_columns!=null&&(filter_columns.contains(scolumn)||filter_columns.contains(s_column))){
					continue;
				}
				for(String tcolumn:tcolumns.keySet()){
					String t_column = tcolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
					if(filter_columns!=null&&(filter_columns.contains(tcolumn)||filter_columns.contains(t_column))){
						continue;
					}
					if(scolumn.equalsIgnoreCase(tcolumn)||scolumn.equalsIgnoreCase(t_column)||s_column.equalsIgnoreCase(tcolumn)||s_column.equalsIgnoreCase(t_column)){
						reflect.put(scolumn, tcolumn);
					}
				}
			}
			if(reflect.isEmpty()){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无对应字段--");
				continue;
			}
			List<?> datas = factory.executeQuery(stable, null, null);
			System.out.println("--数据表["+stable+"]数据量:"+datas.size());
			for (Object data : datas) {
				Map<String,Object> tdata = new LinkedHashMap<String,Object>();
				JSONObject json = (JSONObject)data;
				for(String key:json.keySet()){
					Object value = json.get(key);
					if(value instanceof Date){
						value = DateUtil.formatDateTimeStr((Date)value);
					}
					if(value instanceof String){
						value = "\""+json.getString(key)+"\"";
					}
					tdata.replace(reflect.get(key), value);
				}
				String sql = "insert into "+ttable+"("+StringUtils.join(tdata.keySet(), ",")+")values("+StringUtils.join(tdata.values(), ",")+")";
				tfactory.executeUpdate(sql);
			}
		}
	}
 
Example 16
Project: database-transform-tool   File: ManageTable.java   View Source Code Vote up 4 votes
/**
 * @decription 数据同步(MongoDB-->Greenplum)
 * @author yi.zhang
 * @time 2017年8月4日 下午5:26:59
 * @param source	数据源
 * @param target	目标库
 * @param mapper	表映射
 * @param filter_columns	字段过滤
 */
protected void mongodb2greenplum(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){
	if(source==null||target==null){
		return;
	}
	MongoDBFactory factory = new MongoDBFactory();
	factory.init(source.getServers(), source.getDatabase(), source.getSchema(), source.getUsername(), source.getPassword());
	Map<String,String> mapping = new HashMap<String,String>();
	if(mapper==null||mapper.size()==0){
		List<String> tables = factory.queryTables();
		for (String table : tables) {
			mapping.put(table, table);
		}
	}else{
		mapping = mapper;
	}
	GreenplumFactory tfactory = new GreenplumFactory();
	tfactory.init(target.getServers(), target.getDatabase(), target.getSchema(), target.getUsername(), target.getPassword(), true, 100, 10);
	List<String> stables = factory.queryTables();
	List<String> ttables = tfactory.queryTables();
	for(String stable : mapping.keySet()){
		String ttable = mapping.get(stable);
		if(!(stables.contains(stable)&&ttables.contains(ttable))){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]不存在--");
			continue;
		}
		Map<String,String> reflect = new LinkedHashMap<String,String>();
		Map<String, String> scolumns = factory.queryColumns(stable);
		Map<String, String> tcolumns = tfactory.queryColumns(ttable);
		if(scolumns==null||scolumns.isEmpty()||tcolumns==null||tcolumns.isEmpty()){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无合适字段--");
			continue;
		}
		for(String scolumn:scolumns.keySet()){
			String s_column = scolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
			if(filter_columns!=null&&(filter_columns.contains(scolumn)||filter_columns.contains(s_column))){
				continue;
			}
			for(String tcolumn:tcolumns.keySet()){
				String t_column = tcolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
				if(filter_columns!=null&&(filter_columns.contains(tcolumn)||filter_columns.contains(t_column))){
					continue;
				}
				if(scolumn.equalsIgnoreCase(tcolumn)||scolumn.equalsIgnoreCase(t_column)||s_column.equalsIgnoreCase(tcolumn)||s_column.equalsIgnoreCase(t_column)){
					reflect.put(scolumn, tcolumn);
				}
			}
		}
		if(reflect.isEmpty()){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无对应字段--");
			continue;
		}
		List<?> datas = factory.executeQuery(stable, null, null);
		System.out.println("--数据表["+stable+"]数据量:"+datas.size());
		for (Object data : datas) {
			Map<String,Object> tdata = new LinkedHashMap<String,Object>();
			JSONObject json = (JSONObject)data;
			for(String key:json.keySet()){
				Object value = json.get(key);
				if(value instanceof Date){
					value = DateUtil.formatDateTimeStr((Date)value);
				}
				if(value instanceof String){
					value = "\""+json.getString(key)+"\"";
				}
				tdata.replace(reflect.get(key), value);
			}
			String sql = "insert into "+ttable+"("+StringUtils.join(tdata.keySet(), ",")+")values("+StringUtils.join(tdata.values(), ",")+")";
			tfactory.executeUpdate(sql);
		}
	}
}
 
Example 17
Project: database-transform-tool   File: ManageTable.java   View Source Code Vote up 4 votes
/**
 * @decription 数据同步(MongoDB-->MongoDB)
 * @author yi.zhang
 * @time 2017年8月4日 下午5:26:59
 * @param source	数据源
 * @param target	目标库
 * @param mapper	表映射
 * @param filter_columns	字段过滤
 */
protected void mongodb2mongodb(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){
	if(source==null||target==null){
		return;
	}
	MongoDBFactory factory = new MongoDBFactory();
	factory.init(source.getServers(), source.getDatabase(), source.getSchema(), source.getUsername(), source.getPassword());
	Map<String,String> mapping = new HashMap<String,String>();
	if(mapper==null||mapper.size()==0){
		List<String> tables = factory.queryTables();
		for (String table : tables) {
			mapping.put(table, table);
		}
	}else{
		mapping = mapper;
	}
	MongoDBFactory tfactory = new MongoDBFactory();
	tfactory.init(target.getServers(), target.getDatabase(), target.getSchema(), target.getUsername(), target.getPassword());
	List<String> stables = factory.queryTables();
	List<String> ttables = tfactory.queryTables();
	for(String stable : mapping.keySet()){
		String ttable = mapping.get(stable);
		if(!stables.contains(stable)){
			System.out.println("--数据表["+stable+"]不存在--");
			continue;
		}
		Map<String,String> reflect = new LinkedHashMap<String,String>();
		Map<String, String> scolumns = factory.queryColumns(stable);
		Map<String, String> tcolumns = ttables!=null&&ttables.contains(ttable)?tfactory.queryColumns(ttable):scolumns;
		if(scolumns==null||scolumns.isEmpty()||tcolumns==null||tcolumns.isEmpty()){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无合适字段--");
			continue;
		}
		for(String scolumn:scolumns.keySet()){
			String s_column = scolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
			if(filter_columns!=null&&(filter_columns.contains(scolumn)||filter_columns.contains(s_column))){
				continue;
			}
			for(String tcolumn:tcolumns.keySet()){
				String t_column = tcolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
				if(filter_columns!=null&&(filter_columns.contains(tcolumn)||filter_columns.contains(t_column))){
					continue;
				}
				if(scolumn.equalsIgnoreCase(tcolumn)||scolumn.equalsIgnoreCase(t_column)||s_column.equalsIgnoreCase(tcolumn)||s_column.equalsIgnoreCase(t_column)){
					reflect.put(scolumn, tcolumn);
				}
			}
		}
		if(reflect.isEmpty()){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无对应字段--");
			continue;
		}
		List<?> datas = factory.executeQuery(stable, null, null);
		System.out.println("--数据表["+stable+"]数据量:"+datas.size());
		for (Object data : datas) {
			Map<String,Object> tdata = new LinkedHashMap<String,Object>();
			JSONObject json = (JSONObject)data;
			for(String key:json.keySet()){
				Object value = json.get(key);
				if(!key.matches("(\\w+)")){
					continue;
				}
				tdata.replace(reflect.get(key), value);
			}
			tfactory.save(ttable, tdata);
		}
	}
}
 
Example 18
Project: database-transform-tool   File: ManageTable.java   View Source Code Vote up 4 votes
/**
	 * @decription 数据同步(Greenplum-->Cassandra)
	 * @author yi.zhang
	 * @time 2017年8月4日 下午5:26:59
	 * @param source	数据源
	 * @param target	目标库
	 * @param mapper	表映射
	 * @param filter_columns	字段过滤
	 */
	protected void greenplum2cassandra(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){
		if(source==null||target==null){
			return;
		}
		GreenplumFactory factory = new GreenplumFactory();
		factory.init(source.getServers(), source.getDatabase(), source.getSchema(), source.getUsername(), source.getPassword(), true, 100, 10);
		Map<String,String> mapping = new HashMap<String,String>();
		if(mapper==null||mapper.size()==0){
			List<String> tables = factory.queryTables();
			for (String table : tables) {
				mapping.put(table, table);
			}
		}else{
			mapping = mapper;
		}
		CassandraFactory tfactory = new CassandraFactory();
		tfactory.init(target.getServers(), target.getKeyspace(), target.getUsername(), target.getPassword());
		List<String> stables = factory.queryTables();
//		List<String> ttables = tfactory.queryTables();
		for(String stable : mapping.keySet()){
			String ttable = mapping.get(stable);
			if(!(stables.contains(stable))){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]不存在--");
				continue;
			}
			Map<String,String> reflect = new LinkedHashMap<String,String>();
			Map<String, String> scolumns = factory.queryColumns(stable);
			Map<String, String> tcolumns = tfactory.queryColumns(ttable);
			if(scolumns==null||scolumns.isEmpty()||tcolumns==null||tcolumns.isEmpty()){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无合适字段--");
				continue;
			}
			for(String scolumn:scolumns.keySet()){
				String s_column = scolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
				if(filter_columns!=null&&(filter_columns.contains(scolumn)||filter_columns.contains(s_column))){
					continue;
				}
				for(String tcolumn:tcolumns.keySet()){
					String t_column = tcolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
					if(filter_columns!=null&&(filter_columns.contains(tcolumn)||filter_columns.contains(t_column))){
						continue;
					}
					if(scolumn.equalsIgnoreCase(tcolumn)||scolumn.equalsIgnoreCase(t_column)||s_column.equalsIgnoreCase(tcolumn)||s_column.equalsIgnoreCase(t_column)){
						reflect.put(scolumn, tcolumn);
					}
				}
			}
			if(reflect.isEmpty()){
				System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无对应字段--");
				continue;
			}
			String ssql = "select "+StringUtils.join(reflect.keySet(), ",")+" from "+stable;
			List<?> datas = factory.executeQuery(ssql, null);
			System.out.println("--数据表["+stable+"]数据量:"+datas.size());
			for (Object data : datas) {
				Map<String,Object> tdata = new LinkedHashMap<String,Object>();
				JSONObject json = (JSONObject)data;
				for(String key:json.keySet()){
					Object value = json.get(key);
					if(value instanceof Date){
						value = DateUtil.formatDateTimeStr((Date)value);
					}
					if(value instanceof String){
						value = "\""+json.getString(key)+"\"";
					}
					tdata.replace(reflect.get(key), value);
				}
				String sql = "insert into "+ttable+"("+StringUtils.join(tdata.keySet(), ",")+")values("+StringUtils.join(tdata.values(), ",")+")";
				tfactory.executeUpdate(sql);
			}
		}
	}
 
Example 19
Project: database-transform-tool   File: ManageTable.java   View Source Code Vote up 4 votes
/**
 * @decription 数据同步(Greenplum-->MongoDB)
 * @author yi.zhang
 * @time 2017年8月4日 下午5:26:59
 * @param source	数据源
 * @param target	目标库
 * @param mapper	表映射
 * @param filter_columns	字段过滤
 */
protected void greenplum2mongodb(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){
	if(source==null||target==null){
		return;
	}
	GreenplumFactory factory = new GreenplumFactory();
	factory.init(source.getServers(), source.getDatabase(), source.getSchema(), source.getUsername(), source.getPassword(), true, 100, 10);
	Map<String,String> mapping = new HashMap<String,String>();
	if(mapper==null||mapper.size()==0){
		List<String> tables = factory.queryTables();
		for (String table : tables) {
			mapping.put(table, table);
		}
	}else{
		mapping = mapper;
	}
	MongoDBFactory tfactory = new MongoDBFactory();
	tfactory.init(target.getServers(), target.getDatabase(), target.getSchema(), target.getUsername(), target.getPassword());
	List<String> stables = factory.queryTables();
	List<String> ttables = tfactory.queryTables();
	for(String stable : mapping.keySet()){
		String ttable = mapping.get(stable);
		if(!(stables.contains(stable)&&ttables.contains(ttable))){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]不存在--");
			continue;
		}
		Map<String,String> reflect = new LinkedHashMap<String,String>();
		Map<String, String> scolumns = factory.queryColumns(stable);
		Map<String, String> tcolumns = tfactory.queryColumns(ttable);
		if(scolumns==null||scolumns.isEmpty()||tcolumns==null||tcolumns.isEmpty()){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无合适字段--");
			continue;
		}
		for(String scolumn:scolumns.keySet()){
			String s_column = scolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
			if(filter_columns!=null&&(filter_columns.contains(scolumn)||filter_columns.contains(s_column))){
				continue;
			}
			for(String tcolumn:tcolumns.keySet()){
				String t_column = tcolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
				if(filter_columns!=null&&(filter_columns.contains(tcolumn)||filter_columns.contains(t_column))){
					continue;
				}
				if(scolumn.equalsIgnoreCase(tcolumn)||scolumn.equalsIgnoreCase(t_column)||s_column.equalsIgnoreCase(tcolumn)||s_column.equalsIgnoreCase(t_column)){
					reflect.put(scolumn, tcolumn);
				}
			}
		}
		if(reflect.isEmpty()){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无对应字段--");
			continue;
		}
		String ssql = "select "+StringUtils.join(reflect.keySet(), ",")+" from "+stable;
		List<?> datas = factory.executeQuery(ssql, null);
		System.out.println("--数据表["+stable+"]数据量:"+datas.size());
		for (Object data : datas) {
			Map<String,Object> tdata = new LinkedHashMap<String,Object>();
			JSONObject json = (JSONObject)data;
			for(String key:json.keySet()){
				Object value = json.get(key);
				if(!key.matches("(\\w+)")){
					continue;
				}
				tdata.replace(reflect.get(key), value);
			}
			tfactory.save(ttable, tdata);
		}
	}
}
 
Example 20
Project: database-transform-tool   File: ManageTable.java   View Source Code Vote up 4 votes
/**
 * @decription 数据同步(Greenplum-->Greenplum)
 * @author yi.zhang
 * @time 2017年8月4日 下午5:26:59
 * @param source	数据源
 * @param target	目标库
 * @param mapper	表映射
 * @param filter_columns	字段过滤
 */
protected void greenplum2greenplum(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){
	if(source==null||target==null){
		return;
	}
	GreenplumFactory factory = new GreenplumFactory();
	factory.init(source.getServers(), source.getDatabase(), source.getSchema(), source.getUsername(), source.getPassword(), true, 100, 10);
	Map<String,String> mapping = new HashMap<String,String>();
	if(mapper==null||mapper.size()==0){
		List<String> tables = factory.queryTables();
		for (String table : tables) {
			mapping.put(table, table);
		}
	}else{
		mapping = mapper;
	}
	GreenplumFactory tfactory = new GreenplumFactory();
	tfactory.init(target.getServers(), target.getDatabase(), target.getSchema(), target.getUsername(), target.getPassword(), true, 100, 10);
	List<String> stables = factory.queryTables();
	List<String> ttables = tfactory.queryTables();
	for(String stable : mapping.keySet()){
		String ttable = mapping.get(stable);
		if(!(stables.contains(stable)&&ttables.contains(ttable))){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]不存在--");
			continue;
		}
		Map<String,String> reflect = new LinkedHashMap<String,String>();
		Map<String, String> scolumns = factory.queryColumns(stable);
		Map<String, String> tcolumns = tfactory.queryColumns(ttable);
		if(scolumns==null||scolumns.isEmpty()||tcolumns==null||tcolumns.isEmpty()){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无合适字段--");
			continue;
		}
		for(String scolumn:scolumns.keySet()){
			String s_column = scolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
			if(filter_columns!=null&&(filter_columns.contains(scolumn)||filter_columns.contains(s_column))){
				continue;
			}
			for(String tcolumn:tcolumns.keySet()){
				String t_column = tcolumn.trim().toLowerCase().replaceAll("(_+?|-+?)", "");
				if(filter_columns!=null&&(filter_columns.contains(tcolumn)||filter_columns.contains(t_column))){
					continue;
				}
				if(scolumn.equalsIgnoreCase(tcolumn)||scolumn.equalsIgnoreCase(t_column)||s_column.equalsIgnoreCase(tcolumn)||s_column.equalsIgnoreCase(t_column)){
					reflect.put(scolumn, tcolumn);
				}
			}
		}
		if(reflect.isEmpty()){
			System.out.println("--数据表["+stable+"]或目标表["+ttable+"]无对应字段--");
			continue;
		}
		String ssql = "select "+StringUtils.join(reflect.keySet(), ",")+" from "+stable;
		List<?> datas = factory.executeQuery(ssql, null);
		System.out.println("--数据表["+stable+"]数据量:"+datas.size());
		for (Object data : datas) {
			Map<String,Object> tdata = new LinkedHashMap<String,Object>();
			JSONObject json = (JSONObject)data;
			for(String key:json.keySet()){
				Object value = json.get(key);
				if(value instanceof Date){
					value = DateUtil.formatDateTimeStr((Date)value);
				}
				if(value instanceof String){
					value = "\""+json.getString(key)+"\"";
				}
				tdata.replace(reflect.get(key), value);
			}
			String sql = "insert into "+ttable+"("+StringUtils.join(tdata.keySet(), ",")+")values("+StringUtils.join(tdata.values(), ",")+")";
			tfactory.executeUpdate(sql);
		}
	}
}