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);
}
}
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());
}
}
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);
}
}
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);
}
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));
}
Aggregations