使用HTTP POST,以下脚本可以插入新字段createtime或更新lastupdatetime:
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc": {
"lastupdatetime": "2015-09-16T18:00:00"
}
"upsert" : {
"createtime": "2015-09-16T18:00:00"
"lastupdatetime": "2015-09-16T18:00",
}
}'
Run Code Online (Sandbox Code Playgroud)
但是在spark脚本中,设置后"es.write.operation": "upsert",我根本不知道如何插入createtime。只有es.update.script.*在正式文件。所以,任何人都可以给我一个例子吗?
更新:就我而言,我想将Android设备的信息从日志中保存为一种 Elasticsearch类型,并将其首次出现时间设置为createtime。如果该设备再次出现,则我只会更新lastupdatetime,而保持createtime原样。
所以文档id是android ID,如果id存在,则update lastupdatetime,否则insert createtime和lastupdatetime.So这里的设置是(在python中):
conf = {
"es.resource.write": "stats-device/activation",
"es.nodes": "NODE1:9200",
"es.write.operation": "upsert",
"es.mapping.id": "id"
# ???
}
rdd.saveAsNewAPIHadoopFile(
path='-',
outputFormatClass="org.elasticsearch.hadoop.mr.EsOutputFormat",
keyClass="org.apache.hadoop.io.NullWritable",
valueClass="org.elasticsearch.hadoop.mr.LinkedMapWritable",
conf=conf
)
Run Code Online (Sandbox Code Playgroud)
我只是不知道如何在不存在的情况下插入新字段id …
我DateTimeFormatterRegistrar在我的班级中注册了一个WebMvcConfigurer这样的实现:
@Override
public void addFormatters(FormatterRegistry registry) {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setUseIsoFormat(true);
registrar.registerFormatters(registry);
}
Run Code Online (Sandbox Code Playgroud)
在其余控制器中,我尝试将客户端GET参数解析为一个对象:
@GetMapping("/api/url/path")
public APIResponse getPersonAttendList(@Valid SampleVO vo){}
Run Code Online (Sandbox Code Playgroud)
SampleVO包括字段LocalDateTime time。如果客户端提供了错误的time参数格式,绑定将失败。服务器将返回 500,并像这样打印一些日志:
>ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] 175 - Servlet.service() for
servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;
nested exception is java.time.format.DateTimeParseException
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何捕获此异常并将 400 返回给客户端?它似乎ControllerAdvice不起作用。