티스토리 뷰

Dev/AWS

[cloudsearch] Indexing Data Upload (java)

Happy Anko♡ 2021. 1. 5. 10:39

1. 데이터 포맷 

- Date : NVL2(CRT_DT, TO_CHAR(CRT_DT,'YYYY-MM-DD"T"HH24:MI:ss.')||'000Z', '') AS CRT_DT,

- 그외 필드들은 Null이 아니면 문제 없었던듯 

 

2. 업로드용 json 파일 만들기 

JSONArray list = new JSONArray();

for(TestVO testVO : testList){
  ObjectMapper objectMapper = new ObjectMapper();
  // null인 필드가 있으면 업로드시 오류 발생! 
  objectMapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);

  JSONObject object = new JSONObject();
  object.put("type", "add");
  object.put("id", testVO.getId());
  object.put("fields", objectMapper.writeValueAsString(testVO));
  list.add(object);
}

... 파일을 만들고 

File file = new File("json파일위치");
String endpoint = "search-test-blahblah.amazonaws.com";

// upload 
String indexingResult = AWSCloudSearchService.sendIndexing(file, endpoint);

 

 

3. 파일 업로드 

public String uploadDocuments(File file, String endpoint) {
  String profile = "cloudProfile";
  String region = Regions.AP_NORTHEAST_2.getName();

  AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder
      .EndpointConfiguration(endpoint, region);

  AmazonCloudSearchDomainClientBuilder builder = AmazonCloudSearchDomainClientBuilder
      .standard()
      .withCredentials(new ProfileCredentialsProvider(profile))
      .withEndpointConfiguration(endpointConfig);

  AmazonCloudSearchDomain amazonCloudSearchDomain = builder.build();

  InputStream docAsStream = new FileInputStream(file);
  UploadDocumentsRequest req = new UploadDocumentsRequest();
  req.setContentType(ContentType.Applicationjson);
  req.setContentLength(file.length());
  req.setDocuments(docAsStream);
  UploadDocumentsResult result = amazonCloudSearchDomain.uploadDocuments(req);

  return result.getStatus();
}

 

'Dev > AWS' 카테고리의 다른 글

AWS Cloudsearch 를 쓰지 않기로 한 이유  (1) 2021.03.18
Amazon CloudSearch를 사용하기로 한 이유  (0) 2020.11.06
CloudSearch vs. Elasticsearch  (0) 2017.03.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함