当前位置: 首页>>代码示例>>Java>>正文


Java Map.replace方法代码示例

本文整理汇总了Java中java.util.Map.replace方法的典型用法代码示例。如果您正苦于以下问题:Java Map.replace方法的具体用法?Java Map.replace怎么用?Java Map.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.Map的用法示例。


在下文中一共展示了Map.replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getContests

import java.util.Map; //导入方法依赖的package包/类
@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);
}
 
开发者ID:Eagle-OJ,项目名称:eagle-oj-api,代码行数:17,代码来源:ContestController.java

示例2: setTranslation

import java.util.Map; //导入方法依赖的package包/类
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);
}
 
开发者ID:Leviathan-Studio,项目名称:MineIDE,代码行数:18,代码来源:Translation.java

示例3: setSelectedAllData

import java.util.Map; //导入方法依赖的package包/类
public void setSelectedAllData(boolean selected) {
    for (Map<String, Object> datum : plot.getData()) {
        datum.replace("Selected", selected);
    }

    plot.setData(plot.getData());
}
 
开发者ID:CIRDLES,项目名称:Squid,代码行数:8,代码来源:AbstractTopsoilPlot.java

示例4: testReplaceIfMapped

import java.util.Map; //导入方法依赖的package包/类
@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));

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:41,代码来源:InPlaceOpsCollisions.java

示例5: checkContestValid

import java.util.Map; //导入方法依赖的package包/类
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);
        }
    }
}
 
开发者ID:Eagle-OJ,项目名称:eagle-oj-api,代码行数:12,代码来源:ContestService.java

示例6: testReplaceOldValue

import java.util.Map; //导入方法依赖的package包/类
@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));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:InPlaceOpsCollisions.java

示例7: getLearningAlgorithmsMetricResultsForOneDatasetFile

import java.util.Map; //导入方法依赖的package包/类
/**
 * 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();
}
 
开发者ID:Intelligent-Systems-Group,项目名称:jpl-framework,代码行数:46,代码来源:EvaluationsOutputGenerator.java

示例8: testReplace

import java.util.Map; //导入方法依赖的package包/类
/**
 * 新增
 * 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);
}
 
开发者ID:cbooy,项目名称:cakes,代码行数:38,代码来源:MapNewFuturesDemo.java

示例9: addItemToCart

import java.util.Map; //导入方法依赖的package包/类
/**
 * @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());
}
 
开发者ID:redhat-developer-demos,项目名称:popular-movie-store,代码行数:40,代码来源:ShoppingCartController.java

示例10: shouldRouteCorrectly

import java.util.Map; //导入方法依赖的package包/类
@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);

    }
}
 
开发者ID:drinkwater-io,项目名称:drinkwater-java,代码行数:62,代码来源:RoutingTest.java

示例11: transformDatasetToBaseLearnerRegressionDataset

import java.util.Map; //导入方法依赖的package包/类
/**
 * 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;

}
 
开发者ID:Intelligent-Systems-Group,项目名称:jpl-framework,代码行数:50,代码来源:RankingRegressionTransformerUtils.java

示例12: verifyParameters

import java.util.Map; //导入方法依赖的package包/类
@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();
}
 
开发者ID:syndesisio,项目名称:connectors,代码行数:50,代码来源:SqlStoredConnectorVerifierExtension.java

示例13: cassandra2cassandra

import java.util.Map; //导入方法依赖的package包/类
/**
	 * @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);
			}
		}
	}
 
开发者ID:dev-share,项目名称:database-transform-tool,代码行数:81,代码来源:ManageTable.java

示例14: cassandra2mongodb

import java.util.Map; //导入方法依赖的package包/类
/**
	 * @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);
			}
		}
	}
 
开发者ID:dev-share,项目名称:database-transform-tool,代码行数:78,代码来源:ManageTable.java

示例15: mongodb2cassandra

import java.util.Map; //导入方法依赖的package包/类
/**
	 * @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);
			}
		}
	}
 
开发者ID:dev-share,项目名称:database-transform-tool,代码行数:82,代码来源:ManageTable.java


注:本文中的java.util.Map.replace方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。