Java
Pinot provides a native java client to execute queries on the cluster. The client makes it easier for user to query data. The client is also tenant-aware and thus is able to redirect the queries to the correct broker.

Installation

You can use the client by including the following dependency -
Maven
Gradle
1
<dependency>
2
<groupId>org.apache.pinot</groupId>
3
<artifactId>pinot-java-client</artifactId>
4
<version>0.5.0</version>
5
</dependency>
Copied!
1
include 'org.apache.pinot:pinot-java-client:0.5.0'
Copied!
You can also build the code for java client locally and use it.

Usage

Here's an example of how to use the pinot-java-client to query Pinot.
1
import org.apache.pinot.client.Connection;
2
import org.apache.pinot.client.ConnectionFactory;
3
import org.apache.pinot.client.Request;
4
import org.apache.pinot.client.ResultSetGroup;
5
import org.apache.pinot.client.ResultSet;
6
7
/**
8
* Demonstrates the use of the pinot-client to query Pinot from Java
9
*/
10
public class PinotClientExample {
11
12
public static void main(String[] args) {
13
14
// pinot connection
15
String zkUrl = "localhost:2181";
16
String pinotClusterName = "PinotCluster";
17
Connection pinotConnection = ConnectionFactory.fromZookeeper(zkUrl + "/" + pinotClusterName);
18
19
String query = "SELECT COUNT(*) FROM myTable GROUP BY foo";
20
21
// set queryType=sql for querying the sql endpoint
22
Request pinotClientRequest = new Request("sql", query);
23
ResultSetGroup pinotResultSetGroup = pinotConnection.execute(pinotClientRequest);
24
ResultSet resultTableResultSet = pinotResultSetGroup.getResultSet(0);
25
26
int numRows = resultTableResultSet.getRowCount();
27
int numColumns = resultTableResultSet.getColumnCount();
28
String columnValue = resultTableResultSet.getString(0, 1);
29
String columnName = resultTableResultSet.getColumnName(1);
30
31
System.out.println("ColumnName: " + columnName + ", ColumnValue: " + columnValue);
32
}
33
}
Copied!

Connection Factory

The client provides a ConnectionFactory class to create connections to a Pinot cluster. The factory supports the following methods to create a connection -
    Zookeeper (Recommended) - Comma seperated list of zookeeper of the cluster. This is the recommended method which can redirect queries to appropriate brokers based on tenant/table.
    Broker list - Comma seperated list of the brokers in the cluster. This should only be used in standalone setups or for POC, unless you have a load balancer setup for brokers.
    Properties file - You can also put the broker list as brokerList in a properties file and provide the path to that file to the factory. This should only be used in standalone setups or for POC, unless you have a load balancer setup for brokers.
Here's an example demonstrating all methods of Connection factory -
1
Connection connection = ConnectionFactory.fromZookeeper
2
("some-zookeeper-server:2191/zookeeperPath");
3
4
Connection connection = ConnectionFactory.fromProperties("demo.properties");
5
6
Connection connection = ConnectionFactory.fromHostList
7
("broker-1:1234", "broker-2:1234", ...);
Copied!

Query Methods

You can run the query in both blocking as well as async manner. Use
    Connection.execute(org.apache.pinot.client.Request) for blocking queries
    Connection.executeAsync(org.apache.pinot.client.Request) for asynchronous queries that return a future object.
1
ResultSetGroup resultSetGroup =
2
connection.execute(new Request("sql", "select * from foo..."));
3
// OR
4
Future<ResultSetGroup> futureResultSetGroup =
5
connection.executeAsync(new Request("sql", "select * from foo..."));
Copied!
You can also use PreparedStatement to escape query parameters. We don't store the Prepared Statement in the database and hence it won't increase the subsequent query performance.
1
PreparedStatement statement =
2
connection.prepareStatement(new Request("sql", "select * from foo where a = ?"));
3
statement.setString(1, "bar");
4
5
ResultSetGroup resultSetGroup = statement.execute();
6
// OR
7
Future<ResultSetGroup> futureResultSetGroup = statement.executeAsync();
Copied!

Result Set

Results can be obtained with the various get methods in the first ResultSet, obtained through the getResultSet(int) method:
1
Request request = new Request("sql", "select foo, bar from baz where quux = 'quuux'");
2
ResultSetGroup resultSetGroup = connection.execute(request);
3
ResultSet resultTableResultSet = pinotResultSetGroup.getResultSet(0);
4
5
for (int i = 0; i < resultSet.getRowCount(); ++i) {
6
System.out.println("foo: " + resultSet.getString(i, 0));
7
System.out.println("bar: " + resultSet.getInt(i, 1));
8
}
Copied!

PQL Queries

If queryFormat pql is used in the Request, there are some differences in how the results can be accessed, depending on the query.
In the case of aggregation, each aggregation function is within its own ResultSet. A query with multiple aggregation function will return one result set per aggregation function, as they are computed in parallel.
1
ResultSetGroup resultSetGroup =
2
connection.execute(new Request("pql", "select max(foo), min(foo) from bar"));
3
4
System.out.println("Number of result groups:" +
5
resultSetGroup.getResultSetCount(); // 2, min(foo) and max(foo)
6
ResultSet resultSetMax = resultSetGroup.getResultSet(0);
7
System.out.println("Max foo: " + resultSetMax.getInt(0));
8
ResultSet resultSetMin = resultSetGroup.getResultSet(1);
9
System.out.println("Min foo: " + resultSetMin.getInt(0));
Copied!
In case of aggregation with GROUP BY, there will be as many ResultSets as the number of aggregations, each of which will contain multiple results grouped by a grouping key.
1
ResultSetGroup resultSetGroup =
2
connection.execute(
3
new Request("pql", "select min(foo), max(foo) from bar group by baz"));
4
5
System.out.println("Number of result groups:" +
6
resultSetGroup.getResultSetCount(); // 2, min(foo) and max(foo)
7
8
ResultSet minResultSet = resultSetGroup.getResultSet(0);
9
for(int i = 0; i < minResultSet.length(); ++i) {
10
System.out.println("Minimum foo for " + minResultSet.getGroupKeyString(i, 1) +
11
": " + minResultSet.getInt(i));
12
}
13
14
ResultSet maxResultSet = resultSetGroup.getResultSet(1);
15
for(int i = 0; i < maxResultSet.length(); ++i) {
16
System.out.println("Maximum foo for " + maxResultSet.getGroupKeyString(i, 1) +
17
": " + maxResultSet.getInt(i));
18
}
Copied!
This section is only applicable for PQL endpoint, which is deprecated and will be deleted soon. For more information about the endpoints, visit Querying Pinot.
Last modified 1mo ago