You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@drill.apache.org by Payam Sabzmeydani <Pa...@airg.com> on 2016/05/03 23:47:47 UTC
Setting session options for REST API queries
Hi all,
I was wondering if there is a way to change session options (ALTER SESSION) when using the REST API to query Drill?
Thanks,
Payam
Re: Setting session options for REST API queries
Posted by Payam Sabzmeydani <Pa...@airg.com>.
That is exactly what I need. I had assumed that the session is limited to
authentication layer and it is not respected by the queries, I'm glad I
was wrong.
I also tried your solution without the authentication but no persistent
session is created without going through auth.
Thanks John,
Payam
On 2016-05-04, 3:57 AM, "John Omernik" <jo...@omernik.com> wrote:
>The rest api works great with session variables, and it works in the
>current version of Drill. The key is you need to have a session created
>within your web requests. Here is an example using Python and the
>requests
>module. Fairly straightforward. I am not sure how requests works without
>auth enabled, however, it may create a session cookie even without Auth,
>so
>if you are not using authentication, just try commenting out the s =
>authDrill(s) line and see if it works.
>
>Let me know if you have any questions.
>
>John
>
>
>#!/usr/bin/python
>
>import requests
>import json
>import sys
>import os
>
>drill_base_url = "https://drillbit.local:8047"
>
>drill_user = "username"
>drill_pass = "password"
>
>print "Drill Rest Endpoint: %s" % (drill_base_url)
>
>def main():
> s = requests.Session() # Create a session object
> s = authDrill(s) # Authenticate to Drill
>
> r = runQuery(s, "ALTER SESSION set `store.json.all_text_mode` = true")
> result = runQuery(s, "CREATE TABLE your_new_table as select
>your_fields
>from your_src_table where something = somethingelse")
>
> print r.text
>
> if result.status_code == 200:
> print "Load Successful"
> else:
> print "Error encountered: %s" % result.status_code
>
>def runQuery(s, drill):
> url = drill_base_url + "/query.json"
> payload = {"queryType":"SQL", "query":drill}
> headers = {"Content-type": "application/json"}
> r = s.post(url, data=json.dumps(payload), headers=headers,
>verify=False)
>
> return r
>def authDrill(s):
>
> url = drill_base_url + "/j_security_check"
>
> login = {'j_username': drill_user, 'j_password': drill_pass}
>
> r = s.post(url, data=login, verify=False)
>
> if r.status_code == 200:
> if r.text.find("Invalid username/password credentials") >= 0:
> print "Authentication Failed - Please check Secrets - Exiting"
> sys.exit(1)
> elif r.text.find("Number of Drill Bits") >= 0:
> print "Authentication successful"
> else:
> print "Unknown Response Code 200 - Exiting"
> print r.text
> sys.exit(1)
> else:
> print "Non HTTP-200 returned - Unknown Error - Exiting"
> print "HTTP Code: %s" % r.status_code
> print r.text
> sys.exit(1)
>
> return s
>
>if __name__ == '__main__':
> main()
>
>On Tue, May 3, 2016 at 11:51 PM, Tomer Shiran <ts...@dremio.com> wrote:
>
>> Actually, there's no reason Drill can't support session variables
>>through a
>> URL parameter or cookie. I am not sure if it's currently supported
>>through.
>>
>> On Tue, May 3, 2016 at 9:11 PM, Abhishek Girish
>><abhishek.girish@gmail.com
>> >
>> wrote:
>>
>> > From my limited knowledge, I believe this is not supported by design.
>> Refer
>> > to
>> https://en.wikipedia.org/wiki/Representational_state_transfer#Stateless
>> >
>> > On Tue, May 3, 2016 at 2:47 PM, Payam Sabzmeydani <Pa...@airg.com>
>> wrote:
>> >
>> > > Hi all,
>> > >
>> > > I was wondering if there is a way to change session options (ALTER
>> > > SESSION) when using the REST API to query Drill?
>> > >
>> > > Thanks,
>> > > Payam
>> > >
>> >
>>
Re: Setting session options for REST API queries
Posted by John Omernik <jo...@omernik.com>.
The rest api works great with session variables, and it works in the
current version of Drill. The key is you need to have a session created
within your web requests. Here is an example using Python and the requests
module. Fairly straightforward. I am not sure how requests works without
auth enabled, however, it may create a session cookie even without Auth, so
if you are not using authentication, just try commenting out the s =
authDrill(s) line and see if it works.
Let me know if you have any questions.
John
#!/usr/bin/python
import requests
import json
import sys
import os
drill_base_url = "https://drillbit.local:8047"
drill_user = "username"
drill_pass = "password"
print "Drill Rest Endpoint: %s" % (drill_base_url)
def main():
s = requests.Session() # Create a session object
s = authDrill(s) # Authenticate to Drill
r = runQuery(s, "ALTER SESSION set `store.json.all_text_mode` = true")
result = runQuery(s, "CREATE TABLE your_new_table as select your_fields
from your_src_table where something = somethingelse")
print r.text
if result.status_code == 200:
print "Load Successful"
else:
print "Error encountered: %s" % result.status_code
def runQuery(s, drill):
url = drill_base_url + "/query.json"
payload = {"queryType":"SQL", "query":drill}
headers = {"Content-type": "application/json"}
r = s.post(url, data=json.dumps(payload), headers=headers, verify=False)
return r
def authDrill(s):
url = drill_base_url + "/j_security_check"
login = {'j_username': drill_user, 'j_password': drill_pass}
r = s.post(url, data=login, verify=False)
if r.status_code == 200:
if r.text.find("Invalid username/password credentials") >= 0:
print "Authentication Failed - Please check Secrets - Exiting"
sys.exit(1)
elif r.text.find("Number of Drill Bits") >= 0:
print "Authentication successful"
else:
print "Unknown Response Code 200 - Exiting"
print r.text
sys.exit(1)
else:
print "Non HTTP-200 returned - Unknown Error - Exiting"
print "HTTP Code: %s" % r.status_code
print r.text
sys.exit(1)
return s
if __name__ == '__main__':
main()
On Tue, May 3, 2016 at 11:51 PM, Tomer Shiran <ts...@dremio.com> wrote:
> Actually, there's no reason Drill can't support session variables through a
> URL parameter or cookie. I am not sure if it's currently supported through.
>
> On Tue, May 3, 2016 at 9:11 PM, Abhishek Girish <abhishek.girish@gmail.com
> >
> wrote:
>
> > From my limited knowledge, I believe this is not supported by design.
> Refer
> > to
> https://en.wikipedia.org/wiki/Representational_state_transfer#Stateless
> >
> > On Tue, May 3, 2016 at 2:47 PM, Payam Sabzmeydani <Pa...@airg.com>
> wrote:
> >
> > > Hi all,
> > >
> > > I was wondering if there is a way to change session options (ALTER
> > > SESSION) when using the REST API to query Drill?
> > >
> > > Thanks,
> > > Payam
> > >
> >
>
Re: Setting session options for REST API queries
Posted by Tomer Shiran <ts...@dremio.com>.
Actually, there's no reason Drill can't support session variables through a
URL parameter or cookie. I am not sure if it's currently supported through.
On Tue, May 3, 2016 at 9:11 PM, Abhishek Girish <ab...@gmail.com>
wrote:
> From my limited knowledge, I believe this is not supported by design. Refer
> to https://en.wikipedia.org/wiki/Representational_state_transfer#Stateless
>
> On Tue, May 3, 2016 at 2:47 PM, Payam Sabzmeydani <Pa...@airg.com> wrote:
>
> > Hi all,
> >
> > I was wondering if there is a way to change session options (ALTER
> > SESSION) when using the REST API to query Drill?
> >
> > Thanks,
> > Payam
> >
>
Re: Setting session options for REST API queries
Posted by Abhishek Girish <ab...@gmail.com>.
From my limited knowledge, I believe this is not supported by design. Refer
to https://en.wikipedia.org/wiki/Representational_state_transfer#Stateless
On Tue, May 3, 2016 at 2:47 PM, Payam Sabzmeydani <Pa...@airg.com> wrote:
> Hi all,
>
> I was wondering if there is a way to change session options (ALTER
> SESSION) when using the REST API to query Drill?
>
> Thanks,
> Payam
>