需求:传入的好多个条件,不同条件之间的关系也是不同的。
案例JSON String json= "{\"code\":\"0\",\"msg\": \"\",\"searchtype\":\"1\",\"label_id\":\"\",\"county\":\"1|2|3|4\",\"lng_lat\": [{\"lng\":\"122.1231\"},{\"lat\":\"112.324324\"},{\"radius\":\"12\"}],"
+ "\"share_label\":[{\"child_english_label\":\"a|b|c|d\"},{\"chusan_gaosan_label\":\"a|b|c|d\"}],"
+ "\"high_label\":[{\"sex\":\"\"},{\"chusan_gaosan_label\":\"a|b|c|d\"}],"
+ "\"high_label_rang\":[{\"age\":\"12|25\"}]} ";
基本的SQL拼装,上代码
//最后返回的sql
public static String allSql(String json){
Map<String, String> base_map_map = getEachMap(DMPEntity.base_map,json);
Map<String, String> share_label_map = getEachMap(DMPEntity.share_label,json);
Map<String, String> high_label_map = getEachMap(DMPEntity.high_label,json);
Map<String, String> high_label_rang_map = getEachMap(DMPEntity.high_label_rang,json);
Map<String, String> lng_lat_map=new HashMap<String, String>();
String sql="";
// 字符串结构: 半径-lng1|lat1|lng2|lat2 ; 例如:[10-23.1232334|23.1232334|23.1232334|23.1232334]
// spark.udf().register("distance", new SpaceMeasure(), DataTypes.StringType); //注册udf
for(String key:base_map_map.keySet()){
String value=base_map_map.get(key);
//如果是查询类型是1:经纬度;2:区域;3:都为空
if(DMPEntity.searchtype.equals(key) && "1".equals(value)){
lng_lat_map = getEachMap(DMPEntity.lng_lat,json);
sql+=lng_lat_map.get(DMPEntity.radius)+"-"+lng_lat_map.get(DMPEntity.lng)+"|"+lng_lat_map.get(DMPEntity.lat)+"|lng|lat";
sql="select if(distance("+sql+"),0,1) as lnglat,* ";
}else if(DMPEntity.searchtype.equals(key) && "2".equals(value)){
String countries = base_map_map.get(DMPEntity.country);
String[] country = countries.split("\\|");
sql+="(";
for (String c : country) {
sql+=" "+countries+" like '%"+c+"%' or";
}
sql=sql.substring(0,sql.length()-2);
sql+=") ";
}
}
sql+=" from "+DMPEntity.table+" where 1=1 ";
String share_sql = jointSql(DMPEntity.share_label, share_label_map);
String high_sql = jointSql(DMPEntity.high_label, high_label_map);
String high_label_rang_sql = jointSql(DMPEntity.high_label_rang, high_label_rang_map);
return sql+share_sql+high_sql+high_label_rang_sql;
}
//拼接SQL,不同的标签类型之间不同的方式
public static String jointSql(String type,Map<String,String> map){
String sql="";
//如果是共享标签,则执行如下逻辑
if(DMPEntity.share_label.equals(type)){
sql+=" and (";
for ( String key : map.keySet()) {
String value=map.get(key);
String[] values = value.split("\\|");
for (String v : values) {
sql+=" "+key+" like '%"+v+"%' or";
}
}
sql=sql.substring(0,sql.length()-2);
sql+=") ";
}else if(DMPEntity.high_label.equals(type)){
for ( String key : map.keySet()) {
sql+="(";
String value=map.get(key);
String[] values = value.split("\\|");
for (String v : values) {
sql+=" "+key+" like '%"+v+"%' or";
}
sql=sql.substring(0,sql.length()-2);
sql+=") and ";
}
sql= " and " +sql.substring(0,sql.length()-4);
}else if(DMPEntity.high_label_rang.equals(type)){
for ( String key : map.keySet()) {
sql+="(";
String value=map.get(key);
String[] values = value.split("\\|");
if(values.length==2){
sql+=" "+values[0]+" <= "+key+" and "+key +" <="+values[1];
}
sql+=") and ";
}
sql= " and " +sql.substring(0,sql.length()-4);
}
return sql;
}
//封装map
public static Map<String,String> getEachMap(String key,String js){
JSONObject json;
Map<String,String> base_map=new HashMap<String,String>();
Map<String,String> other_map=new HashMap<String, String>();
try {
json = new JSONObject(js);
if(DMPEntity.base_map.equals(key)){
base_map.put(DMPEntity.code,json.getString(DMPEntity.code) );
base_map.put(DMPEntity.label_id,json.getString(DMPEntity.label_id) );
base_map.put(DMPEntity.searchtype, json.getString(DMPEntity.searchtype));
base_map.put(DMPEntity.msg, json.getString(DMPEntity.msg));
base_map.put(DMPEntity.country,json.getString(DMPEntity.country) );
return base_map;
}
other_map=getJsonMap(json.getJSONArray(key));
} catch (JSONException e) {
e.printStackTrace();
return null;
}
return other_map;
}
//封装map
public static Map<String,String> getJsonMap(JSONArray js){
Map<String,String> map=new HashMap<String, String>();
try {
for(int i = 0; i < js.length(); i++){
JSONObject jsonTmp;
jsonTmp = (JSONObject)js.getJSONObject(i);
Iterator keys = jsonTmp.keys();
if(keys.hasNext()){
String key = keys.next().toString();
String value = jsonTmp.get(key).toString();
map.put(key, value);
}
}
return map;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
常量类如下
package com.hdshu.spark.lzkprogram;
import java.io.Serializable;
public class DMPEntity implements Serializable{
private static final long serialVersionUID = 1L;
public static final String table="precisionmarketing.result_190905_sugang_jingqing_big_wide_label";
//基础类型标签
public static final String code="code";//0=成功;1=失败
public static final String msg="msg";//错误信息,code=0时,为空字符串
public static final String searchtype="searchtype";//1=经纬度;2=区域;3=区域和经纬度都不选
public static final String country="county";//[区域]当经type=2时为非空字符串
public static final String label_id="label_id";//标签id
public static final String base_map="base_map";//上面的五个基本类型都加到这里了
//json static 格式的类型标签
public static final String lng_lat="lng_lat";//经纬度
public static final String share_label="share_label";//共享标签
public static final String high_label="high_label";//高级标签
public static final String high_label_rang="high_label_rang";//高级标签中的范围查询
public static final String lng="lng";//经纬度
public static final String lat="lat";//经纬度
public static final String radius="radius";//经纬度
}
下图是个简化版
这是网上的一个简化版
String Value={"data":[{"school_name":"西北农林科技大学","school_id":"8"},{"school_name":"西北大学","school_id":"6"},{"school_name":"西北工业大学","school_id":"5"},{"school_name":"西北政法大学","school_id":"2"},{"school_name":"西安交通大学","school_id":"4"},{"school_name":"西安建筑科技大学","school_id":"10"},{"school_name":"西安电子科技大学","school_id":"3"},{"school_name":"西安邮电大学","school_id":"1"},{"school_name":"长安大学","school_id":"9"},{"school_name":"陕西师范大学","school_id":"7"},{"school_name":"陕西科技大学","school_id":"11"}],"msg":"列表获取成功","status":0}
用2个数组接收school_name和school_id
首先
public String[] schoolID;
public String[] schoolName;
JSONObject jsonObj_school;//用来接收解析的JSON字符串
JSONArray jsonArr_school ;//用来接收JSON对象里的数组
try{
jsonObj_school = new JSONObject ( Value ); //解析JSON字符串
jsonArr_school =jsonObj_school.getJSONArray("data");//接收JSON对象里的数组
int jsonSize_school = jsonArr_school.length();.//获取数组长度
schoolID = new String[ jsonSize_school ];//初始化数组
schoolName = new String[ jsonSize_school];//初始化数组
for(int i = 0; i < jsonSize_school; i++ )//通过循环取出数组里的值
{
JSONObject jsonTemp = (JSONObject)jsonArr_school.getJSONObject(i);
schoolID[i] = jsonTemp.getString("school_id");
schoolName[i] = jsonTemp.getString("school_name");
}
}catch(Exception e){
e.printStackTrace();
}