Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Merge branch 'rikcyhuo.replace'
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyHuo committed Dec 4, 2017
2 parents da46cca + 62cda41 commit 36ced58
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/main/java/com/sina/bip/hangout/outputs/Clickhouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,24 @@ protected void bulkInsert(Map event) throws Exception{
for (int j =0; j < fields.size(); j++) {
String field = fields.get(j);
// 判断元数据中是否有对应的key

if (e.containsKey(field)) {
if (e.get(field) instanceof String) {
String fieldValue = e.get(field).toString();
if (!(fieldValue.indexOf("'") > 0)){
value += "'" + e.get(field) + "'";
} else {
value += "''";
if (fieldValue.indexOf("\\'") > 0)
value += "'" + fieldValue.replace("'", "\\'").replace("\\\\'", "\\\\\\'") + "'";
else
value += "'" + fieldValue.replace("'", "\\'") + "'";
}
} else {
value += e.get(field);
if (e.get(field) == null){
value += "''";
} else {
value += e.get(field);
}
}
} else {
value += ClickhouseUtils.renderDefault(this.schema.get(field));
Expand All @@ -126,9 +134,14 @@ protected void bulkInsert(Map event) throws Exception{
try {
conn.createStatement().execute(sqls.toString());
} catch (SQLException e){
System.out.println(sqls.toString());
System.out.println(e.toString());
for (int i = 0; i < this.events.size(); i++) {
System.out.println(events.get(i));
}
// System.out.println("insert error");
} catch (Exception e) {
System.out.println(e.toString());
System.out.println("error");
}
conn.close();
Expand All @@ -140,6 +153,7 @@ protected void emit(Map event) {
try {
bulkInsert(event);
} catch (Exception e) {
System.out.println(e.toString());
System.out.println("insert error");
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/TestMain.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -6,8 +7,10 @@
*/
public class TestMain {
public static void main(String args[]) {
Map<String, String> map = new HashMap<String, String>();
map.put("a", "b");
System.out.println(map.get("h"));

String src = "a='b+\\'+c'";
System.out.println(src);
System.out.println(src.replace("'", "\\'"));
System.out.println(src.replace("'", "\\'").replace("\\\\'", "\\\\\\'"));
}
}

0 comments on commit 36ced58

Please sign in to comment.