【SQL】 Apache Drill に ODBC で接続してみた

OSX10.10.5 で Apache Drill (v1.0.0)に ODBC (Open Database Connectivity)でアクセスしてみました。基本的には, 公式チュートリアルに沿って設定できると思いますが個人的なメモとして残しておきます。

ODBC環境の設定

Drillへ接続するための Interfaceは下記が用意されている。[1]

  • Drill shell
  • Drill Web Console
  • ODBC/JDBC
  • C++ API

まず, iODBCをインストールする。Installing the Driver on Mac OS X から MapR Drill ODBC Driverもインストールして, 下記コマンドで確認する。

$ pkgutil --info mapr.drillodbc
package-id: mapr.drillodbc
version: 1.1.0
volume: /
location:
install-time: 1442934827

launchd.conf に環境変数をセットする。

$ sudo vim /etc/launchd.conf
setenv SIMBAINI /Users/you/.mapr.drillodbc.ini
setenv ODBCSYSINI /Users/you/.odbcinst.ini
setenv ODBCINI /Users/you/.odbc.ini
launchctl setenv DYLD_LIBRARY_PATH /usr/local/iODBC/lib:/opt/mapr/drillodbc/lib/universal

.odbc.iniを編集する。今回は ZKQuorum, ZKClusterID, UID, PWDは設定しないのでコメントアウトしておく。

$ vim ~/.odbc.ini
[ODBC]
# Specify any global ODBC configuration here such as ODBC tracing.

[ODBC Data Sources]
Sample MapR Drill DSN=MapR Drill ODBC Driver

[Sample MapR Drill DSN]
# Description: DSN Description.
# This key is not necessary and only describes the data source.
Description=Sample MapR Drill ODBC Driver DSN
# Driver: The location where the ODBC driver is installed.
Driver=/opt/mapr/drillodbc/lib/universal/libmaprdrillodbc.dylib

# The DriverUnicodeEncoding setting is only used for SimbaDM
# When set to 1, SimbaDM runs in UTF-16 mode.
# When set to 2, SimbaDM runs in UTF-8 mode.
#DriverUnicodeEncoding=2

# Values for ConnectionType, AdvancedProperties, Catalog, Schema should be set here.

# If ConnectionType is Direct, include Host and Port. If ConnectionType is ZooKeeper, include ZKQuorum and ZKClusterID
# They can also be specified on the connection string.
# AuthenticationType: No authentication; Basic Authentication
ConnectionType=Direct
HOST=localhost
PORT=31010
# ZKQuorum=[Zookeeper Quorum]
# ZKClusterID=[Cluster ID]
AuthenticationType=No Authentication
# UID=[USERNAME]
# PWD=[PASSWORD]
AdvancedProperties=CastAnyToVarchar=true;HandshakeTimeout=5;QueryTimeout=180;TimestampTZDisplayTimezone=utc;ExcludedSchemas=sys,INFORMATION_SCHEMA;NumberOfPrefetchBuffers=5;
Catalog=DRILL
Schema=

.mapr.drillodbc.iniを編集する。

$ vim ~/.mapr.drillodbc.ini
[Driver]
## - Note that this default DriverManagerEncoding of UTF-32 is for iODBC.
DriverManagerEncoding=UTF-32
ErrorMessagesPath=/opt/mapr/drillodbc/ErrorMessages

LogLevel=0
LogPath=
SwapFilePath=/tmp

# iODBC
ODBCInstLib=libiodbcinst.dylib

Apache Drillに ODBC で接続

drill-embeddedを起動する。

$ cd /usr/local/Cellar/apache-drill/1.0.0
$ bin/drill-embedded

iODBC Administrator.appからODBCの確認を行う。
User DSN > [Sample MapR Drill DSN] を選択して testを押下する。

$ open -a /Applications/iODBC/iODBC\ Administrator.app

Pythonから pyodbcを使ってDrillに接続してみる。

import pyodbc
from pandas import *

MY_DSN='DSN=Sample MapR Drill DSN;ConnectionType=Direct;Host=localhost;Port=31010'
conn = pyodbc.connect(MY_DSN, autocommit=True)
cursor = conn.cursor()

# setup the query and run it
query = "SELECT * " \
    "FROM dfs.`/path/to/your/sample.json` " \

# fetch and display filtered output
cursor.execute(query)
res = cursor.fetchall()

Python以外にも R, Perlからも接続できる。[3]


[1] Interfaces Introduction
[2] Testing the ODBC Connection
[3] Using Drill Programmatically from Python, R and Perl