一、报错内容
Exception in thread "main" ElasticsearchStatusException[Elasticsearch exception [type=security_exception, reason=missing authentication credentials for REST request [/user2?master_timeout=30s&ignore_unavailable=false&expand_wildcards=open%2Cclosed&allow_no_indices=true&ignore_throttled=false&timeout=30s]]] at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:176) at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:2011) at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1988) at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1745) at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1702) at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1672) at org.elasticsearch.client.IndicesClient.delete(IndicesClient.java:103) at com.xxx.elasticsearch.operation.main(operation.java:26) Suppressed: org.elasticsearch.client.ResponseException: method [DELETE], host [http://10.219.87.159:32705], URI [/user2?master_timeout=30s&ignore_unavailable=false&expand_wildcards=open%2Cclosed&allow_no_indices=true&ignore_throttled=false&timeout=30s], status line [HTTP/1.1 401 Unauthorized] {"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/user2?master_timeout=30s&ignore_unavailable=false&expand_wildcards=open%2Cclosed&allow_no_indices=true&ignore_throttled=false&timeout=30s]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/user2?master_timeout=30s&ignore_unavailable=false&expand_wildcards=open%2Cclosed&allow_no_indices=true&ignore_throttled=false&timeout=30s]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401} at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:326) at org.elasticsearch.client.RestClient.performRequest(RestClient.java:296) at org.elasticsearch.client.RestClient.performRequest(RestClient.java:270) at org.elasticsearch.client.RestHighLevelClient.performClientRequest(RestHighLevelClient.java:2082) at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1732) ... 4 more
二、报错内容
使用Flink输出(sink)到Elasticsearch时,出现了上面的错误,是因为登录到ES,ES启用了账号密码的模式,因此必须要使用账号密码才能够访问它。
三、报错解决
于是新增如下代码后,问题解决
ElasticsearchSink.BuilderesSinkBuilder = new ElasticsearchSink.Builder<>(httpHosts, new ElasticsearchSinkFunction () { public IndexRequest createIndexRequest(String element) { Map json = new HashMap<>(); json.put("data", element); return Requests.indexRequest() .index("您的索引名") //.type("my-type") .source(json); } @Override public void process(String element, RuntimeContext ctx, RequestIndexer indexer) { indexer.add(createIndexRequest(element)); } } ); // 鉴权,正对写 es 需要密码的场景 esSinkBuilder.setRestClientFactory(new HDRestClientFactory("您的账号名", "您的密码"));
public class HDRestClientFactory implements RestClientFactory { private String userName; private String password; transient CredentialsProvider credentialsProvider; public HDRestClientFactory(String userName, String password) { this.userName = userName; this.password = password; } @Override public void configureRestClientBuilder(RestClientBuilder restClientBuilder) { if (credentialsProvider == null) { credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); } restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) { builder.setConnectTimeout(5000); builder.setSocketTimeout(60000); builder.setConnectionRequestTimeout(2000); return builder; } }); } }
猜你喜欢
- 17天前(三亚太阳湾柏悦度假酒店)三亚太阳湾柏悦酒店携手ROSEONLY诺誓缔造浪漫七夕
- 17天前(札幌小樽市)2024年暑期飞往北海道避暑吧!札幌小樽city walk路线推荐
- 17天前(中国最好的避暑山庄)2025中国十大避暑山庄评选揭晓,澳涞山庄夺魁
- 17天前(中国旅游集团旗下酒店)中国旅游集团酒店控股有限公司战略投资雅阁酒店集团
- 17天前(美诺酒店集团旗下臻选品牌m collection)美诺酒店集团启动盛橡品牌战略焕新 开启全球扩张新篇章
- 17天前(辽宁新增6个国家4a级旅游景区有哪些)辽宁新增6个国家4A级旅游景区
- 17天前(殷建祥简历)全国十大牛商解码:殷建祥如何用178天技术突围打造星空梦星空房
- 17天前(福州“一县一桌菜”“两马乡宴”品鉴会圆满举办,马尾美食共叙血脉亲情)福州“一县一桌菜”“两马乡宴”品鉴会圆满举办,马尾美食共叙血脉亲情
- 17天前(泛舟诗海觅春迹什么意思)泛舟觅桃源,又一头部机构下场文旅赛道
- 17天前(阿斯塔纳航空属于哪个联盟)阿斯塔纳航空荣获Skytrax世界航空公司大奖,将继续助力中哈交流往来
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章