zoukankan      html  css  js  c++  java
  • Elasticsearch6.1.0 TransportClient滚动查询索引中所有数据写入文件中

    pom.xml:

    <dependencies>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>transport</artifactId>
                <version>6.7.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.9.1</version>
            </dependency>
    
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.14.3</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.47</version>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>RELEASE</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>com.beust</groupId>
                <artifactId>jcommander</artifactId>
                <version>1.72</version>
            </dependency>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>19.0</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.4</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.5</version>
            </dependency>
    
        </dependencies>
    
    
    
        <build>
            <plugins>
                <!--java版本-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <!--所有依赖打成jar包-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>1.2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>com.task.JobManager</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    View Code

    java:

    import org.elasticsearch.action.index.IndexResponse;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.TransportAddress;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.elasticsearch.index.query.RangeQueryBuilder;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.aggregations.AggregationBuilders;
    import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    
    public class EsToExport {
    
        private TransportClient client;
    
        private static final String FILE_PATH = "C:\Users\admin\Desktop\大数据\es\";
    
        @BeforeEach
        public void test1() throws UnknownHostException {
            Settings settings = Settings.builder().put("cluster.name", "UL_ES").build();
            client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.100.101"), 9301)).addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.100.102"), 9301));
        }
    
        
    
    
        /**
         * 范围值查询
         */
        @Test
        public void rangeQuery() throws IOException {
    
            SearchResponse searchResponse = client.prepareSearch("l_ftp")
                    .setTypes("doc")
                    .setQuery(new RangeQueryBuilder("x_begintime").gt(1589487556375L).lt(1589558400000L))
                    .setFetchSource(new String[]{"id", "seraddr_p", "seraddr_s"}, new String[]{})
                    .setScroll(new TimeValue(30000))
                    .setSize(10000)
                    .execute().actionGet();
            String fileBySysTime = DateUtil.createFileBySysTime(FILE_PATH);
            String filePath = fileBySysTime + File.separator + "-" + 123 + "-" + 68984968;
            BufferedWriter out = new BufferedWriter(new FileWriter(filePath, true));
    
            while (searchResponse.getHits().getHits().length != 0) {
                SearchHits hits = searchResponse.getHits();
                SearchHit[] hits1 = hits.getHits();
                for (SearchHit searchHit : hits1) {
                    String json = searchHit.getSourceAsString();
                    System.out.println(json);
                    out.write(json);
                    out.write("
    ");
                }
                searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(30000)).execute().actionGet();
            }
    
            System.out.println(filePath);
            out.close();
            client.close();
        }
    
       
    
    }
  • 相关阅读:
    WUSTOJ 1247: 递增或递减排序(Java)
    WUSTOJ 1246: 字符串排序(Java)
    Oracle查询部门工资最高员工的两种方法 1、MAX()函数 2、RANK()函数
    Oracle数据库——用户(USER)
    Firefox在新标签页打开“书签”和“搜索栏”(无需插件)
    Oracle数据库——ROWNUM
    Oracle数据库 SET ECHO [ON|OFF]
    WUST Oracle数据库 实验一实验二
    优课在线 嵌入式系统(胡威)2018春季测验 参考解析
    Oracle数据库——查询所有用户
  • 原文地址:https://www.cnblogs.com/chong-zuo3322/p/12974809.html
Copyright © 2011-2022 走看看