es版本2.4.1 集群node地址与集群名称根据具体配置进行修改
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.bulk.Retry;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
public class EsTest {
private static String testjson = "{\"yjd\": 10,\"study_yjd\": 10}";
private static TransportClient client;
public static void main(String[] args) {
String hostString1 = "10.2.4.15";
String hostString2 = "10.2.4.42";
String hostString3 = "10.2.4.43";
int port = 9300;
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "TEST-15")
.put("transport.tcp.compress", true)
.build();
TransportAddress[] addressArr = new TransportAddress[3];
try {
addressArr[0] = new InetSocketTransportAddress(InetAddress.getByName(hostString1), port);
addressArr[1] = new InetSocketTransportAddress(InetAddress.getByName(hostString2), port);
addressArr[2] = new InetSocketTransportAddress(InetAddress.getByName(hostString3), port);
} catch (UnknownHostException e) {
e.printStackTrace();
}
client = TransportClient.builder().settings(settings).build().addTransportAddresses(addressArr);
BulkRequestBuilder brb = client.prepareBulk();
testEs( brb);
client.close();
}
public static void testEs(BulkRequestBuilder brb1){
for(int i=0;i<10;i++){
for(int j = 0 ;j<10;j++){
String json = "{\"gs\": 10,\"study_gs\": "+i+"}";
IndexRequest request = client.prepareIndex("test", "test").setSource(json).request();
brb1.add(request);
}
BulkResponse bulkResponse = brb1.execute().actionGet();
}
}
}
结果如图