Elasticsearch5.x建立索引(Java)

獵手家園發表於2017-04-27

索引建立程式碼使用官方給的示例程式碼,我把它在java專案裡實現了一遍。

官方示例

1、建立索引

/**
 * Java建立Index
 */
public void CreateIndex() {
    int i = 0;
    List<IndexDomain> list = getIndexList();

    for (IndexDomain indexDomain : list) {
        if (indexExists(indexDomain.getIndex())) {
            continue;
        }

        // create indexDomain, setting, put mapping
        adminClient.prepareCreate(indexDomain.getIndex())
                .setSettings(Settings.builder()
                        .put("indexDomain.number_of_shards", indexDomain.getNumber_of_shards())
                        .put("indexDomain.number_of_replicas", indexDomain.getNumber_of_replicas())
                )
                .addMapping(indexDomain.getType(), indexDomain.getFieldsJson())
                .get();

        i++;
    }
    System.out.println("IndexDomain creation success! create " + i + " IndexDomain");
}

 

2、附一些其它小方法:

2.1 叢集初始化

private Client client;
private IndicesAdminClient adminClient;

/**
 * 構造方法 單例
 */
public Elasticsearch() {
    try {
        init();
    } catch (Exception e) {
        System.out.println("init() exception!");
        e.printStackTrace();
    }
    adminClient = client.admin().indices();
}


/**
 * 叢集配置初始化方法
 *
 * @throws Exception
 */
private void init() throws Exception {
    // 讀取配置檔案中資料
    Properties properties = readElasticsearchConfig();
    String clusterName = properties.getProperty("clusterName");
    String[] inetAddresses = properties.getProperty("hosts").split(",");

    Settings settings = Settings.builder().put("cluster.name", clusterName).build();

    client = new PreBuiltTransportClient(settings);

    for (int i = 0; i < inetAddresses.length; i++) {
        String[] tmpArray = inetAddresses[i].split(":");
        String ip = tmpArray[0];
        int port = Integer.valueOf(tmpArray[1]);

        client = ((TransportClient) client).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), port));
    }
}

 

2.2 讀取ES配置資訊

/**
 * 讀取ES配置資訊
 *
 * @return
 */
public Properties readElasticsearchConfig() {
    Properties properties = new Properties();
    try {
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("elasticsearch.properties");
        properties.load(new InputStreamReader(is, "UTF-8"));
    } catch (IOException e) {
        System.out.println("readEsConfig exception!");
        e.printStackTrace();
    }
    return properties;
}

elasticsearch.properties

hosts: 192.168.33.5:9300,192.168.33.50:9300  
clusterName: my-es-analyze

 

2.3 讀取json配置檔案

/**
 * 讀取json配置檔案
 *
 * @return JSONArray
 * @throws IOException
 */
public JSONArray readJosnFile() throws IOException {
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("index.json");
    BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    StringBuffer sb = new StringBuffer();
    String tmp = null;
    while ((tmp = br.readLine()) != null) {
        sb.append(tmp);
    }
    JSONArray result = JSONArray.parseArray(sb.toString());
    return result;
}

index.json

[
  {
    "index": "caixiao",
    "type": "compensate",
    "number_of_shards": 2,
    "number_of_replicas": 1,
    "fieldsSource": {
      "compensate": {
        "properties": {
          "name": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },"createtime": {
            "type": "date"
          }
        }
      }
    }
  }
]

 

2.4 判斷叢集中索引是否存在

/**
 * 判斷叢集中{Index}是否存在
 *
 * @param index
 * @return 存在(true)、不存在(false)
 */
public boolean indexExists(String index) {
    IndicesExistsRequest request = new IndicesExistsRequest(index);
    IndicesExistsResponse response = adminClient.exists(request).actionGet();
    if (response.isExists()) {
        return true;
    }
    return false;
}

 

2.5 建立索引實體類

/**
 * 獲取要建立的index列表
 *
 * @return List<IndexDomain>
 */
public List<IndexDomain> getIndexList() {

    List<IndexDomain> result = new ArrayList<IndexDomain>();
    JSONArray jsonArray = null;
    try {
        jsonArray = readJosnFile();
    } catch (IOException e) {
        System.out.println("readJsonFile exception!");
        e.printStackTrace();
    }

    if (jsonArray == null || jsonArray.size() == 0) {
        return null;
    }

    for (int i = 0; i < jsonArray.size(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        IndexDomain indexDomainObject = new IndexDomain();
        String index = jsonObject.getString("index");
        String type = jsonObject.getString("type");
        Integer number_of_shards = jsonObject.getInteger("number_of_shards");
        Integer number_of_replicas = jsonObject.getInteger("number_of_replicas");
        String fieldsSource = jsonObject.get("fieldsSource").toString();

        indexDomainObject.setIndex(index);
        indexDomainObject.setType(type);
        indexDomainObject.setFieldsJson(fieldsSource);
        indexDomainObject.setNumber_of_shards(number_of_shards);
        indexDomainObject.setNumber_of_replicas(number_of_replicas);
        result.add(indexDomainObject);
    }
    return result;
}

 

2.6 索引實體類

public class IndexDomain {
    private String index;                   //索引名
    private String type;                    //type表名
    private Integer number_of_shards;       //分片數
    private Integer number_of_replicas;     //備份數
    private String fieldsJson;              //欄位型別

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getFieldsJson() {
        return fieldsJson;
    }

    public void setFieldsJson(String fieldsJson) {
        this.fieldsJson = fieldsJson;
    }

    public Integer getNumber_of_shards() {
        return number_of_shards;
    }

    public void setNumber_of_shards(Integer number_of_shards) {
        this.number_of_shards = number_of_shards;
    }

    public Integer getNumber_of_replicas() {
        return number_of_replicas;
    }

    public void setNumber_of_replicas(Integer number_of_replicas) {
        this.number_of_replicas = number_of_replicas;
    }
}

 

2.7 log4j2.properties

appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout

rootLogger.level = info
rootLogger.appenderRef.console.ref = console

 

2.8 pom檔案依賴

<properties>
    <elasticsearch.version>5.3.0</elasticsearch.version>
    <log4j.version>2.8.2</log4j.version>
    <fastjson.version>1.2.31</fastjson.version>
</properties>

<dependencies>
    <!-- Elasticsearch -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>

    <!-- Log4j -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <!-- Alibaba Json -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${fastjson.version}</version>
    </dependency>
</dependencies>

 

相關文章