Search in sources :

Example 1 with User

use of com.demo.es.entity.User in project learn by th997.

the class DataMImportTest method testInsert.

@Test
void testInsert() throws Exception {
    System.out.println(mongoClient.listDatabaseNames().first());
    MongoCollection userTable = mongoClient.getDatabase("test").getCollection("user");
    // 导入2000w数据
    BufferedReader br = IOUtils.toBufferedReader(new InputStreamReader(new FileInputStream("/mnt/l/data/2000w"), "gbk"));
    List<User> users = new ArrayList<>();
    int count = 0;
    String line;
    long start = System.currentTimeMillis();
    while ((line = br.readLine()) != null) {
        String[] ss = line.split(",");
        if (ss.length != 33) {
            // System.out.println(ss);
            continue;
        }
        count++;
        User user = new User();
        for (int i = 1; i <= 33; i++) {
            BeanUtils.getPropertyDescriptor(User.class, "c" + i).getWriteMethod().invoke(user, ss[i - 1]);
        }
        users.add(user);
        if (users.size() >= 10000) {
            userMRepository.saveAll(users);
            // mongoTemplate.insert(users,User.class);
            users.clear();
            long cost = System.currentTimeMillis() - start;
            System.out.println("count=" + count + ",cost=" + cost);
        // break;
        }
    }
    if (users.size() > 0) {
        userMRepository.saveAll(users);
    }
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) User(com.demo.es.entity.User) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with User

use of com.demo.es.entity.User in project learn by th997.

the class UserRepositoryTest method testCount.

// https://github.com/a601942905git/boot-example/blob/master/boot-example-advance/boot-example-elasticsearch/src/test/java/com/boot/example/EsTest.java
// https://blog.csdn.net/wenwen513/article/details/85163168
// https://blog.csdn.net/lixiang19971019/article/details/105009148
// https://elasticsearchjava-api.readthedocs.io/en/latest/aggregation.html
@Test
public void testCount() {
    NativeSearchQuery query = new NativeSearchQueryBuilder().addAggregation(AggregationBuilders.count("count_c7").field("c7")).withSourceFilter(new FetchSourceFilterBuilder().build()).build();
    SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class);
    for (String key : searchHits.getAggregations().asMap().keySet()) {
        ParsedValueCount value = searchHits.getAggregations().get(key);
        System.out.println(key + " " + value.getValue());
    }
}
Also used : User(com.demo.es.entity.User) ParsedValueCount(org.elasticsearch.search.aggregations.metrics.ParsedValueCount) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) FetchSourceFilterBuilder(org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with User

use of com.demo.es.entity.User in project learn by th997.

the class DataImportTest method testInsert.

@Test
void testInsert() throws Exception {
    // 导入2000w数据
    BufferedReader br = IOUtils.toBufferedReader(new InputStreamReader(new FileInputStream("/home/th/Downloads/data/2000w"), "gbk"));
    List<User> users = new ArrayList<>();
    int count = 0;
    String line;
    long start = System.currentTimeMillis();
    while ((line = br.readLine()) != null) {
        String[] ss = line.split(",");
        if (ss.length != 33) {
            // System.out.println(ss);
            continue;
        }
        User user = new User();
        for (int i = 1; i <= 33; i++) {
            BeanUtils.getPropertyDescriptor(User.class, "c" + i).getWriteMethod().invoke(user, ss[i - 1]);
        }
        users.add(user);
        count++;
        if (count < 0) {
            continue;
        }
        if (count % 1000 == 0) {
            // System.out.println(user);
            // userRepository.saveAll(users);
            users.clear();
            long cost = System.currentTimeMillis() - start;
            System.out.println("cost count=" + count + ",cost=" + cost);
            break;
        }
    }
    if (users.size() > 0) {
    // userRepository.saveAll(users);
    }
}
Also used : User(com.demo.es.entity.User) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with User

use of com.demo.es.entity.User in project learn by th997.

the class UserMRepositoryTest method testFind.

@Test
void testFind() {
    mongoTemplate.indexOps(User.class).ensureIndex(new Index().background().on("c1", Sort.Direction.DESC));
    mongoTemplate.indexOps(User.class).ensureIndex(new Index().background().on("c2", Sort.Direction.DESC));
    List<User> obj = userRepository.findByC1("test");
    System.out.println(obj);
    System.out.println(obj.size());
    obj.forEach(u -> {
        u.setC2("test");
        u.setC4(null);
    });
    userRepository.save(obj.get(0));
    userRepository.saveAll(obj);
}
Also used : User(com.demo.es.entity.User) Index(org.springframework.data.mongodb.core.index.Index) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with User

use of com.demo.es.entity.User in project learn by th997.

the class UserRepositoryTest method testScroll.

// https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference
@Test
public void testScroll() {
    int scrollTimeInMillis = 60000;
    IndexCoordinates index = IndexCoordinates.of("user");
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withPageable(PageRequest.of(0, 10000)).build();
    SearchScrollHits<User> scroll = elasticsearchRestTemplate.searchScrollStart(scrollTimeInMillis, searchQuery, User.class, index);
    String scrollId = scroll.getScrollId();
    int count = 0;
    while (scroll.hasSearchHits()) {
        // scroll.getSearchHits().forEach(userSearchHit -> {
        // System.out.println(userSearchHit.getContent());
        // });
        count += scroll.getSearchHits().size();
        System.out.println(count);
        scrollId = scroll.getScrollId();
        scroll = elasticsearchRestTemplate.searchScrollContinue(scrollId, scrollTimeInMillis, User.class, index);
    }
    elasticsearchRestTemplate.searchScrollClear(Arrays.asList(scrollId));
}
Also used : User(com.demo.es.entity.User) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.demo.es.entity.User)9 Test (org.junit.jupiter.api.Test)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)5 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)5 FetchSourceFilterBuilder (org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder)4 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 ParsedStringTerms (org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms)2 ParsedCardinality (org.elasticsearch.search.aggregations.metrics.ParsedCardinality)2 ParsedValueCount (org.elasticsearch.search.aggregations.metrics.ParsedValueCount)2 Pageable (org.springframework.data.domain.Pageable)2 IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)2 UserRepository (com.demo.es.repository.UserRepository)1 MongoCollection (com.mongodb.client.MongoCollection)1 Arrays (java.util.Arrays)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 QueryBuilders.matchAllQuery (org.elasticsearch.index.query.QueryBuilders.matchAllQuery)1