Skip to content

Instantly share code, notes, and snippets.

@drorata
Created January 4, 2017 15:24
Show Gist options
  • Save drorata/25f274f4365fafd8895d8012ca9ede5d to your computer and use it in GitHub Desktop.
Save drorata/25f274f4365fafd8895d8012ca9ede5d to your computer and use it in GitHub Desktop.
Minimal example with Snakebite
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initiate `snakebite` client"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from snakebite.client import Client\n",
"client = Client(\"my.host.com\", 8020, effective_user='datascientist', \n",
" use_datanode_hostname=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List the items in the directory and collect the files"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['/user/datascientist/da-foobar-2.txt', '/user/datascientist/da-foobar.txt']"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"files = []\n",
"for x in client.ls(['/user/datascientist']):\n",
" if x['file_type'] == 'f':\n",
" files.append(x['path'])\n",
"files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the files (using `join`)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Yet another file\\nmy file is this\\ntest file\\nmy file is this\\ntest file'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"''.join(client.text(files))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or using a loop"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Yet another file\n",
"my file is this\n",
"test file\n",
"\n",
"my file is this\n",
"test file\n"
]
}
],
"source": [
"for cat in client.text(files):\n",
" print(cat)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:hdfs-snakebite]",
"language": "python",
"name": "conda-env-hdfs-snakebite-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment