|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | +package com.dtstack.flink.sql.sink.clickhouse; |
| 21 | + |
| 22 | + |
| 23 | +import com.dtstack.flink.sql.sink.IStreamSinkGener; |
| 24 | +import com.dtstack.flink.sql.sink.rdb.RdbSink; |
| 25 | +import com.dtstack.flink.sql.sink.rdb.format.RetractJDBCOutputFormat; |
| 26 | + |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | + |
| 31 | +public class ClickhouseSink extends RdbSink implements IStreamSinkGener<RdbSink> { |
| 32 | + |
| 33 | + private static final String CLICKHOUSE_DRIVER = "ru.yandex.clickhouse.ClickHouseDriver"; |
| 34 | + |
| 35 | + public ClickhouseSink() { |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public RetractJDBCOutputFormat getOutputFormat() { |
| 40 | + return new RetractJDBCOutputFormat(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void buildSql(String scheam, String tableName, List<String> fields) { |
| 45 | + buildInsertSql(tableName, fields); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String buildUpdateSql(String schema, String tableName, List<String> fieldNames, Map<String, List<String>> realIndexes, List<String> fullField) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + private void buildInsertSql(String tableName, List<String> fields) { |
| 54 | + String sqlTmp = "insert into " + tableName + " (${fields}) values (${placeholder})"; |
| 55 | + String fieldsStr = ""; |
| 56 | + String placeholder = ""; |
| 57 | + |
| 58 | + for (String fieldName : fields) { |
| 59 | + fieldsStr += ",`" + fieldName + "`"; |
| 60 | + placeholder += ",?"; |
| 61 | + } |
| 62 | + |
| 63 | + fieldsStr = fieldsStr.replaceFirst(",", ""); |
| 64 | + placeholder = placeholder.replaceFirst(",", ""); |
| 65 | + |
| 66 | + sqlTmp = sqlTmp.replace("${fields}", fieldsStr).replace("${placeholder}", placeholder); |
| 67 | + this.sql = sqlTmp; |
| 68 | + System.out.println("---insert sql----"); |
| 69 | + System.out.println(sql); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Override |
| 74 | + public String getDriverName() { |
| 75 | + return CLICKHOUSE_DRIVER; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +} |
0 commit comments