gpt4 book ai didi

java - 使用elasticsearch为Oracle等现有关系数据库建立索引

转载 作者:行者123 更新时间:2023-12-02 11:18:38 25 4
gpt4 key购买 nike

我创建了与elasticsearch应用程序集成的Spring boot。现在可以为我自己在 postman 中输入的记录创建索引,并且还可以在 id 的帮助下搜索这些记录。

如何为 Oracle DB 记录创建索引,并且我想借助 id 或这些记录的任何其他列值来搜索这些记录。

请在下面找到我现有的代码。

Book.java

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Book {

private String id;
private String title;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

ElasticSearchConfiguration.java

@Configuration
public class ElasticSearchConfiguration extends AbstractFactoryBean<RestHighLevelClient> {

private static final Logger logger = LoggerFactory.getLogger(ElasticSearchConfiguration.class);
@Value("${spring.data.elasticsearch.cluster-nodes}")
private String clusterNodes;
@Value("${spring.data.elasticsearch.cluster-name}")
private String clusterName;
private RestHighLevelClient restHighLevelClient;

@Override
public void destroy() {
try {
if (restHighLevelClient != null) {
restHighLevelClient.close();
}
} catch (final Exception e) {
logger.error("Error closing ElasticSearch client: ", e);
}
}

@Override
public Class<RestHighLevelClient> getObjectType() {
return RestHighLevelClient.class;
}

@Override
public boolean isSingleton() {
return false;
}

@Override
public RestHighLevelClient createInstance() {
return buildClient();
}

private RestHighLevelClient buildClient() {
try {
restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
} catch (Exception e) {
logger.error(e.getMessage());
}
return restHighLevelClient;
}


}

BookController.java

@RestController
@RequestMapping("/books")
public class BookController {

private BookDao bookDao;

public BookController(BookDao bookDao) {
this.bookDao = bookDao;
}

@PostMapping
public Book insertBook(@RequestBody Book book) throws Exception{
return bookDao.insertBook(book);
}

@GetMapping("/{id}")
public Map<String, Object> getBookById(@PathVariable String id){
return bookDao.getBookById(id);
}

}
}

BookDao.java

@Repository
public class BookDao {

private final String INDEX = "bookdata";
private final String TYPE = "books";

private RestHighLevelClient restHighLevelClient;

private ObjectMapper objectMapper;

public BookDao( ObjectMapper objectMapper, RestHighLevelClient restHighLevelClient) {
this.objectMapper = objectMapper;
this.restHighLevelClient = restHighLevelClient;
}

public Book insertBook(Book book){
book.setId(UUID.randomUUID().toString());
Map<String, Object> dataMap = objectMapper.convertValue(book, Map.class);
IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, book.getId())
.source(dataMap);
try {
IndexResponse response = restHighLevelClient.index(indexRequest);
} catch(ElasticsearchException e) {
e.getDetailedMessage();
} catch (java.io.IOException ex){
ex.getLocalizedMessage();
}
return book;
}

public Map<String, Object> getBookById(String id){
GetRequest getRequest = new GetRequest(INDEX, TYPE, id);
GetResponse getResponse = null;
try {
getResponse = restHighLevelClient.get(getRequest);
} catch (java.io.IOException e){
e.getLocalizedMessage();
}
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
return sourceAsMap;
}

}

}

Maven 依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Elasticsearch Dependencies -->

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.1.2</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.1.2</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.1.2</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client-sniffer</artifactId>
<version>6.1.2</version>
</dependency>

</dependencies>

也请找到我的输入和输出:

我通过 Postman 按以下格式插入记录:

输入:

 {
"title": "Java Always",
}

输出:

{
"id": "be2e497f-585f-4dff-8d07-fbcee1dc1c80",
"title": "Java Always",
}

最佳答案

我建议使用logstash 。它是 Elastic 提供的数据收集和管道传输产品。它有一个 jdbc输入,然后您可以将其路由到 elasticsearch输出。

示例文件可能如下所示:

input {
jdbc {
jdbc_driver_library => "ojdbc6.jar"
jdbc_driver_class => "oracle.jdbc.OracleDriver"
jdbc_connection_string => "jdbc:oracle:thin:@localhost:1521/mydb"
jdbc_user => "user"
jdbc_password => "password"
statement => "SELECT * FROM my_table"
}
}

output {
elasticsearch {
hosts => ["localhost:9200"]
index => "my_index"
}
}

这应该将 my_table 中的所有内容放入名为 my_index 的索引中。您有很多选项(例如按计划运行数据库查询),因此请查看链接。

关于java - 使用elasticsearch为Oracle等现有关系数据库建立索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50082037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com