Skip to content

Commit 0439225

Browse files
committed
hotfix 31226 实时写入message对应信息为字符串,转为json失败
1 parent 10d5a4c commit 0439225

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

flinkx-hive/flinkx-hive-writer/src/main/java/com/dtstack/flinkx/hive/writer/HiveOutputFormat.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@
3030
import com.dtstack.flinkx.restore.FormatState;
3131
import com.dtstack.flinkx.util.ExceptionUtil;
3232
import com.dtstack.flinkx.util.GsonUtil;
33+
import com.google.common.collect.Maps;
3334
import com.google.gson.JsonSyntaxException;
35+
import org.apache.commons.collections.CollectionUtils;
3436
import org.apache.commons.collections.MapUtils;
3537
import org.apache.commons.math3.util.Pair;
3638
import org.apache.flink.types.Row;
3739
import org.apache.hadoop.conf.Configuration;
3840

3941
import java.io.IOException;
4042
import java.io.Serializable;
41-
import java.util.HashMap;
42-
import java.util.Iterator;
43-
import java.util.List;
44-
import java.util.Map;
43+
import java.util.*;
4544

4645
/**
4746
* @author toutian
@@ -194,10 +193,25 @@ public void writeRecord(Row row) throws IOException {
194193
event = (Map) tempObj;
195194
} else if (tempObj instanceof String) {
196195
try {
197-
event = GsonUtil.GSON.fromJson((String) tempObj, GsonUtil.gsonMapTypeToken);
196+
List<String> columns = tableInfos.entrySet().iterator().next().getValue().getColumns();
197+
//如果写入的hive表字段包含column message,并且tempObj不符合简单json定义【以{开头 以}结尾】,同时不以message开头 则直接将tempObj作为value
198+
if(CollectionUtils.isNotEmpty(columns) && columns.size()==1 && columns.get(0).equals("message")){
199+
String data = (String) tempObj;
200+
if(!data.startsWith("{") || !data.endsWith("}") || !data.substring(1,data.length()-1).trim().startsWith("\"message\":")){
201+
event = Collections.singletonMap("message",data);
202+
}
203+
}else{
204+
event = GsonUtil.GSON.fromJson((String) tempObj, GsonUtil.gsonMapTypeToken);
205+
}
198206
}catch (JsonSyntaxException e){
207+
208+
List<String> columns = tableInfos.entrySet().iterator().next().getValue().getColumns();
209+
//如果hive没有message column,那么认为是脏数据 打印日志
210+
if(CollectionUtils.isNotEmpty(columns) && !columns.contains("message")){
211+
LOG.warn("bad json string:【{}】", tempObj);
212+
}
199213
// is not a json string
200-
LOG.warn("bad json string:【{}】", tempObj);
214+
event = Collections.singletonMap("message",tempObj);
201215
}
202216
}
203217
}

0 commit comments

Comments
 (0)