Skip to content

Commit

Permalink
Merge pull request #18 from zapr-oss/develop
Browse files Browse the repository at this point in the history
Releasing Druid Client and version bumped to 2.0
  • Loading branch information
GG-Zapr authored Apr 2, 2018
2 parents 4dd131f + e9d2ae7 commit 6c7f1bc
Show file tree
Hide file tree
Showing 14 changed files with 508 additions and 14 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Welcome to project Druidry!
=======================================

![build_status](https://api.travis-ci.org/zapr-oss/druidry.svg?branch=master)
![build_status](https://api.travis-ci.org/zapr-oss/druidry.svg?branch=master) [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)

Druid is an extremely popular tool to perform OLAP queries on event data. Druid drives real-time dashboards in most of the organisations right now. We@Zapr love Druid! Therefore we want to contribute towards making Druid, even more, friendlier to the ever expanding community.

Expand All @@ -27,6 +27,12 @@ This library is still growing and does not support each and every constructs, ho
Getting Started
---------------

Prerequisite
-----------

* Maven
* Java 8

Usage
-----

Expand Down Expand Up @@ -154,6 +160,19 @@ ObjectMapper mapper = new ObjectMapper();
String requiredJson = mapper.writeValueAsString(query);
```

``` java
DruidConfiguration config = DruidConfiguration
.builder()
.host("druid.io")
.endpoint("druid/v2/")
.build();

DruidClient client = new DruidJerseyClient(druidConfiguration);
client.connect();
List<DruidResponse> responses = client.query(query, DruidResponse.class);
client.close();
```

Supported Features
------------------

Expand Down
33 changes: 29 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

<groupId>in.zapr.druid</groupId>
<artifactId>druidry</artifactId>
<version>1.5</version>
<version>2.0</version>

<name>Druidry - Druid Query Generator</name>
<name>Druidry - Druid Java Client</name>
<description>Druidry is an open-source Java based utility library which supports creating
query to Druid
query to Druid and connection handling with Druid
</description>
<url>https://github.com/zapr-oss/druidry</url>

Expand All @@ -23,6 +23,7 @@
<logback.version>1.2.1</logback.version>
<jsonassert.version>1.4.0</jsonassert.version>
<commons.version>3.5</commons.version>
<jersey.version>2.26</jersey.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -83,6 +84,30 @@
<version>${commons.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>${jersey.version}</version>
</dependency>

</dependencies>

<profiles>
Expand Down Expand Up @@ -231,4 +256,4 @@
</repository>
</distributionManagement>

</project>
</project>
43 changes: 43 additions & 0 deletions src/main/java/in/zapr/druid/druidry/client/DruidClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package in.zapr.druid.druidry.client;

import java.util.List;

import in.zapr.druid.druidry.client.exception.ConnectionException;
import in.zapr.druid.druidry.client.exception.QueryException;
import in.zapr.druid.druidry.query.DruidQuery;

public interface DruidClient {

/**
* Connects with Druid
*
* @throws ConnectionException When connection is not formed
*/
void connect() throws ConnectionException;

/**
* Closes connection with Druid
* @throws ConnectionException When connection is not closed
*/
void close() throws ConnectionException;

/**
* Queries druid
*
* @param druidQuery Druid Query object
* @return Result from Druid
* @throws QueryException Error while querying
*/
String query(DruidQuery druidQuery) throws QueryException;

/**
* Queries druid
*
* @param druidQuery Druid Query Object
* @param className Class according to which DruidResult should be converted to
* @param <T> Class according to which DruidResult should be converted to
* @return Druid Result in the form of class T object
* @throws QueryException Error while querying
*/
<T> List<T> query(DruidQuery druidQuery, Class<T> className) throws QueryException;
}
104 changes: 104 additions & 0 deletions src/main/java/in/zapr/druid/druidry/client/DruidConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package in.zapr.druid.druidry.client;

import org.apache.commons.lang3.StringUtils;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class DruidConfiguration {

private static final int DEFAULT_HTTP_PORT = 8082;
private static final int DEFAULT_HTTPS_PORT = 8282;

/**
* Protocol by which Druid Broker is accessible
* Defaults to HTTP
*/
private DruidQueryProtocol protocol;

/**
* Address of Druid Broker Instance
*/
private String host;

/**
* Port at which Druid Broker is listening.
* {@value DEFAULT_HTTP_PORT} if protocol is 8082
* {@value DEFAULT_HTTPS_PORT} if protocol is 8282
*/
private Integer port;

/**
* Endpoint (without host address) at which query needs to be fired
*/
private String endpoint;

/**
* Number of connections to be maintained in connection pool
* Is Ignored when custom JerseyConfig is passed.
*/
private Integer concurrentConnectionsRequired;

@Builder
private DruidConfiguration(DruidQueryProtocol protocol,
String host,
Integer port,
String endpoint,
Integer concurrentConnectionsRequired) {

if (StringUtils.isEmpty(host)) {
throw new IllegalArgumentException("Host cannot be null or empty");
}

if (port != null && port < 0) {
throw new IllegalArgumentException("Port cannot be negative");
}

if (concurrentConnectionsRequired != null && concurrentConnectionsRequired < 1) {
throw new IllegalArgumentException("Connections required cannot be less than 1");
}

if (protocol == null) {
protocol = DruidQueryProtocol.HTTP;
}

if (port == null) {
port = getDefaultPortOnBasisOfProtocol(protocol);
}

this.protocol = protocol;
this.host = host;
this.port = port;
this.endpoint = endpoint;
this.concurrentConnectionsRequired = concurrentConnectionsRequired;
}

protected String getUrl() {

String endpoint = this.getEndpoint();
if (endpoint == null) {
endpoint = "";
}

return String.format("%s://%s:%d/%s",
this.getProtocol(),
this.getHost(),
this.getPort(),
endpoint);
}

private Integer getDefaultPortOnBasisOfProtocol(DruidQueryProtocol protocol) {

switch (protocol) {
case HTTP:
return DEFAULT_HTTP_PORT;
case HTTPS:
return DEFAULT_HTTPS_PORT;
default:
throw new IllegalArgumentException("Druid Query Protocol not handled");
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/in/zapr/druid/druidry/client/DruidError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package in.zapr.druid.druidry.client;

import lombok.Getter;

@Getter
public class DruidError {

private String error;
private String errorMessage;
private String errorClass;
private String host;
}
Loading

0 comments on commit 6c7f1bc

Please sign in to comment.