mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
update中增加upsert判断
This commit is contained in:
@@ -100,34 +100,12 @@ public class ESTemplate {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public void update(ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
|
public void update(ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
|
||||||
append4Update(getBulk(), mapping, pkVal, esFieldData);
|
append4Update(mapping, pkVal, esFieldData);
|
||||||
commitBatch();
|
commitBatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void append4Update(BulkRequestBuilder bulkRequestBuilder, ESMapping mapping, Object pkVal,
|
|
||||||
Map<String, Object> esFieldData) {
|
|
||||||
if (mapping.get_id() != null) {
|
|
||||||
bulkRequestBuilder
|
|
||||||
.add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(), pkVal.toString())
|
|
||||||
.setDoc(esFieldData));
|
|
||||||
} else {
|
|
||||||
// TODO SearchResponse response =
|
|
||||||
// transportClient.prepareSearch(mapping.get_index())
|
|
||||||
// .setTypes(mapping.get_type())
|
|
||||||
// .setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal))
|
|
||||||
// .setSize(MAX_BATCH_SIZE)
|
|
||||||
// .get();
|
|
||||||
// for (SearchHit hit : response.getHits()) {
|
|
||||||
// bulkRequestBuilder
|
|
||||||
// .add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(),
|
|
||||||
// hit.getId())
|
|
||||||
// .setDoc(esFieldData));
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO XXX update by query
|
* update by query
|
||||||
*
|
*
|
||||||
* @param config
|
* @param config
|
||||||
* @param paramsTmp
|
* @param paramsTmp
|
||||||
@@ -154,7 +132,7 @@ public class ESTemplate {
|
|||||||
try {
|
try {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Object idVal = getIdValFromRS(mapping, rs);
|
Object idVal = getIdValFromRS(mapping, rs);
|
||||||
append4Update(getBulk(), mapping, idVal, esFieldData);
|
append4Update(mapping, idVal, esFieldData);
|
||||||
commitBatch();
|
commitBatch();
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
@@ -227,6 +205,32 @@ public class ESTemplate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void append4Update(ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
|
||||||
|
if (mapping.get_id() != null) {
|
||||||
|
if (mapping.isUpsert()) {
|
||||||
|
getBulk().add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(), pkVal.toString())
|
||||||
|
.setDoc(esFieldData)
|
||||||
|
.setDocAsUpsert(true));
|
||||||
|
} else {
|
||||||
|
getBulk().add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(), pkVal.toString())
|
||||||
|
.setDoc(esFieldData));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// TODO SearchResponse response =
|
||||||
|
// transportClient.prepareSearch(mapping.get_index())
|
||||||
|
// .setTypes(mapping.get_type())
|
||||||
|
// .setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal))
|
||||||
|
// .setSize(MAX_BATCH_SIZE)
|
||||||
|
// .get();
|
||||||
|
// for (SearchHit hit : response.getHits()) {
|
||||||
|
// bulkRequestBuilder
|
||||||
|
// .add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(),
|
||||||
|
// hit.getId())
|
||||||
|
// .setDoc(esFieldData));
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Object getValFromRS(ESMapping mapping, ResultSet resultSet, String fieldName,
|
public Object getValFromRS(ESMapping mapping, ResultSet resultSet, String fieldName,
|
||||||
String columnName) throws SQLException {
|
String columnName) throws SQLException {
|
||||||
String esType = getEsType(mapping, fieldName);
|
String esType = getEsType(mapping, fieldName);
|
||||||
|
|||||||
Reference in New Issue
Block a user