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

The following examples show how to use java.util.Map#replace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResolverFully.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
public Map<String,Example> resolveExample(Map<String,Example> examples){

        Map<String,Example> resolveExamples = examples;

        if (examples != null) {

            for (String name : examples.keySet()) {
                if (examples.get(name).get$ref() != null) {
                    String ref = examples.get(name).get$ref();
                    ref = ref.substring(ref.lastIndexOf("/") + 1);
                    Example sample = this.examples.get(ref);
                    resolveExamples.replace(name, sample);
                }
            }
        }

        return resolveExamples;

    }
 
Example 2
Source File: LinkedHashMapTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_replace$K$V() {
    MapDefaultMethodTester.test_replace$K$V(new LinkedHashMap<>(), true /*acceptsNullKey*/,
            true /*acceptsNullValue*/);

    // Test for access order
    Map<String, String> m = new LinkedHashMap<>(8, .75f, true  /*accessOrder*/);
    m.put("key", "value");
    m.put("key1", "value1");
    m.put("key2", "value2");
    m.replace("key1", "value2");
    Map.Entry<String, String> newest = null;
    for (Map.Entry<String, String> e : m.entrySet()) {
        newest = e;
    }
    assertEquals("key1", newest.getKey());
    assertEquals("value2", newest.getValue());
}
 
Example 3
Source File: PostServiceImpl.java    From SimpleBBS with Apache License 2.0 6 votes vote down vote up
@Override
public Page<Post> findPostByPage(Map<String, Long> map) {
    Page<Post> page = new Page<>();
    map.put("showPage", page.getShowCount().longValue());
    page.setCurrentPage((int)(map.get("startPage") + 1));
    map.replace("startPage", map.get("startPage") * page.getShowCount());
    page.setModelList(postMapper.findPostByPage(map));

    Integer postCount = postMapper.getPostCount();


    page.setPageTotal(postCount % page.getShowCount() == 0 ? postCount / page.getShowCount() : (postCount / page.getShowCount()) + 1);
    for (Post post : page.getModelList()) {//查询回复数
        post.setReplyCount(replyMapper.getReplyCountByPid(post.getPid()));
    }
    return page;
}
 
Example 4
Source File: AsMapTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@CheckNoStats
@Test(dataProvider = "caches", expectedExceptions = WriteException.class)
@CacheSpec(implementation = Implementation.Caffeine, keys = ReferenceType.STRONG,
    population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
    compute = Compute.SYNC, writer = Writer.EXCEPTIONAL, removalListener = Listener.REJECTING)
public void replaceConditionally_writerFails(Map<Integer, Integer> map, CacheContext context) {
  try {
    Integer key = context.middleKey();
    map.replace(key, context.original().get(key), context.absentValue());
  } finally {
    assertThat(map, equalTo(context.original()));
  }
}
 
Example 5
Source File: CollectionsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_CheckedMap_replace$K$V$V() {
    Map<Integer, Double> map = new HashMap<>();
    Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
    MapDefaultMethodTester.test_replace$K$V$V(checkedMap, true /* acceptsNullKey */,
            true /* acceptsNullValue */);

    // Without generics to check the typeCheck implementation
    Map checkedMap2 = Collections.checkedMap(new HashMap<>(), Integer.class, String.class);
    checkedMap2.put(1, A_STRING);

    try {
        checkedMap2.replace(1, NOT_A_STRING);
        fail();
    } catch (ClassCastException expected) {}
}
 
Example 6
Source File: AsMapTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@CheckNoWriter @CheckNoStats
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void replaceConditionally_nullKeyAndValues(
    Map<Integer, Integer> map, CacheContext context) {
  map.replace(null, null, null);
}
 
Example 7
Source File: GenomicsDBImportIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(expectedExceptions = GenomicsDBImport.UnableToCreateGenomicsDBWorkspace.class)
public void testWithMiscOptionsNoOverwrite() throws IOException {
    final String workspace = createTempDir("genomicsdb-misc-options-nooverwrite").getAbsolutePath() + "/workspace";
    IOUtils.deleteOnExit(IOUtils.getPath(workspace));
    Map<String, Object> options = new HashMap<String, Object>();
    basicWriteAndQueryWithOptions(workspace, options);

    // Test with overwrite workspace set to false - should throw an exception - GenomicsDBImport.UnableToCreateGenomicsDBWorkspace
    options.replace(GenomicsDBImport.OVERWRITE_WORKSPACE_LONG_NAME, false);
    basicWriteAndQueryWithOptions(workspace, options);
}
 
Example 8
Source File: EducationManagementActorTest.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
private Request getUpdateEducReqWithoutEducId(String operationName) {
  Request request = new Request();
  Map<String, Object> educationMap = getBaseEducationRequest();
  educationMap.replace(JsonKey.IS_DELETED, false);
  educationMap.remove(JsonKey.ID);
  educationMap.put(JsonKey.ADDRESS, getBaseAddressRequest());
  List<Map<String, Object>> educationList = new ArrayList<>();
  educationList.add(educationMap);
  Map<String, Object> reqMap = new HashMap<>();
  reqMap.put(JsonKey.EDUCATION, educationList);
  reqMap.put(JsonKey.ID, "anyUserId");
  request.setOperation(operationName);
  request.setRequest(reqMap);
  return request;
}
 
Example 9
Source File: ClusterHostServiceRunner.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Map<String, List<String>> getServiceLocations(Cluster cluster) {
    List<String> serviceNames = exposedServiceCollector.getAllServiceNames();
    Map<String, List<String>> componentLocation = componentLocator.getComponentLocation(cluster, serviceNames);
    ExposedService impalaService = exposedServiceCollector.getImpalaService();
    if (componentLocation.containsKey(impalaService.getServiceName())) {
        // IMPALA_DEBUG_UI role is not a valid role, but we need to distinguish the 2 roles in order to generate the Knox topology file
        componentLocation.put(exposedServiceCollector.getImpalaDebugUIService().getServiceName(),
                List.copyOf(componentLocation.get(impalaService.getServiceName())));
        Map<String, List<String>> impalaLocations = componentLocator.getImpalaCoordinatorLocations(cluster);
        List<String> locations = impalaLocations.values().stream().flatMap(List::stream).collect(Collectors.toList());
        componentLocation.replace(impalaService.getServiceName(), locations);
    }
    return componentLocation;
}
 
Example 10
Source File: ResourceClassGenerator.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
public static Map<ResourceType, Map<String, Object>> buildFullResourceMap(ResourceNamespace namespace, String fqcn, File projectR, ResourceClassGeneratorConfig config) {
    final Map<Integer, Integer> originalToGenerated = new HashMap<>();
    final Map<ResourceType, Map<String, Object>> resources = Maps.newHashMap();
    try {
        List<String> readLines = Files.readLines(projectR, StandardCharsets.UTF_8);
        for (String line : readLines) {
            StringTokenizer tok = new StringTokenizer(line, " ", false);
            if (tok.countTokens() > 3) {
                switch (tok.nextToken()) {
                    case "int":
                        handleFullLine(namespace, tok, resources, originalToGenerated, config);
                        break;
                    case "int[]":
                        handleFullLineArray(tok, line, resources);
                        break;
                }
            }
        }
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    //refresh array ids to new IDs
    for (Map.Entry<ResourceType, Map<String, Object>> entry : resources.entrySet()) {
        ResourceType type = entry.getKey();
        Map<String, Object> values = entry.getValue();
        for (Map.Entry<String, Object> entry1 : values.entrySet()) {
            String name = entry1.getKey();
            Object value = entry1.getValue();
            if (value instanceof List) {
                List<Integer> in = (List<Integer>) value;
                List<Integer> out = new ArrayList<>();
                for (Integer val : in) {
                    Integer newVal = originalToGenerated.get(val);
                    if (newVal == null) {
                        out.add(val);
                    } else {
                        out.add(newVal);
                    }
                }
                values.replace(name, out);
            }
        }
    }
    return resources;
}
 
Example 11
Source File: SqlConnectorVerifierExtension.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Override
public 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 = SqlSupport.createConnection(parameters)) {
            // just try to get the connection
            if (!connection.isValid(0)) {
                builder.error(ResultErrorBuilder.withCodeAndDescription(
                    VerificationError.StandardCode.GENERIC, "Unable to connect to database")
                    .build());
            }
        } catch (SQLException e) {
            final Map<String, Object> redacted = new HashMap<>(parameters);
            redacted.replace("password", "********");
            LOG.warn("Unable to connect 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 12
Source File: ManageTable.java    From database-transform-tool with Apache License 2.0 4 votes vote down vote up
/**
 * @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 13
Source File: AsMapTest.java    From caffeine with Apache License 2.0 4 votes vote down vote up
@CheckNoWriter @CheckNoStats
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void replaceConditionally_nullKey(Map<Integer, Integer> map, CacheContext context) {
  map.replace(null, 1, 1);
}
 
Example 14
Source File: ManageTable.java    From database-transform-tool with Apache License 2.0 4 votes vote down vote up
/**
	 * @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
Source File: ManageTable.java    From database-transform-tool with Apache License 2.0 4 votes vote down vote up
/**
	 * @decription 数据同步(Cassandra-->MySQL|SQL Server|Oracle)
	 * @author yi.zhang
	 * @time 2017年8月4日 下午5:26:59
	 * @param source	数据源
	 * @param target	目标库
	 * @param mapper	表映射
	 * @param filter_columns	字段过滤
	 */
	protected void cassandra2sql(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;
		}
		JDBCFactory tfactory = new JDBCFactory();
		String driverName=null,url = null;
		if(target.getType()==3.1){//MySQL
			driverName="com.mysql.jdbc.Driver";
			url = "jdbc:mysql://"+target.getServers()+(target.getPort()>0?":"+target.getPort():"")+"/"+target.getDatabase()+"?useUnicode=true&characterEncoding=UTF8";
		}
		if(target.getType()==3.2){//SQL Server
			driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
			url = "jdbc:microsoft://"+target.getServers()+(target.getPort()>0?":"+target.getPort():"")+";database="+target.getDatabase()+";encrypt=true;trustServerCertificate=false;useUnicode=true;characterEncoding=UTF8";
		}
		if(target.getType()==3.3){//Oracle
			driverName="oracle.jdbc.driver.OracleDriver";
			url = "jdbc:oracle:thin:@"+target.getServers()+(target.getPort()>0?":"+target.getPort():"")+":"+target.getDatabase();
		}
		tfactory.init(driverName, url,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(!(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 16
Source File: HashImpl.java    From icefig with Apache License 2.0 4 votes vote down vote up
@Override
public MutableHash<K, V> replace(K k, V oldValue, V newValue) {
    Map<K, V> newHash = new HashMap<>(hash);
    newHash.replace(k, oldValue, newValue);
    return new HashImpl<>(newHash);
}
 
Example 17
Source File: AsMapTest.java    From caffeine with Apache License 2.0 4 votes vote down vote up
@CheckNoWriter @CheckNoStats
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void replaceConditionally_nullNewValue(Map<Integer, Integer> map, CacheContext context) {
  map.replace(1, 1, null);
}
 
Example 18
Source File: ManageTable.java    From database-transform-tool with Apache License 2.0 4 votes vote down vote up
/**
 * @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);
		}
	}
}
 
Example 19
Source File: ManageTable.java    From database-transform-tool with Apache License 2.0 4 votes vote down vote up
/**
 * @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 20
Source File: PluginManager.java    From plugin-installation-manager-tool with MIT License 4 votes vote down vote up
private Map<String, Plugin> resolveRecursiveDependencies(Plugin plugin, @CheckForNull Map<String, Plugin> topLevelDependencies) {
    Deque<Plugin> queue = new LinkedList<>();
    Map<String, Plugin> recursiveDependencies = new HashMap<>();
    queue.add(plugin);
    recursiveDependencies.put(plugin.getName(), plugin);

    while (queue.size() != 0) {
        Plugin dependency = queue.poll();

        if (!dependency.isDependenciesSpecified()) {
            dependency.setDependencies(resolveDirectDependencies(dependency));
        }

        for (Plugin p : dependency.getDependencies()) {
            String dependencyName = p.getName();
            Plugin pinnedPlugin = topLevelDependencies != null ? topLevelDependencies.get(dependencyName) : null;

            // See https://github.com/jenkinsci/plugin-installation-manager-tool/pull/102
            if (pinnedPlugin != null) { // There is a top-level plugin with the same ID
                if (pinnedPlugin.getVersion().isNewerThanOrEqualTo(p.getVersion())) {
                    if (verbose) {
                        logVerbose(String.format("Skipping dependency %s:%s and its sub-dependencies, because there is a higher version defined on the top level - %s:%s",
                                p.getName(), p.getVersion(), pinnedPlugin.getName(), pinnedPlugin.getVersion()));
                    }
                    continue;
                } else {
                    String message = String.format("Plugin %s:%s depends on %s:%s, but there is an older version defined on the top level - %s:%s",
                            plugin.getName(), plugin.getVersion(), p.getName(), p.getVersion(), pinnedPlugin.getName(), pinnedPlugin.getVersion());
                    // TODO(oleg_nenashev): Should be an error by default, but it is not how the tests are written now
                    // throw new PluginDependencyStrategyException(message);
                    logVerbose(message);
                }
            }

            if (!recursiveDependencies.containsKey(dependencyName)) {
                recursiveDependencies.put(dependencyName, p);
                queue.add(p);
            } else {
                Plugin existingDependency = recursiveDependencies.get(dependencyName);
                if (existingDependency.getVersion().isOlderThan(p.getVersion())) {
                    outputPluginReplacementInfo(existingDependency, p);
                    queue.add(p); //in case the higher version contains dependencies the lower version didn't have
                    recursiveDependencies.replace(dependencyName, existingDependency, p);
                }
            }
        }
    }
    return recursiveDependencies;
}