远程读取elasticSearch数据库并导出数据「建议收藏」

远程读取elasticSearch数据库并导出数据「建议收藏」packageorg.elasticsearch.esTest;importjava.awt.List;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.util.ArrayList;importjava

大家好,又见面了,我是你们的朋友全栈君。

最近刚开完题,毕设是使用机器学习算法对电磁数据中的异常进行检测。所有的电磁数据都存储在分布式数据库es中,所以第一步需要导出数据,这两天写了点这部分的程序,已经导出部分数据。

package org.elasticsearch.esTest;

import java.awt.List;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.ExecutionException;
//maven管理依赖
import org.elasticsearch.action.admin.indices.stats.IndicesStatsAction;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.index.query.QueryBuilders.*;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.search.SearchHits;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/** * Hello world! * */
public class EsClient {
    static File trace = new File("E:/es data/emcas-2018.01.04_trace.txt");
    static File warning = new File("E:/es data/emcas-2018.01.04_warning.txt");
    static File other = new File("E:/es data/emcas-2018.01.04_other.txt");

    public static Client  getClient() throws IOException {
         Settings settings = ImmutableSettings.settingsBuilder()
                    .put("cluster.name", "estest1")
                    .build();
            TransportClient client = new TransportClient(settings).addTransportAddress(  
                    new InetSocketTransportAddress("10.10.41.153", 9300));
//          FileWriter fw = new FileWriter(article);
//          BufferedWriter bfw = new BufferedWriter(fw);
            return client; 
    }


   public static HashSet<String> write2File(Client client) throws IOException{
       long start = System.currentTimeMillis();
       int scrollSize = 1000;
       SearchResponse response = null; 
       FileWriter fw_trace1 = new FileWriter(trace);
       BufferedWriter bfw1 = new BufferedWriter(fw_trace1);

       FileWriter fw_warning1 = new FileWriter(warning);
       BufferedWriter bfw2 = new BufferedWriter(fw_warning1);

       FileWriter fw_other1 = new FileWriter(trace);
       BufferedWriter bfw3 = new BufferedWriter(fw_other1);

//     ArrayList<Integer>collectid = new ArrayList<Integer>();
       HashSet collectid = new HashSet();

       int i =0;
       while (response == null || response.getHits().hits().length != 0 && i <=1) {            
//         if(i % 100 == 0){
//             fw = new FileWriter(autoCreateFile(i/10+1));
//             BufferedWriter bfw1 = new BufferedWriter(fw); 
//             bfw = bfw1;
//             System.out.println("这是第"+i/10+"万条数据");
//         }           
           try{
           response = client.prepareSearch("emcas-2017.10.16")
                    .setQuery(QueryBuilders.matchAllQuery())
                    .setSize(scrollSize)                            
                    .setFrom(i*scrollSize)
//                  .setFrom(0)
                    .execute()
                    .actionGet();
           }
           catch (IndexMissingException e) {
             System.out.println("not found");
        }   




           SearchHits hits = response.getHits();

           int trace_count = 0;
           int warning_count =0;
           int other_count = 0;

           for(int j = 0 ; j < hits.getHits().length; j++){ 
   
               String jsonstr = hits.getHits()[j]
                       .getSourceAsString(); 
               JSONObject json_1 = JSON.parseObject(jsonstr);
               System.out.println(json_1);



               if(json_1.get("eventType").equals("trace")){
                   trace_count++;
                   collectid.add(json_1.get("collectorId"));
                   if(trace_count % 100000 == 0){
                       FileWriter fw_trace2 = new FileWriter(autoCreateFile(trace_count/100000));
                       BufferedWriter bfw_trace = new BufferedWriter(fw_trace2);
                       bfw1 = bfw_trace;
                   }
                   bfw1.write(json_1.toString()+'\r');
                   bfw1.flush();
               }else if(json_1.get("eventType").equals("warning")){
                   warning_count++;
                   if(warning_count % 100 == 0){
                       FileWriter fw_warning2 = new FileWriter(autoCreateFile(warning_count/100));
                       BufferedWriter bfw_warning2 = new BufferedWriter(fw_warning2);
                       bfw2 = bfw_warning2;
                   }
                   bfw2.write(json_1.toString()+'\r');
                   bfw2.flush();
               }else{
                   other_count++;
                   if(other_count % 100 == 0){
                       FileWriter fw_other2 = new FileWriter(autoCreateFile(other_count/100));
                       BufferedWriter bfw_other2 = new BufferedWriter(fw_other2);
                       bfw3 = bfw_other2;
                   }
                   bfw3.write(json_1.toString()+'\r');
                   bfw3.flush();
               }
              }                                           
           i++;
       }            
            bfw1.close();
            bfw2.close();
            bfw3.close();
            fw_other1.close();
            fw_trace1.close();
            fw_warning1.close();
            long end = System.currentTimeMillis();
            long totalTime = end - start;
            System.out.println("总耗时:"+totalTime);
            return collectid;
      }



   public static File autoCreateFile(int i ) throws IOException {
       File file = new File("E:/es data/"+i+".txt"); 
       file.createNewFile();
       return file;
   }




    public static void main(String[] args) throws InterruptedException, ExecutionException, IOException {
        EsClient instance = new EsClient();
        Client client = instance.getClient();
        HashSet hashSet = new HashSet();
        hashSet = write2File(client);
        for (Object object : hashSet) {
            System.out.println(object);
        }
        System.out.println(hashSet.size()+"size!!!!!!!!");
//      GetResponse response = client.prepareGet("emcas-2017.10.18","trace","AV8tK5NeSBmsIUk260HQ")
//      GetResponse response = client.prepareGet("emcas-2017.10.18","status","4")
//              .execute()
//              .actionGet(); 
//      System.out.println(response.getSource()); 
//用于计算es数据库中一个index下docs的总记录数
//      SearchResponse response2 = client.prepareSearch("emcas-2018.01.04")
//                  .setQuery(QueryBuilders.matchAllQuery())
//                  .setSize(0)                             
//                  .execute()
//                  .actionGet();
//      SearchHits hits = response2.getHits();
//      long hitscount = hits.getTotalHits();
//      System.out.println(hitscount);
    }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134374.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 用js来实现那些数据结构16(图02-图的遍历)

    上一篇文章我们简单介绍了一下什么是图,以及用JS来实现一个可以添加顶点和边的图。按照惯例,任何数据结构都不可或缺的一个point就是遍历。也就是获取到数据结构中的所有元素。那么图当然也不例外。这篇文章

    2022年3月25日
    38
  • eplan激活码破解步骤win10【2021.7最新】

    (eplan激活码破解步骤win10)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~MLZP…

    2022年3月21日
    1.4K
  • TCP和UDP协议的区别_朋友关系

    TCP和UDP协议的区别_朋友关系在解释两者之间的关系之前,我们必须从宏观的角度了解互联网的整个交互模型。因为当了解互联网在大体上是如何运作时,我们才能了解HTTP和TCP存在的意义,包括他们所要解决的问题是。 (此图来自Udacity的网络协议教程)互联网的模型被分为4层,从上至下每一层都依赖其底层协议。换言之,Application(应用层)的协议操作成功的前提是Transport(运输层)的存在。没有运输层就没有应…

    2026年2月4日
    4
  • 知识技能归档–CA-PKI体系-20210324

    知识技能归档–CA-PKI体系-20210324 

    2022年8月22日
    8
  • [转]Win 10 的 Win 按键没反应

    [转]Win 10 的 Win 按键没反应来自:http://www.pc0359.cn/article/win10/66397.html方法步骤:  1、首先我们需要将任务管理器运行出来。方法有两种,第一种:使用鼠标右键单击屏幕下方的任务栏,在弹出的选框中单击“任务管理器”即可打开;第二种:使用快捷键:Ctrl+Shift+Esc将其调出来。  2、进入任务管理器界面之后,结束桌面窗口管理器任务。  3、会…

    2022年5月12日
    37
  • flash 外国小游戏教程网站[通俗易懂]

    flash 外国小游戏教程网站[通俗易懂]http://www.tutorialized.com/tutorial/game-tutorial-part-1-character-movement/44240  相关的小游戏制作教程:有兴趣可以看看 http://www.emanueleferonato.com/2007/05/15/create-a-flash-racing-game-tutorial/

    2025年7月4日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号