I use SimpleDriverDataSource and JdbcTemplate to connect to my drill server. In order to make this connection successful, I have to set the correct jdbcUrl. The problem is, I can set the drill server's port to any number, even larger than 65535, and the result is correct every time. I don't know why. Here's my code.
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.apache.drill.jdbc.Driver;
import java.util.List;
import java.util.Map;
public class TestDrill {
private final static Logger logger = Logger.getLogger(TestDrill.class);
public static void main(String[] args) {
Driver driver = new Driver();
String jdbcUrl = "jdbc:drill:drillbit=server1:66666,server2:66666,server3:66666";
SimpleDriverDataSource drillDs = new SimpleDriverDataSource(driver, jdbcUrl, "username", "password");
JdbcTemplate drillTemplate = new JdbcTemplate(drillDs);
try {
List<Map<String, Object>> dbs = drillTemplate.queryForList("show schemas");
logger.debug(dbs.toString());
logger.debug("show schemas succeeded");
} catch(DataAccessException e) {
logger.debug("show schemas failed");
}
}
}
P.S. If I set the wrong server hostname, the query would fail. This is normal. I just have no idea why I can set any port number and the query would still be successful.
Related
I'm not an expert with Wiremock and probably there's something wrong in my settings/configuration but I'm facing a weird scenario when it comes to verify a specific test.
if I run this specific test among the whole IT suite, the test fails on WireMockServer.verify(int count, RequestPatternBuilder requestPatternBuilder) but if I run the test alone, it works fine and the assertion is correct.
so what I did so far to understand this weird behavior is :
I set a break point the check if the actualCount inside the method was correct and indeed it is (only if run alone).
more over, I actually put some log messages to verify on the application side that this method has been invoked 4 times (one is the first request which throws exception and the other 3 are the so called retries)
I've debugged this specific use case by sending some requests with postman and the behaviour is what I expected
so at the moment I got no clue about what the heck is going on with this specific test when is running among the whole IT suite.
any input about what could it be the issue or why Wiremock behaves in this specific way?
Thank you in advance for the help & inputs :)
I have the same issue: tests finish successfully if I run them separately and fail when run with a class.
Wiremock version "com.github.tomakehurst:wiremock-jre8:2.33.1"
package com.kn.tic.web.client.jira;
import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.api.domain.util.UriUtil;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.kn.tic.config.ApplicationProperties;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.data.util.Pair;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.UUID;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
class CustomAsynchronousIssueRestClientTest {
private static final String USER = "user";
private static final String PASS = "pass";
private static final String HOST = "localhost";
private static final String PROTOCOL = "http";
private static final String JIRA_TRACKING_ID = "jiraTrackingId";
#TempDir
Path tempDir;
private final WireMockServer wireMockServer = new WireMockServer();
private JiraClient jiraClient;
#BeforeEach
public void setUp() {
this.wireMockServer.start();
ApplicationProperties applicationProperties = new ApplicationProperties();
applicationProperties.getJira().setUsername(USER);
applicationProperties.getJira().setPassword(PASS);
applicationProperties.getJira().setHost(HOST);
applicationProperties.getJira().setProtocol(PROTOCOL);
applicationProperties.getJira().setPort(wireMockServer.port());
this.jiraClient = new JiraClient(applicationProperties.getJira());
}
#AfterEach
void afterAll() {
this.wireMockServer.stop();
}
#Test
void testThatCustomCreateIssueImplementationAddsJiraTrackingIdToQuery() {
String jiraTrackingId = UUID.randomUUID().toString();
String uriPath = "/rest/api/latest/issue";
wireMockServer.stubFor(post(urlPathEqualTo(uriPath))
.withQueryParam(JIRA_TRACKING_ID, equalTo(jiraTrackingId))
.withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.APPLICATION_JSON_VALUE))
.withBasicAuth(USER, PASS)
.willReturn(
aResponse()
.withStatus(HttpStatus.SC_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.withBody("{\"id\":\"2197656\",\"key\":\"TTT-822\",\"self\":\"https://test-jira.com/rest/api/latest/issue/2197656\"}")
));
IssueInput issueInput = new IssueInputBuilder()
.setProjectKey("TCF")
.setIssueTypeId(5L)
.setSummary("summary")
.setReporterName("reporter")
.setDescription("description")
.build();
jiraClient.createIssue(issueInput, jiraTrackingId);
verify(postRequestedFor(urlPathEqualTo(uriPath))
.withQueryParam(JIRA_TRACKING_ID, equalTo(jiraTrackingId))
.withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.APPLICATION_JSON_VALUE)));
}
#Test
void testThatCustomAddAttachmentImplementationAddsJiraTrackingIdToQuery() throws URISyntaxException, IOException {
String jiraTrackingId = UUID.randomUUID().toString();
URI issueUri = new URI(String.format("%s://%s:%s/rest/api/latest/issue/2197654",PROTOCOL, HOST, wireMockServer.port()));
URI attachmentUri = UriUtil.path(issueUri, "attachments");
wireMockServer.stubFor(post(urlPathEqualTo(attachmentUri.getPath()))
.withQueryParam(JIRA_TRACKING_ID, equalTo(jiraTrackingId))
.withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.MULTIPART_FORM_DATA_VALUE))
.withBasicAuth(USER, PASS)
.willReturn(
aResponse()
.withStatus(HttpStatus.SC_OK)
));
Path tempFile = Files.createFile(tempDir.resolve("attachment.pdf"));
Files.writeString(tempFile, "test");
List<Pair<File, String>> attachments = List.of(Pair.of(tempFile.toFile(), "attachment.pdf"));
jiraClient.addAttachmentsToIssue(issueUri, attachments, jiraTrackingId);
verify(postRequestedFor(urlPathEqualTo(attachmentUri.getPath()))
.withQueryParam(JIRA_TRACKING_ID, equalTo(jiraTrackingId))
.withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.MULTIPART_FORM_DATA_VALUE)));
}
}
I am trying to create a listener on the HiveMetastore where i need to retrieve the query submitted to the metastore. Is there any way to retrieve the queryString?
In the MetatstoreListener we get events such as onCreate, onDelete etc.
It is possible to have postHook on the hive but need to have the listener on the metastore so that all the DDL commands are getting catched executed from anywhere
Is there any way to capture the events in the Metastore and apply the same events to another parallel Metastore setup?
context:- I am trying to upgrade the hive from 1.x version to 3.x.x
where the idea is to have the stateless setup of Metastore-service in Kubernetes.
but not sure how much the query syntax is compatible between both versions. So wanted to set hot-hot setup parallelly and monitor the results of queries. So if there is any way on the MetastoreListener to transfer the DDL events from one Metastore to another and execute simultaneously?
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.metastore.events.AlterTableEvent;
import org.apache.hadoop.hive.metastore.MetaStoreEventListener;
import org.apache.hadoop.hive.metastore.events.CreateTableEvent;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.time.LocalDateTime;
public class HiveMetastoreListener extends MetaStoreEventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(HiveMetastoreListener.class);
private static final ObjectMapper objMapper = new ObjectMapper();
private final DataProducer dataProducer = DataProducer.getInstance();
public HiveMetastoreListener(Configuration config) {
super(config);
}
/**
* Handler for a CreateTable Event
*/
#Override
public void onCreateTable(CreateTableEvent tableEvent) throws MetaException{
super.onCreateTable(tableEvent);
try {
String data = null;
dataProducer.produceToKafka("metastore_topic", LocalDateTime.now().toString(), data);
}catch (Exception e) {
System.out.println("Error:- " + e);
}
}
I created the connection between java and mysql as follows:
package conexao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://localhost/projetojava","xxxxxxxxxx","xxxxxxxx");
}
catch(SQLException excecao) {
throw new RuntimeException(excecao);
}
}
}
to test the connection I used this code:
package conexao;
import java.sql.Connection;
import java.sql.SQLException;
public class TestaConexao {
public static void main(String[] args) throws SQLException {
Connection connection = new ConnectionFactory().getConnection();
System.out.println("Conexão aberta!");
connection.close();
}
}
But I always get this error:
In-place deployment at \192.168.1.70\Server\html\Pouco Comum\build\web GlassFish Server 4.1.1, deploy, null, false
\\192.168.1.70\Server\html\Pouco Comum\nbproject\build-impl.xml:1048: The module has not been deployed.
See the server log for details. BUILD FAILED (total time: 0 seconds)
How can I solve?
MySQL port seems to be missing in JDBC connection URL. The default port is 3306.
Try below code:
return DriverManager.getConnection("jdbc:mysql://localhost:3306/projetojava","xxxxxxxxxx","xxxxxxxx");
i am brand new to Java . Here i am trying to connect to mysql database here my code:
package Services;
package Services;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class LoginService {
private static final String USERNAME="root";
private static final String PASSWORD="";
private static final String CONNECTION_STRING="jdbc:mysql://localhost/testdb";
public void Authenticate(String uname,String password){
Connection connection=null;
Statement statement=null;
ResultSet result =null;
try{
//Class.forName("com.mysql.jdbc.Driver").newInstance();//if java 6 or higher there is no need to load cass
connection =(Connection)DriverManager.getConnection(CONNECTION_STRING,USERNAME,PASSWORD);
System.out.println("Connection to the database is established ");
}catch(SQLException e){
System.out.println("Can't connect to db:" +e );
}
}
}
This end up with an error like this:
Can't connect to db:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/testdb
i have added the jar file and by right clicking in it added the same to build path;what wrong with my code .
You have to download the JDBC driver ..
after that ... add it to your library..
You can read this! It's really simple after you get the idea!
here is the link! http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html
And this one! http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/
Good luck!
We use JAAS in a heavily loaded web server. The configuration file is loaded from a file,
System.setProperty("java.security.auth.login.config", "/config/jaas.config");
During profiling, we noticed that the configuration is loaded from file for every login attempt. This is an I/O operation we try to avoid. Is there anyway to store the JAAS configuration in memory?
Following code snippet connects to a PostgreSQL database (using pgjdbc and HikariCP) with an in-memory JAAS configuration, that is, no Configuration files are required:
package com.vlkan.kerberos.auth;
import com.google.common.collect.ImmutableMap;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import static com.google.common.base.Preconditions.checkArgument;
public enum Main {
private static final String JAAS_CONFIG_NAME = "pgjdbc";
public static void main(String[] args) throws LoginException, SQLException {
String jdbcUrl = "jdbc:postgresql://host/dbname";
String jdbcDriver = "org.postgresql.Driver";
String username = "user";
String password = "pass";
Configuration jaasConfig = createJaasConfig();
Configuration.setConfiguration(jaasConfig);
HikariConfig hikariConfig = createHikariConfig(jdbcUrl, jdbcDriver, username, password);
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
try (Connection connection = dataSource.getConnection()) {
try (PreparedStatement statement = connection.prepareStatement("SELECT 1")) {
try (ResultSet resultSet = statement.executeQuery()) {
boolean next = resultSet.next();
checkArgument(next, "no results");
int result = resultSet.getInt(1);
checkArgument(result == 1, "expecting: 1, found: %s", result);
System.out.println("ok");
}
}
}
}
private static HikariConfig createHikariConfig(String jdbcUrl, String jdbcDriver, String username, String password) {
HikariConfig config = new HikariConfig();
config.setDriverClassName(jdbcDriver);
config.setJdbcUrl(jdbcUrl);
config.setUsername(username);
config.setPassword(password);
fixKerberosProperties(config, username, password);
return config;
}
private static void fixKerberosProperties(HikariConfig config, String username, String password) {
Properties properties = new Properties();
properties.setProperty("user", username);
properties.setProperty("password", password);
properties.setProperty("JAASConfigName", JAAS_CONFIG_NAME);
config.setDataSourceProperties(properties);
}
private static Configuration createJaasConfig() {
// Create entry options.
Map<String, Object> options = ImmutableMap.of(
"useFirstPass", "false", // Do *not* use javax.security.auth.login.{name,password} from shared state.
"debug", "true" // Output debug (including plain text username and password!) messages.
);
// Create entries.
AppConfigurationEntry[] entries = {
new AppConfigurationEntry(
"com.sun.security.auth.module.Krb5LoginModule",
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
options)
};
// Create configuration.
return new Configuration() {
#Override
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
checkArgument(JAAS_CONFIG_NAME.equals(name));
return entries;
}
};
}
}
You could implement your own Configuration. The javadoc says:
The default Configuration
implementation can be changed by
setting the value of the
"login.configuration.provider"
security property (in the Java
security properties file) to the fully
qualified name of the desired
Configuration implementation class.
The default implementation com.sun.security.auth.login.ConfigFile (source) appears to load the file each time the class is instantiated. You could cache the contents. No comment on the security aspects either way.