Elasticsearch5.2.2同步mysql数据库,设置mapping

该博客介绍了如何使用Elasticsearch5.2.2同步MySQL数据库的数据,并展示了如何设置mapping。首先初始化客户端连接,然后判断索引是否存在,如果不存在则创建索引并构建mapping。接着,通过BulkProcessor实现数据的批量导入,确保数据的高效同步。同时,博客中还展示了处理批量操作成功、失败的情况以及如何写入数据到Elasticsearch。
摘要由CSDN通过智能技术生成

package com.c4c.test;

import java.io.IOException;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;

import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;

import com.es.allpeizhi.EsAll;
/**
 *
 */
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * @author cuicunjin
 */
public class ESClient {

//    private Client client;
    static ConcurrentLinkedQueue<String>    queues        = new ConcurrentLinkedQueue<>();
    static AtomicBoolean                    isInsert    = new AtomicBoolean(true);
    static TransportClient                    client        = null;
    static TimeValue                        client1        = null;

    /**
     * 初始化客户端连接
     */
    @Before
    public static void main(String args[]) throws Exception {
        EsAll es = new EsAll();
        // 配置节点
        InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName(es.host), es.prot);
        // 配置settiong
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        System.out.println("ES--ip地址:" +

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值