<commit_before>package com.nflabs.zeppelin.driver.hive;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.session.SessionState;
import org.apache.hadoop.hive.service.HiveInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nflabs.zeppelin.conf.ZeppelinConfiguration;
import com.nflabs.zeppelin.conf.ZeppelinConfiguration.ConfVars;
import com.nflabs.zeppelin.driver.ZeppelinConnection;
import com.nflabs.zeppelin.driver.ZeppelinDriverException;
import com.nflabs.zeppelin.driver.ZeppelinDriver;

public class HiveZeppelinDriver extends ZeppelinDriver {
	Logger logger = LoggerFactory.getLogger(HiveZeppelinDriver.class);
    private static String HIVE_SERVER = "org.apache.hadoop.hive.jdbc.HiveDriver";
    private static String HIVE_SERVER_2 = "org.apache.hive.jdbc.HiveDriver";

	private HiveInterface client;
	
	public HiveZeppelinDriver(ZeppelinConfiguration conf) {
		super(conf);
	}
	
	public void setClient(HiveInterface client){
		this.client = client;
	}

	private String getConnectionUri(){
		return getConf().getString("HIVE_CONNECTION_URI", "hive.connection.uri", "jdbc:hive2://");
	}
	
	/**
	 * HiveServer1 by default, but if uri starts with hive2 - assumes HiveServer2
	 * @param uri Hive URI
	 * @return JDBC driver class name
	 */
	private String getHiveDriverClass(String uri){
	    String className;
        if (uri.startsWith("jdbc:hive://")) { // HiveServer 
	        className = HIVE_SERVER;
	    } else { // HiveServer2
	        className = HIVE_SERVER_2;
	    }
		return getConf().getString("HIVE_DRIVER_CLASS", "hive.driver.class", className);
	}
	
	private String getLocalMetastore(){
		return getConf().getString(ConfVars.ZEPPELIN_HOME)+"/metastore_db";
	}
	
	private String getLocalWarehouse(){
		return new File(getConf().getString(ConfVars.ZEPPELIN_HOME)+"/warehouse").getAbsolutePath();
	}
	
	@Override
	public ZeppelinConnection getConnection() throws ZeppelinDriverException {
		try {
			Connection con;
			String uri = getConnectionUri();
			if (client!=null){ // create connection with given client instance
				logger.debug("Create connection from provided client instance");
				con = new HiveConnection(client);
			//else create instance using configuration files
			} else if(isEmpty(uri)){ 
				logger.debug("Create connection from hive configuration");
				con = new HiveConnection(hiveConf());
			} else if(uri.equals("jdbc:hive://")) { //local mode detected 
				logger.debug("Create connection from local mode");
				con = new HiveConnection(localHiveConf());
			} else { // remote connection using jdbc uri<commit_msg>Make hive driver use default warehouse dir in zeppelin's distr dir in local mode.<commit_after>package com.nflabs.zeppelin.driver.hive;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.session.SessionState;
import org.apache.hadoop.hive.service.HiveInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nflabs.zeppelin.conf.ZeppelinConfiguration;
import com.nflabs.zeppelin.conf.ZeppelinConfiguration.ConfVars;
import com.nflabs.zeppelin.driver.ZeppelinConnection;
import com.nflabs.zeppelin.driver.ZeppelinDriverException;
import com.nflabs.zeppelin.driver.ZeppelinDriver;

public class HiveZeppelinDriver extends ZeppelinDriver {
	Logger logger = LoggerFactory.getLogger(HiveZeppelinDriver.class);
    private static String HIVE_SERVER = "org.apache.hadoop.hive.jdbc.HiveDriver";
    private static String HIVE_SERVER_2 = "org.apache.hive.jdbc.HiveDriver";

	private HiveInterface client;
	
	public HiveZeppelinDriver(ZeppelinConfiguration conf) {
		super(conf);
	}
	
	public void setClient(HiveInterface client){
		this.client = client;
	}

	private String getConnectionUri(){
		return getConf().getString("HIVE_CONNECTION_URI", "hive.connection.uri", "jdbc:hive2://");
	}
	
	/**
	 * HiveServer1 by default, but if uri starts with hive2 - assumes HiveServer2
	 * @param uri Hive URI
	 * @return JDBC driver class name
	 */
	private String getHiveDriverClass(String uri){
	    String className;
        if (uri.startsWith("jdbc:hive://")) { // HiveServer 
	        className = HIVE_SERVER;
	    } else { // HiveServer2
	        className = HIVE_SERVER_2;
	    }
		return getConf().getString("HIVE_DRIVER_CLASS", "hive.driver.class", className);
	}
	
	private String getLocalMetastore(){
		return getConf().getString(ConfVars.ZEPPELIN_HOME)+"/metastore_db";
	}
	
	private String getLocalWarehouse(){
		return new File(getConf().getString(ConfVars.ZEPPELIN_HOME)+"/warehouse").getAbsolutePath();
	}
	
	@Override
	public ZeppelinConnection getConnection() throws ZeppelinDriverException {
		try {
			Connection con;
			String uri = getConnectionUri();
			if (client!=null){ // create connection with given client instance
				logger.debug("Create connection from provided client instance");
				con = new HiveConnection(client);
			//else create instance using configuration files
			} else if(isEmpty(uri)){ 
				logger.debug("Create connection from hive configuration");
				con = new HiveConnection(hiveConf());
			} else if(uri.equals("jdbc:hive://") || uri.equals("jdbc:hive2://")) { //local mode detected 
				logger.debug("Create connection from local mode");
				con = new HiveConnection(localHiveConf());
			} else { // remote connection using jdbc uri
{'fraction_non_alphanumeric': 0.11071107110711072, 'fraction_numerical': 0.0034203420342034203, 'mean_word_length': 4.692622950819672, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7254043', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1702, 'n_words': 377}