59 lines
2.1 KiB
Java
59 lines
2.1 KiB
Java
package eu.openaire.urls_controller.controllers;
|
|
|
|
import eu.openaire.urls_controller.configuration.ImpalaConnector;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.ResultSet;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping("/impala")
|
|
public class ImpalaController {
|
|
|
|
// This controller will test the connectivity with the database and return statistics!
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ImpalaController.class);
|
|
|
|
|
|
@GetMapping("get10PublicationIdsTest")
|
|
public ResponseEntity<?> get10PublicationIdsTest() {
|
|
|
|
Connection con = ImpalaConnector.getInstance().getConnection();
|
|
if ( con == null )
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Problem when connecting with the Impala-database!");
|
|
|
|
String query = "SELECT id FROM publication LIMIT 10;";
|
|
|
|
try ( ResultSet res = con.prepareStatement(query).executeQuery()) {
|
|
if ( !res.first() ) {
|
|
String errorMsg = "No results retrieved from the \"getAssignmentsQuery\"!";
|
|
logger.error(errorMsg);
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg);
|
|
}
|
|
|
|
List<String> publications = new ArrayList<>();
|
|
do {
|
|
publications.add(res.getString(0));
|
|
} while ( res.next() );
|
|
|
|
return new ResponseEntity<String>(publications.toString(), HttpStatus.OK);
|
|
|
|
} catch (Exception e) {
|
|
String errorMsg = "Problem when executing \"getAssignmentsQuery\": " + query;
|
|
logger.error(errorMsg, e);
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg);
|
|
} finally {
|
|
ImpalaConnector.closeConnection(con);
|
|
}
|
|
}
|
|
|
|
}
|