org.apache.kylin.jdbc.KylinClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kylin-jdbc Show documentation
Show all versions of kylin-jdbc Show documentation
Kylin JDBC Driver on Calcite Avatica
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kylin.jdbc;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.xml.bind.DatatypeConverter;
import org.apache.calcite.avatica.AvaticaParameter;
import org.apache.calcite.avatica.ColumnMetaData;
import org.apache.calcite.avatica.ColumnMetaData.Rep;
import org.apache.calcite.avatica.ColumnMetaData.ScalarType;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.kylin.jdbc.KylinMeta.KMetaCatalog;
import org.apache.kylin.jdbc.KylinMeta.KMetaColumn;
import org.apache.kylin.jdbc.KylinMeta.KMetaProject;
import org.apache.kylin.jdbc.KylinMeta.KMetaSchema;
import org.apache.kylin.jdbc.KylinMeta.KMetaTable;
import org.apache.kylin.jdbc.json.PreparedQueryRequest;
import org.apache.kylin.jdbc.json.QueryRequest;
import org.apache.kylin.jdbc.json.SQLResponseStub;
import org.apache.kylin.jdbc.json.StatementParameter;
import org.apache.kylin.jdbc.json.TableMetaStub;
import org.apache.kylin.jdbc.json.TableMetaStub.ColumnMetaStub;
import org.apache.kylin.jdbc.util.DefaultSslProtocolSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class KylinClient implements IRemoteClient {
private static final Logger logger = LoggerFactory.getLogger(KylinClient.class);
private final KylinConnection conn;
private final Properties connProps;
private final HttpClient httpClient;
private final ObjectMapper jsonMapper;
public KylinClient(KylinConnection conn) {
this.conn = conn;
this.connProps = conn.getConnectionProperties();
this.httpClient = new HttpClient();
this.jsonMapper = new ObjectMapper();
// trust all certificates
if (isSSL()) {
Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) new DefaultSslProtocolSocketFactory(), 443));
}
}
private boolean isSSL() {
return Boolean.parseBoolean(connProps.getProperty("ssl", "false"));
}
private String baseUrl() {
return (isSSL() ? "https://" : "http://") + conn.getBaseUrl();
}
private void addHttpHeaders(HttpMethodBase method) {
method.addRequestHeader("Accept", "application/json, text/plain, */*");
method.addRequestHeader("Content-Type", "application/json");
String username = connProps.getProperty("user");
String password = connProps.getProperty("password");
String basicAuth = DatatypeConverter.printBase64Binary((username + ":" + password).getBytes());
method.addRequestHeader("Authorization", "Basic " + basicAuth);
}
@Override
public void connect() throws IOException {
PostMethod post = new PostMethod(baseUrl() + "/kylin/api/user/authentication");
addHttpHeaders(post);
StringRequestEntity requestEntity = new StringRequestEntity("{}", "application/json", "UTF-8");
post.setRequestEntity(requestEntity);
httpClient.executeMethod(post);
if (post.getStatusCode() != 200 && post.getStatusCode() != 201) {
throw asIOException(post);
}
}
@Override
public KMetaProject retrieveMetaData(String project) throws IOException {
assert conn.getProject().equals(project);
String url = baseUrl() + "/kylin/api/tables_and_columns?project=" + project;
GetMethod get = new GetMethod(url);
addHttpHeaders(get);
httpClient.executeMethod(get);
if (get.getStatusCode() != 200 && get.getStatusCode() != 201) {
throw asIOException(get);
}
List tableMetaStubs = jsonMapper.readValue(get.getResponseBodyAsStream(), new TypeReference>() {
});
List tables = convertMetaTables(tableMetaStubs);
List schemas = convertMetaSchemas(tables);
List catalogs = convertMetaCatalogs(schemas);
return new KMetaProject(project, catalogs);
}
private List convertMetaCatalogs(List schemas) {
Map> catalogMap = new LinkedHashMap>();
for (KMetaSchema schema : schemas) {
List list = catalogMap.get(schema.tableCatalog);
if (list == null) {
list = new ArrayList();
catalogMap.put(schema.tableCatalog, list);
}
list.add(schema);
}
List result = new ArrayList();
for (List catSchemas : catalogMap.values()) {
String catalog = catSchemas.get(0).tableCatalog;
result.add(new KMetaCatalog(catalog, catSchemas));
}
return result;
}
private List convertMetaSchemas(List tables) {
Map> schemaMap = new LinkedHashMap>();
for (KMetaTable table : tables) {
String key = table.tableCat + "!!" + table.tableSchem;
List list = schemaMap.get(key);
if (list == null) {
list = new ArrayList();
schemaMap.put(key, list);
}
list.add(table);
}
List result = new ArrayList();
for (List schemaTables : schemaMap.values()) {
String catalog = schemaTables.get(0).tableCat;
String schema = schemaTables.get(0).tableSchem;
result.add(new KMetaSchema(catalog, schema, schemaTables));
}
return result;
}
private List convertMetaTables(List tableMetaStubs) {
List result = new ArrayList(tableMetaStubs.size());
for (TableMetaStub tableStub : tableMetaStubs) {
result.add(convertMetaTable(tableStub));
}
return result;
}
private KMetaTable convertMetaTable(TableMetaStub tableStub) {
List columns = new ArrayList(tableStub.getColumns().size());
for (ColumnMetaStub columnStub : tableStub.getColumns()) {
columns.add(convertMetaColumn(columnStub));
}
return new KMetaTable(tableStub.getTABLE_CAT(), tableStub.getTABLE_SCHEM(), tableStub.getTABLE_NAME(), tableStub.getTABLE_TYPE(), columns);
}
private KMetaColumn convertMetaColumn(ColumnMetaStub columnStub) {
return new KMetaColumn(columnStub.getTABLE_CAT(), columnStub.getTABLE_SCHEM(), columnStub.getTABLE_NAME(), columnStub.getCOLUMN_NAME(), columnStub.getDATA_TYPE(), columnStub.getTYPE_NAME(), columnStub.getCOLUMN_SIZE(), columnStub.getDECIMAL_DIGITS(), columnStub.getNUM_PREC_RADIX(), columnStub.getNULLABLE(), columnStub.getCHAR_OCTET_LENGTH(), columnStub.getORDINAL_POSITION(), columnStub.getIS_NULLABLE());
}
@Override
public QueryResult executeQuery(String sql, List params, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy