Skip to content

Commit 07a69af

Browse files
author
toutian
committed
Merge branch '1.8_cassandra_fix' into 'v1.8.0_dev'
cassandra plugin See merge request !160
2 parents 6dd208a + ab72b1e commit 07a69af

File tree

6 files changed

+63
-36
lines changed

6 files changed

+63
-36
lines changed

cassandra/cassandra-side/cassandra-all-side/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<configuration>
3939
<artifactSet>
4040
<excludes>
41-
41+
<exclude>org.slf4j</exclude>
4242
</excludes>
4343
</artifactSet>
4444
<filters>

cassandra/cassandra-side/cassandra-async-side/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<configuration>
5555
<artifactSet>
5656
<excludes>
57-
57+
<exclude>org.slf4j</exclude>
5858
</excludes>
5959
</artifactSet>
6060
<filters>

cassandra/cassandra-side/cassandra-async-side/src/main/java/com/dtstack/flink/sql/side/cassandra/CassandraAsyncReqRow.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,16 @@ public void asyncInvoke(Row input, ResultFuture<Row> resultFuture) throws Except
174174
return;
175175
}
176176
inputParams.add(equalObj);
177-
stringBuffer.append(sideInfo.getEqualFieldList().get(i))
178-
.append(" = ").append("'" + equalObj + "'")
179-
.append(" and ");
177+
StringBuffer sqlTemp = stringBuffer.append(sideInfo.getEqualFieldList().get(i))
178+
.append(" = ");
179+
if (equalObj instanceof String) {
180+
sqlTemp.append("'" + equalObj + "'")
181+
.append(" and ");
182+
} else {
183+
sqlTemp.append(equalObj)
184+
.append(" and ");
185+
}
186+
180187
}
181188

182189
String key = buildCacheKey(inputParams);
@@ -190,12 +197,12 @@ public void asyncInvoke(Row input, ResultFuture<Row> resultFuture) throws Except
190197
dealMissKey(input, resultFuture);
191198
return;
192199
} else if (ECacheContentType.MultiLine == val.getType()) {
193-
194-
for (Object rowArray : (List) val.getContent()) {
195-
Row row = fillData(input, rowArray);
196-
resultFuture.complete(Collections.singleton(row));
200+
List<Row> rowList = Lists.newArrayList();
201+
for (Object jsonArray : (List) val.getContent()) {
202+
Row row = fillData(input, jsonArray);
203+
rowList.add(row);
197204
}
198-
205+
resultFuture.complete(rowList);
199206
} else {
200207
throw new RuntimeException("not support cache obj type " + val.getType());
201208
}
@@ -206,7 +213,7 @@ public void asyncInvoke(Row input, ResultFuture<Row> resultFuture) throws Except
206213
//connect Cassandra
207214
connCassandraDB(cassandraSideTableInfo);
208215

209-
String sqlCondition = sideInfo.getSqlCondition() + " " + sqlWhere;
216+
String sqlCondition = sideInfo.getSqlCondition() + " " + sqlWhere + " ALLOW FILTERING ";
210217
System.out.println("sqlCondition:" + sqlCondition);
211218

212219
ListenableFuture<ResultSet> resultSet = Futures.transformAsync(session,
@@ -231,14 +238,15 @@ public void onSuccess(List<com.datastax.driver.core.Row> rows) {
231238
cluster.closeAsync();
232239
if (rows.size() > 0) {
233240
List<com.datastax.driver.core.Row> cacheContent = Lists.newArrayList();
241+
List<Row> rowList = Lists.newArrayList();
234242
for (com.datastax.driver.core.Row line : rows) {
235243
Row row = fillData(input, line);
236244
if (openCache()) {
237245
cacheContent.add(line);
238246
}
239-
resultFuture.complete(Collections.singleton(row));
247+
rowList.add(row);
240248
}
241-
249+
resultFuture.complete(rowList);
242250
if (openCache()) {
243251
putCache(key, CacheObj.buildCacheObj(ECacheContentType.MultiLine, cacheContent));
244252
}
@@ -285,7 +293,6 @@ public Row fillData(Row input, Object line) {
285293
}
286294
}
287295

288-
System.out.println("row:" + row.toString());
289296
return row;
290297
}
291298

cassandra/cassandra-side/cassandra-side-core/src/main/java/com/dtstack/flink/sql/side/cassandra/table/CassandraSideParser.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import com.dtstack.flink.sql.table.TableInfo;
2424
import com.dtstack.flink.sql.util.MathUtil;
2525

26+
import java.math.BigDecimal;
27+
import java.sql.Date;
28+
import java.sql.Timestamp;
2629
import java.util.Map;
2730
import java.util.regex.Matcher;
2831
import java.util.regex.Pattern;
@@ -96,4 +99,32 @@ public TableInfo getTableInfo(String tableName, String fieldsInfo, Map<String, O
9699

97100
private static void dealSideSign(Matcher matcher, TableInfo tableInfo) {
98101
}
102+
103+
public Class dbTypeConvertToJavaType(String fieldType) {
104+
switch (fieldType.toLowerCase()) {
105+
case "bigint":
106+
return Long.class;
107+
case "int":
108+
case "counter":
109+
return Integer.class;
110+
111+
case "text":
112+
case "inet":
113+
case "varchar":
114+
case "ascii":
115+
case "timeuuid":
116+
return String.class;
117+
118+
case "decimal":
119+
case "float":
120+
return Float.class;
121+
case "double":
122+
return Double.class;
123+
case "timestamp":
124+
return Timestamp.class;
125+
}
126+
127+
throw new RuntimeException("不支持 " + fieldType + " 类型");
128+
129+
}
99130
}

cassandra/cassandra-sink/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<configuration>
3434
<artifactSet>
3535
<excludes>
36-
36+
<exclude>org.slf4j</exclude>
3737
</excludes>
3838
</artifactSet>
3939
<filters>

cassandra/cassandra-sink/src/main/java/com/dtstack/flink/sql/sink/cassandra/CassandraOutputFormat.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,11 @@
4848
import com.datastax.driver.core.SocketOptions;
4949
import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy;
5050
import com.datastax.driver.core.policies.RetryPolicy;
51-
import com.dtstack.flink.sql.metric.MetricConstant;
52-
import org.apache.flink.api.common.io.RichOutputFormat;
51+
import com.dtstack.flink.sql.sink.MetricOutputFormat;
5352
import org.apache.flink.api.common.typeinfo.TypeInformation;
5453
import org.apache.flink.api.java.tuple.Tuple;
5554
import org.apache.flink.api.java.tuple.Tuple2;
5655
import org.apache.flink.configuration.Configuration;
57-
import org.apache.flink.metrics.Counter;
58-
import org.apache.flink.metrics.Meter;
59-
import org.apache.flink.metrics.MeterView;
6056
import org.apache.flink.types.Row;
6157
import org.slf4j.Logger;
6258
import org.slf4j.LoggerFactory;
@@ -73,7 +69,7 @@
7369
* @see Tuple
7470
* @see DriverManager
7571
*/
76-
public class CassandraOutputFormat extends RichOutputFormat<Tuple2> {
72+
public class CassandraOutputFormat extends MetricOutputFormat {
7773
private static final long serialVersionUID = -7994311331389155692L;
7874

7975
private static final Logger LOG = LoggerFactory.getLogger(CassandraOutputFormat.class);
@@ -94,17 +90,9 @@ public class CassandraOutputFormat extends RichOutputFormat<Tuple2> {
9490
protected String[] fieldNames;
9591
TypeInformation<?>[] fieldTypes;
9692

97-
private int batchInterval = 5000;
98-
9993
private Cluster cluster;
10094
private Session session = null;
10195

102-
private int batchCount = 0;
103-
104-
private transient Counter outRecords;
105-
106-
private transient Meter outRecordsRate;
107-
10896
public CassandraOutputFormat() {
10997
}
11098

@@ -120,7 +108,8 @@ public void configure(Configuration parameters) {
120108
* I/O problem.
121109
*/
122110
@Override
123-
public void open(int taskNumber, int numTasks) throws IOException {
111+
public void open(int taskNumber, int numTasks) {
112+
initMetric();
124113
try {
125114
if (session == null) {
126115
QueryOptions queryOptions = new QueryOptions();
@@ -176,17 +165,12 @@ public void open(int taskNumber, int numTasks) throws IOException {
176165
// 建立连接 连接已存在的键空间
177166
session = cluster.connect(database);
178167
LOG.info("connect cassandra is successed!");
179-
initMetric();
180168
}
181169
} catch (Exception e) {
182170
LOG.error("connect cassandra is error:" + e.getMessage());
183171
}
184172
}
185173

186-
private void initMetric() {
187-
outRecords = getRuntimeContext().getMetricGroup().counter(MetricConstant.DT_NUM_RECORDS_OUT);
188-
outRecordsRate = getRuntimeContext().getMetricGroup().meter(MetricConstant.DT_NUM_RECORDS_OUT_RATE, new MeterView(outRecords, 20));
189-
}
190174

191175
/**
192176
* Adds a record to the prepared statement.
@@ -226,6 +210,7 @@ private void insertWrite(Row row) {
226210
resultSet.wasApplied();
227211
}
228212
} catch (Exception e) {
213+
outDirtyRecords.inc();
229214
LOG.error("[upsert] is error:" + e.getMessage());
230215
}
231216
}
@@ -237,7 +222,11 @@ private String buildSql(Row row) {
237222
if (row.getField(index) == null) {
238223
} else {
239224
fields.append(fieldNames[index] + ",");
240-
values.append("'" + row.getField(index) + "'" + ",");
225+
if (row.getField(index) instanceof String) {
226+
values.append("'" + row.getField(index) + "'" + ",");
227+
} else {
228+
values.append(row.getField(index) + ",");
229+
}
241230
}
242231
}
243232
fields.deleteCharAt(fields.length() - 1);

0 commit comments

Comments
 (0)