Java Code Examples for com.alibaba.fastjson.JSONObject#keySet()
The following examples show how to use
com.alibaba.fastjson.JSONObject#keySet() .
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: LeancloudObject.java From leancloud_flutter_plugin with MIT License | 6 votes |
private AVObject convertStringToAVObject(String avObjectString) { JSONObject avObjectJson = JSON.parseObject(avObjectString); String className = avObjectJson.getString("className"); String fieldsString = avObjectJson.getString("fields"); JSONObject fieldsJson = JSON.parseObject(fieldsString); AVObject avObject = new AVObject(className); for (String key : fieldsJson.keySet()) { //TODO if those value is Date or byte[] type? //TODO more data type? e.g. AVGeoPoint? if (key.equals("createdAt")) continue; if (key.equals("updatedAt")) continue; if (key.equals("objectId")) avObject.setObjectId(fieldsJson.get(key).toString()); else avObject.put(key, fieldsJson.get(key)); } return avObject; }
Example 2
Source File: OperationService.java From SoloPi with Apache License 2.0 | 6 votes |
/** * 初始化运行环境 */ public void initParams() { if (temporaryVariables != null) { temporaryVariables.clear(); } else { temporaryVariables = new ConcurrentHashMap<>(); } if (runtimeVariables != null) { runtimeVariables.clear(); } else { runtimeVariables = new Stack<>(); } // 存放全局变量 HashMap<String, Object> globalParam = new HashMap<>(); String globalSettings = SPService.getString(SPService.KEY_GLOBAL_SETTINGS, "{}"); JSONObject obj = JSON.parseObject(globalSettings); if (obj != null && obj.size() > 0) { for (String key : obj.keySet()) { globalParam.put(key, obj.getString(key)); } } runtimeVariables.push(globalParam); runtimeVariables.push(new HashMap<String, Object>()); }
Example 3
Source File: BulkResultVo.java From DataLink with Apache License 2.0 | 6 votes |
/** * * Description: 校验是否有失败的操作 * Created on 2017-1-3 下午9:45:47 * @author 孔增([email protected]) */ public void checkFailed() { if (jsonObject != null && jsonObject.containsKey(ParseHandler.ITEMS_NAME)) { for (Object obj : jsonObject.getJSONArray(ParseHandler.ITEMS_NAME)) { JSONObject jso = (JSONObject)obj; for (String key : jso.keySet()) { JSONObject item = jso.getJSONObject(key); JSONObject shards = item.getJSONObject(ParseHandler.SHARD_NAME); if(shards == null) { continue; } Integer failed = shards.getIntValue(ParseHandler.FAILED_NAME); if(failed > 0) { throw new ElasticSearchException(item.toJSONString()); } } } } }
Example 4
Source File: ProxyOpertion.java From Mars-Java with MIT License | 6 votes |
/** * 单表增删改 * @param marsUpdate 注解 * @param dataSourceName 数据源 * @param param 参数 * @return 数据 * @throws Exception 异常 */ private static Object doUpdate(MarsUpdate marsUpdate, String dataSourceName, Object param) throws Exception { JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(param)); StringBuffer sql = new StringBuffer(); sql.append("update "); sql.append(marsUpdate.tableName()); sql.append(" set "); boolean isFirst = true; for(String key : jsonObject.keySet()){ Object val = jsonObject.get(key); if(val != null && !key.equals(marsUpdate.primaryKey())) { if (!isFirst) { sql.append(","); } sql.append(key); sql.append(" = #{"); sql.append(key); sql.append("}"); isFirst = false; } } sql.append(builderWhere(marsUpdate)); return JdbcTemplate.get(dataSourceName).update(sql.toString(),param); }
Example 5
Source File: TypeDefineOperator.java From ES-Fastloader with Apache License 2.0 | 6 votes |
public static JSONObject toJson(JSONObject define, ESVersion version) { if (version == ESVersion.ES651) { dealHighLevelType(define); if (define.containsKey(FIELDS_STR)) { JSONObject fields = define.getJSONObject(FIELDS_STR); for (String key : fields.keySet()) { JSONObject fieldType = fields.getJSONObject(key); dealHighLevelType(fieldType); } } } return define; }
Example 6
Source File: IndexConfig.java From ES-Fastloader with Apache License 2.0 | 6 votes |
public IndexConfig(JSONObject root) throws Exception { if(root==null) { throw new Exception("root is null"); } for(String key : root.keySet()) { if(key.equalsIgnoreCase(SETTINGS_STR)) { settings = root.getJSONObject(key); } else if(key.equalsIgnoreCase(ALIASES_STR)) { aliases = root.getJSONObject(key); } else if(key.equalsIgnoreCase(MAPPINGS_STR)) { mappings = new MappingConfig(root.getJSONObject(key)); } else { notUsedMap.put(key, root.get(key)); } } }
Example 7
Source File: BulkResultVo.java From EserKnife with Apache License 2.0 | 6 votes |
public List<BulkResultItem> getSuccessItems() { if(successItems.size() > 0) { return successItems; } if (jsonObject != null && jsonObject.containsKey(ParseHandler.ITEMS_NAME)) { for (Object obj : jsonObject.getJSONArray(ParseHandler.ITEMS_NAME)) { JSONObject jso = (JSONObject)obj; for (String key : jso.keySet()) { JSONObject item = jso.getJSONObject(key); if (!item.containsKey(ParseHandler.ERROR_NAME)) { BulkResultItem itemVo = new BulkResultItem(key,item); successItems.add(itemVo); //冗余一份,便于查询 String itemKey = itemVo.getIndex()+"|"+itemVo.getType()+"|"+itemVo.getId()+"|"+itemVo.getAction(); successMap.put(itemKey, itemVo); } } } } return successItems; }
Example 8
Source File: GenericJsonDeserializer.java From bird-java with MIT License | 6 votes |
private static Object parseObject(Object obj) { if(!(obj instanceof JSONObject))return obj; JSONObject jsonObject = (JSONObject) obj; for (String key : jsonObject.keySet()) { Object value = jsonObject.get(key); if (value == null) continue; if (value instanceof String) { String str = (String) value; if (StringUtils.isBlank(str)) continue; if (Pattern.matches(DATE_REG, str)) { jsonObject.put(key, str.trim() + TIME_SUFFIX); } } else if (value instanceof JSONObject) { jsonObject.put(key, parseObject(value)); } } return jsonObject; }
Example 9
Source File: CaseParamSeparateFragment.java From SoloPi with Apache License 2.0 | 6 votes |
/** * 设置高级设置 * * @param advanceCaseSetting */ @Override public void setAdvanceCaseSetting(@NonNull AdvanceCaseSetting advanceCaseSetting) { storedParams = new LinkedHashMap<>(); presetParams = advanceCaseSetting.getParams(); runningParam = advanceCaseSetting.getRunningParam(); if (runningParam == null) { runningParam = new CaseRunningParam(); } // 如果之前有存储p if (runningParam.getMode() == CaseRunningParam.ParamMode.SEPARATE) { List<JSONObject> params = runningParam.getParamList(); if (params != null) { for (JSONObject obj: params) { for (String key: obj.keySet()) { storedParams.put(key, obj.getString(key)); } } } } }
Example 10
Source File: WXStreamModule.java From ucar-weex-core with Apache License 2.0 | 5 votes |
private void extractHeaders(JSONObject headers, Options.Builder builder){ //set user-agent String UA = WXHttpUtil.assembleUserAgent(WXEnvironment.getApplication(),WXEnvironment.getConfig()); if(headers != null){ for (String key : headers.keySet()) { if (key.equals(KEY_USER_AGENT)) { UA = headers.getString(key); continue; } builder.putHeader(key, headers.getString(key)); } } builder.putHeader(KEY_USER_AGENT,UA); }
Example 11
Source File: TemplateController.java From Cynthia with GNU General Public License v2.0 | 5 votes |
/** * 保存表单邮件配置 * @param request * @param httpSession * @return * @throws Exception */ @ResponseBody @RequestMapping("/saveTemplateMailConfig.do") public String saveTemplateMailConfig(HttpServletRequest request , HttpSession httpSession) throws Exception { String templateIdStr = request.getParameter("templateId"); if (templateIdStr == null || templateIdStr.equals("")) { return ""; } Template template = das.queryTemplate(DataAccessFactory.getInstance().createUUID(templateIdStr)); if (template == null) { return ""; } TemplateMailOption tmo = template.getTemplateMailOption(); JSONObject templateMailOptions = JSONObject.parseObject(request.getParameter("templateMailOptions")); tmo.setSendMail(templateMailOptions.getString("sendMail").toString().equals("true")); tmo.setMailSubject(templateMailOptions.get("mailSubject").toString()); tmo.getActionUsers().clear(); JSONObject actionUsers = templateMailOptions.getJSONObject("actionUsers"); for (String actionId : actionUsers.keySet()) { tmo.setActionUser(actionId, actionUsers.getString(actionId)); } ErrorCode errorCode = das.updateTemplate(template); if(errorCode.equals(ErrorCode.success)){ das.updateCache(DataAccessAction.update, template.getId().getValue(),template); return "true"; }else{ return "false"; } }
Example 12
Source File: GrafanaTest.java From xian with Apache License 2.0 | 5 votes |
public static void main(String[] args) { JSONArray falconBeans = new JSONArray(); JSONObject factors = FactorCollector.collect(); for (String metric : factors.keySet()) falconBeans.addAll(new FalconBeanBuilder().buildAll(metric, factors.get(metric))); GrafanaService.grafana(falconBeans); }
Example 13
Source File: HttpMethed.java From gsc-core with GNU Lesser General Public License v3.0 | 5 votes |
/** * constructor. */ public static void printJsonContent(JSONObject responseContent) { logger.info("----------------------------Print JSON Start---------------------------"); for (String str : responseContent.keySet()) { logger.info(str + ":" + responseContent.get(str)); } logger.info("JSON content size are: " + responseContent.size()); logger.info("----------------------------Print JSON End-----------------------------"); }
Example 14
Source File: QueryDocController.java From EserKnife with Apache License 2.0 | 5 votes |
@RequestMapping(value = "/getFieldInfos") @ResponseBody public Object getFieldInfos( @RequestParam("clusterName") String clusterName, @RequestParam("indexName") String indexName ) { Map<String, Object> results = Maps.newHashMap(); if (StringUtils.isBlank(indexName)) { results.put("fieldInfos", Lists.newArrayList()); } else { ArrayList<Object> fields = Lists.newArrayList(); JestServiceImpl jestServiceImpl = (JestServiceImpl) SpringInit.getApplicationContext().getBean("jestServiceImpl"); try { String execute =jestServiceImpl.getMapping(clusterName,indexName).getJsonString(); Map<String, JSONObject> metaInfo = (Map<String, JSONObject>) JSON.parse(execute); if (metaInfo != null && metaInfo.get(indexName) != null && metaInfo.get(indexName).get("mappings") != null) { JSONObject mappings = (JSONObject) metaInfo.get(indexName).get("mappings"); for (String key : mappings.keySet()) { JSONObject eachMeta = mappings.getJSONObject(key); JSONObject properties = (JSONObject) eachMeta.get("properties"); for (String fName : properties.keySet()) { FieldInfo fieldInfo = new FieldInfo(); fieldInfo.setfName(fName); fields.add(fieldInfo); } } } results.put("fieldInfos", fields); } catch (Exception e) { e.printStackTrace(); } } return results; }
Example 15
Source File: DmetaSnapshotFile.java From distkv with BSD 3-Clause "New" or "Revised" License | 5 votes |
public Map<String, String> load() throws IOException, CodecException { final String s = FileUtils.readFileToString(new File(path)); JSONObject jsonObject = JSONObject.parseObject(s); Set<String> set = jsonObject.keySet(); Map<String, String> map = new TreeMap<>(); for (String key : set) { map.put(key,(String) jsonObject.get(key)); } return map; }
Example 16
Source File: HttpUtil.java From ES-Fastloader with Apache License 2.0 | 5 votes |
public static String doHttp(String url, JSONObject param, String body, HttpType httpType) { String logInfo = "url:" + url + ", param:" + param + ", body:" + body + ", type:" + httpType.getType(); if (param != null) { StringBuilder sb = new StringBuilder(); for (String key : param.keySet()) { sb.append(key).append("=").append(param.getString(key)).append("&"); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } url = url + "?" + sb.toString(); } String result = ""; try { switch (httpType) { case GET: return HttpUtil.get(url); case PUT: return HttpUtil.putJsonEntity(url, body); case POST: return HttpUtil.postJsonEntity(url, body); } return null; } catch (Throwable t) { return null; } }
Example 17
Source File: ManageTable.java From database-transform-tool with Apache License 2.0 | 4 votes |
/** * @decription 数据同步(Greenplum-->[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 greenplum2sql(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); 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); 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; } 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 18
Source File: ManageTable.java From database-transform-tool with Apache License 2.0 | 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 19
Source File: ManageTable.java From database-transform-tool with Apache License 2.0 | 4 votes |
/** * @decription 数据同步([MySQL|SQL Server|Oracle]-->[Greenplum]) * @author yi.zhang * @time 2017年8月4日 下午5:26:59 * @param source 数据源 * @param target 目标库 * @param mapper 表映射 * @param filter_columns 字段过滤 */ protected void sql2greenplum(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){ if(source==null||target==null){ return; } JDBCFactory factory = new JDBCFactory(); String driverName=null,url = null; if(source.getType()==3.1){//MySQL driverName="com.mysql.jdbc.Driver"; url = "jdbc:mysql://"+source.getServers()+(source.getPort()>0?":"+source.getPort():"")+"/"+source.getDatabase()+"?useUnicode=true&characterEncoding=UTF8"; } if(source.getType()==3.2){//SQL Server driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver"; url = "jdbc:microsoft://"+source.getServers()+(source.getPort()>0?":"+source.getPort():"")+";database="+source.getDatabase()+";encrypt=true;trustServerCertificate=false;useUnicode=true;characterEncoding=UTF8"; } if(source.getType()==3.3){//Oracle driverName="oracle.jdbc.driver.OracleDriver"; url = "jdbc:oracle:thin:@"+source.getServers()+(source.getPort()>0?":"+source.getPort():"")+":"+source.getDatabase(); } factory.init(driverName, url,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 20
Source File: ManageTable.java From database-transform-tool with Apache License 2.0 | 4 votes |
/** * @decription 数据同步([MySQL|SQL Server|Oracle]-->[MongoDB]) * @author yi.zhang * @time 2017年8月4日 下午5:26:59 * @param source 数据源 * @param target 目标库 * @param mapper 表映射 * @param filter_columns 字段过滤 */ protected void sql2mongodb(Config source,Config target,Map<String,String> mapper,List<String> filter_columns){ if(source==null||target==null){ return; } JDBCFactory factory = new JDBCFactory(); String driverName=null,url = null; if(source.getType()==3.1){//MySQL driverName="com.mysql.jdbc.Driver"; url = "jdbc:mysql://"+source.getServers()+(source.getPort()>0?":"+source.getPort():"")+"/"+source.getDatabase()+"?useUnicode=true&characterEncoding=UTF8"; } if(source.getType()==3.2){//SQL Server driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver"; url = "jdbc:microsoft://"+source.getServers()+(source.getPort()>0?":"+source.getPort():"")+";database="+source.getDatabase()+";encrypt=true;trustServerCertificate=false;useUnicode=true;characterEncoding=UTF8"; } if(source.getType()==3.3){//Oracle driverName="oracle.jdbc.driver.OracleDriver"; url = "jdbc:oracle:thin:@"+source.getServers()+(source.getPort()>0?":"+source.getPort():"")+":"+source.getDatabase(); } factory.init(driverName, url,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); } } }