Last active
June 24, 2024 07:29
-
-
Save korakot/15fe4f18d0e0f53d7b834ef797880500 to your computer and use it in GitHub Desktop.
Test elasticsearch server & client on Colab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install es server | |
!apt install default-jdk > /dev/null | |
!wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz -q --show-progress | |
!tar -xzf elasticsearch-6.5.4.tar.gz | |
!chown -R daemon:daemon elasticsearch-6.5.4 | |
# start server | |
import os | |
from subprocess import Popen, PIPE, STDOUT | |
es_server = Popen(['elasticsearch-6.5.4/bin/elasticsearch'], | |
stdout=PIPE, stderr=STDOUT, | |
preexec_fn=lambda: os.setuid(1) # as daemon | |
) | |
# client-side | |
!pip install elasticsearch -q | |
from elasticsearch import Elasticsearch | |
es = Elasticsearch() | |
es.ping() # got True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-linux-x86_64.tar.gz -q | |
!tar -xzf elasticsearch-7.0.0-linux-x86_64.tar.gz | |
!chown -R daemon:daemon elasticsearch-7.0.0 | |
# start server | |
import os | |
from subprocess import Popen, PIPE, STDOUT | |
es_server = Popen(['elasticsearch-7.0.0/bin/elasticsearch'], | |
stdout=PIPE, stderr=STDOUT, | |
preexec_fn=lambda: os.setuid(1) # as daemon | |
) | |
# wait a bit then test | |
!curl -X GET "localhost:9200/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from requests import models, Session, Request | |
from urllib.parse import urljoin | |
from IPython.core.magic import register_cell_magic | |
# to display json response | |
models.Response._repr_html_ = lambda rsp: """ | |
<script src="https://rawgit.com/caldwell/renderjson/master/renderjson.js"></script> | |
<script> | |
renderjson.set_show_to_level(1) | |
document.body.appendChild(renderjson(%s)) | |
</script> | |
""" % rsp.text | |
# %%es magic | |
@register_cell_magic | |
def es(line=None, cell=None): | |
line1 = (cell + '\n').find('\n') | |
method, path = cell[:line1].split(None, 1) | |
body = cell[line1:].strip() | |
args = {} | |
if body: | |
args['data'] = body + '\n' # in case _bulk | |
args['headers'] = {'Content-Type': 'application/json'} | |
rsp = Session().send( | |
Request(method, urljoin('http://localhost:9200', path), **args) | |
.prepare()) | |
return rsp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add more features than magic.py | |
from requests import models, Session, Request | |
from urllib.parse import urljoin | |
from IPython.core.magic import register_cell_magic | |
import re | |
# to display json response | |
def render(r): | |
text = r.text | |
if text[0] in "[{": # really json | |
return """ | |
<script src="https://rawgit.com/caldwell/renderjson/master/renderjson.js"></script> | |
<script> | |
renderjson.set_show_to_level(1) | |
document.body.appendChild(renderjson(%s)) | |
new ResizeObserver(google.colab.output.resizeIframeToContent).observe(document.body) | |
</script> | |
""" % text | |
else: # other status text | |
return "<pre>%s</pre>" % text | |
models.Response._repr_html_ = render | |
# %%es magic | |
@register_cell_magic | |
def es(line=None, cell=""): | |
cell = re.sub(r'(?m)^\s*#.*\n?','', cell) # remove comment | |
line1 = (cell + '\n').find('\n') | |
method, path = cell[:line1].split(None, 1) | |
body = cell[line1:].strip() | |
args = {} | |
if body: | |
args['data'] = (body + '\n').encode() # in case _bulk | |
args['headers'] = {'Content-Type': 'application/json'} | |
rsp = Session().send( | |
Request(method, urljoin('http://localhost:9200', path), **args) | |
.prepare()) | |
return rsp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for these code snippets but I am getting permission denied error on collab, can we do something about it? Thank you.