java elasticsearch修改某行数据整体步骤

一,连接集群

public void init(){  
        //on start相当于连接集群  
//        client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("192.168.203.148", 9300));  
        try {
client = TransportClient.builder().build()
       .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
    }

二,插入数据,Id与前插入数据的Id是一样的

/*  
     *创建index,把其中的文档转化为json的格式存储  
    */  
    public void createIndex() {  
//        for (int i=0; i<=200;i++){  
            IndexResponse indexResponse = null;  
            try {  
            List<Map<String, String>> maps = HotelDaos.findHotels();
            for (Map<String, String> map : maps) {
String hotelId = map.get("hotelId");
String jsonData = map.get("jsonData");
client.prepareIndex("testup", "testup1",hotelId).setSource(jsonData).execute().actionGet();
            System.out.println("it is ok !");
}
//                indexResponse = client.prepareIndex("logs", "log2017","1")  
//                                                    .setSource(  
//                                                    XContentFactory.jsonBuilder().startObject()  
//                                                        .field("sourceIp" , "10.10.16.2")  
//                                                        .field("sourcePort" , 989)  
//                                                        .field("destIp" , "114.114.114.118")  
//                                                        .endObject())  
//                                                        .execute()  
//                                                        .actionGet();  
            } catch (ElasticsearchException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            } catch (Exception e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
//            System.out.println("responseIsCreated: "+indexResponse.isCreated());  
//        }  
    } 

三,使用client 的get数据  , 查看数据前后变化  或使用插件head 查看

/*  
     * Get index 获取文档相当于读取数据库的一行数据  
     */  
    public void get(){  
        GetResponse getresponse = client.prepareGet("logs", "log2017", "0")  
                                     .execute()  
                                     .actionGet();  
        System.out.println(getresponse.getSourceAsString());  
    } 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值