Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.source.hive;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.kylin.common.util.DBUtils;
import org.apache.kylin.common.util.SourceConfigurationUtil;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
public class BeelineHiveClient implements IHiveClient {
private static final String HIVE_AUTH_USER = "user";
private static final String HIVE_AUTH_PASSWD = "password";
private Connection cnct;
private Statement stmt;
private DatabaseMetaData metaData;
public BeelineHiveClient(String beelineParams) {
if (StringUtils.isEmpty(beelineParams)) {
throw new IllegalArgumentException("BeelineParames cannot be empty");
}
String[] splits = StringUtils.split(beelineParams);
String url = "", username = "", password = "";
for (int i = 0; i < splits.length - 1; i++) {
if ("-u".equals(splits[i])) {
url = stripQuotes(splits[i + 1]);
}
if ("-n".equals(splits[i])) {
username = stripQuotes(splits[i + 1]);
}
if ("-p".equals(splits[i])) {
password = stripQuotes(splits[i + 1]);
}
if ("-w".equals(splits[i])) {
File file = new File(splits[i + 1]);
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
password = br.readLine();
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Properties jdbcProperties = SourceConfigurationUtil.loadHiveJDBCProperties();
jdbcProperties.put(HIVE_AUTH_PASSWD, password);
jdbcProperties.put(HIVE_AUTH_USER, username);
this.init(url, jdbcProperties);
}
private void init(String url, Properties hiveProperties) {
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
cnct = DriverManager.getConnection(url, hiveProperties);
stmt = cnct.createStatement();
metaData = cnct.getMetaData();
} catch (SQLException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private String stripQuotes(String input) {
if (input.startsWith("'") && input.endsWith("'")) {
return StringUtils.strip(input, "'");
} else if (input.startsWith("\"") && input.endsWith("\"")) {
return StringUtils.strip(input, "\"");
} else {
return input;
}
}
public List getHiveDbNames() throws Exception {
List ret = Lists.newArrayList();
ResultSet schemas = metaData.getSchemas();
while (schemas.next()) {
ret.add(String.valueOf(schemas.getObject(1)));
}
DBUtils.closeQuietly(schemas);
return ret;
}
public List getHiveTableNames(String database) throws Exception {
List ret = Lists.newArrayList();
ResultSet tables = metaData.getTables(null, database, null, null);
while (tables.next()) {
ret.add(String.valueOf(tables.getObject(3)));
}
DBUtils.closeQuietly(tables);
return ret;
}
@Override
public long getHiveTableRows(String database, String tableName) throws Exception {
ResultSet resultSet = null;
long count = 0;
try {
String query = "select count(*) from ";
resultSet = stmt.executeQuery(query.concat(database + "." + tableName));
if (resultSet.next()) {
count = resultSet.getLong(1);
}
} finally {
DBUtils.closeQuietly(resultSet);
}
return count;
}
@Override
public List