2015-04-04 40 views
6

在整個apache鑽取wiki中,我只能看到通過sqline客戶端運行的查詢。除了REST Api之外,是否有任何程式化的方法可以在Drill中運行查詢?任何樣品或指針?通過Java的Apache鑽取連接

還是等同於使用JDBC驅動程序來運行sql查詢?

回答

2

可以使用鑽JDBC驅動程序,這是記錄在這裏:http://drill.apache.org/docs/using-the-jdbc-driver/

注意如果你正在使用Maven構建Java程序,你需要在本地安裝鑽機依賴關係:

mvn install:install-file -Dfile=/opt/apache-drill-1.0.0/jars/drill-java-exec-1.0.0-rebuffed.jar -DgroupId=org.apache.drill.exec -DartifactId=drill-java-exec -Dversion=1.0.0 -Dpackaging=jar -DgeneratePom=true 
mvn install:install-file -Dfile=/opt/apache-drill-1.0.0/jars/drill-common-1.0.0-rebuffed.jar -DgroupId=org.apache.drill -DartifactId=drill-common -Dversion=1.0.0 -Dpackaging=jar -DgeneratePom=true 

而且這裏有一個例子:只爲JDBC部分https://github.com/vicenteg/DrillJDBCExample

+1

帶鑽1.1.0,鑽罐子現在是Maven中心。 – 2015-07-28 04:10:12

1

除了sqlline可以使用

  1. 鑽的Web UI運行查詢。安裝本地 (嵌入式)時,默認端口爲8047。
  2. 或者下載並配置MapR ODBC 驅動程序,它會自帶的Drill Explorer(另一個UI)可以使用 。 https://drill.apache.org/docs/installing-the-driver-on-windows/
  3. 或配置任何其他工具與ODBC工作(我已經配置 Teradata的SQL助手使用MAPR ODBC驅動程序鑽)
  4. JDBC集成 - 使用松鼠或DBVisualizer中,通過它可以運行 查詢。 https://drill.apache.org/docs/using-jdbc-with-squirrel-on-windows/
  5. 編程方式將通過JDBC。 (使用 鑽JDBC-全1.0.0jar和org.apache.drill.jdbc.Driver類)
2

,我用這樣的事情在我的Java代碼 -

------------- 
Dependency 
------------- 
    <dependency> 
     <groupId>org.apache.drill.exec</groupId> 
     <artifactId>drill-jdbc</artifactId> 
     <version>1.1.0</version> 
    </dependency> 

---------- 
Testcase 
---------- 
import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.util.Properties; 

import org.apache.drill.exec.ExecConstants; 
import org.apache.drill.jdbc.Driver; 

import org.testng.Assert; 
import org.testng.annotations.Test; 

public class TestDrillJdbcDriver { 

    /* Drill JDBC Uri for local/cluster zookeeper */ 
    public static final String DRILL_JDBC_LOCAL_URI = "jdbc:drill:zk=local"; 

    /* Sample query used by Drill */ 
    public static final String DRILL_SAMPLE_QUERY = "SELECT * FROM cp.`employee.json` LIMIT 20"; 


    @Test 
    public void testDrillJdbcDriver() throws Exception { 
    Connection con = null; 

    try { 
     con = new Driver().connect(DRILL_JDBC_LOCAL_URI, getDefaultProperties()); 
     Statement stmt = con.createStatement(); 
     ResultSet rs = stmt.executeQuery(DRILL_SAMPLE_QUERY); 

     int count = 0; 
     while (rs.next()) { 
     System.out.println(rs.getString(1)); 
     System.out.println(rs.getString(2)); 
     System.out.println(rs.getString(3)); 
     count++; 
     } 
     Assert.assertEquals(count, 20, "Twenty rows were expected."); 
    } catch (Exception ex) { 
     System.out.println(ex); 
    } finally { 
     if (con != null) { 
     con.close(); 
     } 
    } 
    } 

    public static Properties getDefaultProperties() { 
    final Properties properties = new Properties(); 
    properties.setProperty(ExecConstants.HTTP_ENABLE, "false"); 
    return properties; 
    } 
} 
-2
Other way to execute query by Calling REST API in Apache Drill 

公共類RequestQuery {

private String queryType; 
private String query; 

public String getQueryType() { 
    return queryType; 
} 
public void setQueryType(String queryType) { 
    this.queryType = queryType; 
} 
public String getQuery() { 
    return query; 
} 
public void setQuery(String query) { 
    this.query = query; 
} 

}

import javax.ws.rs.client.Client; 
import javax.ws.rs.client.ClientBuilder; 
import javax.ws.rs.client.Entity; 
import javax.ws.rs.client.WebTarget; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.ramyam.eis.apache.drill.api.model.RequestQuery; 

public class RestAPI { 

    private static Log logger = LogFactory.getLog(RestAPI.class); 
    public static void main(String[] args) { 
     getQueryResponce(); 
    } 

    private static void getQueryResponce(){ 
     Client client = null; 
     WebTarget target = null; 
     try { 
      logger.info("---------Started execution-----------"); 
      RequestQuery query = new RequestQuery(); 
      query.setQueryType("SQL"); 
      //MySQL 
      //query.setQuery("SELECT * FROM MYSQL.foodmart.collections"); 
      //query.setQuery("SELECT * FROM MYSQL.foodmart.collections limit 100"); 
      //query.setQuery("SELECT payment_due_from,NumItems FROM MYSQL.foodmart.collections limit 10"); 
      //query.setQuery("SELECT count(SPORTS_PREFERENCE) FROM MYSQL.foodmart.collections group by SPORTS_PREFERENCE"); 
      //query.setQuery("SELECT DISTINCT payment_due_from FROM MYSQL.foodmart.collections "); 

      //Mongo DB 
      //query.setQuery("select * from mongo.apache_drill.pt_BMS_preferences_data where SPORTS_PREFERENCE = 'Cricket' limit 10"); 
      //query.setQuery("SELECT COUNT(SPORTS_PREFERENCE) FROM mongo.apache_drill.pt_BMS_preferences_data GROUP BY SPORTS_PREFERENCE"); 
      query.setQuery("SELECT DISTINCT(SPORTS_PREFERENCE) FROM mongo.apache_drill.pt_BMS_preferences_data "); 
      client = ClientBuilder.newClient(); 
      target = client.target("http://localhost:8047/query.json"); 
      //target = target.path(path); 
      Response response = target.request().accept(MediaType.APPLICATION_JSON) 
        .post(Entity.json(query), Response.class); 
      if (response.getStatus() != 200) { 
       throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
      } 
      String string = response.readEntity(String.class); 
      logger.info(query.getQueryType()+"->"+query.getQuery()); 
      logger.info("Responce:\n"+string); 
      logger.info("---------End execution-----------"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      logger.error(e.getMessage(),e); 
     } 
    }