
com.intropro.prairie.unit.hive2.Hive2UnitClient Maven / Gradle / Ivy
package com.intropro.prairie.unit.hive2;
import com.intropro.prairie.comparator.ByLineComparator;
import com.intropro.prairie.comparator.CompareResponse;
import com.intropro.prairie.format.Format;
import com.intropro.prairie.format.InputFormatReader;
import com.intropro.prairie.format.exception.FormatException;
import com.intropro.prairie.unit.hdfs.HdfsUnit;
import com.intropro.prairie.utils.StringUtils;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Created by presidentio on 1/10/16.
*/
public class Hive2UnitClient implements Closeable {
private static final Logger LOGGER = LogManager.getLogger(Hive2UnitClient.class);
private static final String DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";
private static final long WAIT_START_TIMEOUT = TimeUnit.SECONDS.toMillis(5);
public static final String USER = System.getProperty("user.name");
private Connection connection;
private Statement statement;
private ByLineComparator byLineComparator = new ByLineComparator();
private FileSystem fileSystem;
private boolean open;
public Hive2UnitClient(String host, int port, FileSystem fileSystem) throws IOException {
this.fileSystem = fileSystem;
initConnection(host, port);
}
public void initConnection(String host, int port) throws IOException {
try {
Class.forName(DRIVER_NAME);
} catch (ClassNotFoundException e) {
throw new Error(e);
}
long startTime = System.currentTimeMillis();
SQLException mostRecentException = null;
while (connection == null && System.currentTimeMillis() - startTime < WAIT_START_TIMEOUT) {
try {
String jdbcUrl = String.format("jdbc:hive2://%s:%s/default", host, port);
LOGGER.info("Connecting to " + jdbcUrl);
connection = DriverManager.getConnection(jdbcUrl, USER, "");
break;
} catch (SQLException e) {
mostRecentException = e;
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
throw new Error(e1);
}
}
}
if (connection == null) {
throw new IOException("Failed to create hive connection", mostRecentException);
}
try {
statement = connection.createStatement();
} catch (SQLException e) {
throw new IOException("Failed to create hive statement", e);
}/*
try {
createDefaultDatabase();
} catch (SQLException e) {
LOGGER.warn("Failed to create default database: default", e);
}*/
}
private void createDefaultDatabase() throws SQLException {
statement.execute("CREATE DATABASE IF NOT EXISTS default");
}
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy