Let’s talk about connecting to your database in Lua. For those who aren’t aware, Lua is a lightweight, powerful and efficient scripting language designed primarily for embedded systems and clients, used in many industrial applications, games (like World of Warcraft, Angry Birds) etc.,
In Lua, to be able to connect to your database, you can use an ODBC driver, but you need to install an ODBC Lua module to interact with ODBC drivers. This tutorial will walk you through how you can connect to your Hive instance using Progress DataDirect Hive ODBC driver.
Before you start, this tutorial assumes that you are using an Ubuntu, have a Hive instance to connect to and basic programming knowledge.
Open your Terminal and run the below commands to install required tools for building the code and using ODBC drivers.
sudo apt-get update
sudo apt-get install make
sudo apt-get install gcc g++
sudo apt-get install libreadline-dev
sudo apt-get install unixodbc unixodbc-dev
tar zxf lua-5.3.4.tar.gz
cd lua-5.3.4
make linux test
cd src
sudo mv lua luac /usr/local/bin/
sudo mkdir -p /usr/local/include/lua/5.3
sudo mv * /usr/local/include/lua/5.3/
tar zxpf luarocks-2.4.3.tar.gz
cd luarocks-2.4.3
./configure; sudo make bootstrap
sudo luarocks install luasocket
sudo luarocks install odbc
tar -zxf PROGRESS_DATADIRECT_ODBC_HIVE_LINUX_64.tgz
./PROGRESS_DATADIRECT_ODBC_8.0_LINUX_64_INSTALL.bin
sudo chmod +x odbc.sh
./odbc.sh
echo $ODBCINI
export LD_LIBRARY_PATH=/install_path/Progress/ DataDirect/ODBC_80_64bit/lib
export ODBCINI=/install_path/Progress/ DataDirect/ODBC_80_64bit/odbc.ini
export ODBCINST=/ install_path/Progress/ DataDirect/ODBC_80_64bit/odbcinst.ini
source ~/.bashrc
odbc = require "odbc"
dbassert = odbc.assert
cnn = odbc.connect('Hive1', '<
User
>', '<
Password
>')
stmt = cnn:execute('SELECT activityid, activitydate, url FROM pmkdatalake.activity_pageview limit 10')
stmt:foreach(function(f1, f2, f3)
print(string.format("activityid: %d, url: %s, activitydate: %s", f1, f3, f2));
end)
assert(stmt:closed()) -- foreach close cursor
assert(not stmt:destroyed()) -- statement valid
lua <
filename
>.lua