{
“cells”: [
{

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“# IO, CREATE/INSERT, and External Data”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Setup”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {

“scrolled”: true

}, “outputs”: [], “source”: [

“import ibisn”, “import osn”, “hdfs_port = int(os.environ.get(‘IBIS_TEST_WEBHDFS_PORT’, 50070))n”, “user = os.environ.get(‘IBIS_TEST_WEBHDFS_USER’, ‘ubuntu’)n”, “hdfs = ibis.impala.hdfs_connect(host=’impala’, user=user, port=hdfs_port)n”, “con = ibis.impala.connect(host=’impala’, database=’ibis_testing’,n”, ” hdfs_client=hdfs)n”, “ibis.options.interactive = True”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Creating new Impala tables from Ibis expressionsn”, “n”, “n”, “Suppose you have an Ibis expression that produces a table:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“table = con.table(‘functional_alltypes’)n”, “n”, “t2 = table[table, (table.bigint_col - table.int_col).name(‘foo’)]n”, “n”, “expr = (t2n”, ” [t2.bigint_col > 30]n”, ” .group_by(‘string_col’)n”, ” .aggregate(min_foo=lambda t: t.foo.min(),n”, ” max_foo=lambda t: t.foo.max(),n”, ” sum_foo=lambda t: t.foo.sum()))n”, “expr”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“To create a table in the database from the results of this expression, use the connection’s create_table method:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.create_table(‘testing_table’, expr, database=’ibis_testing’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“By default, this creates a table stored as Parquet format in HDFS. Support for views, external tables, configurable file formats, and so forth, will come in the future. Feedback on what kind of interface would be useful for that would help.”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.table(‘testing_table’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“Tables can be similarly dropped with drop_table

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.drop_table(‘testing_table’, database=’ibis_testing’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Inserting data into existing Impala tablesn”, “n”, “n”, “The client’s insert method can append new data to an existing table or overwrite the data that is in there.”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.create_table(‘testing_table’, expr)n”, “con.table(‘testing_table’)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.insert(‘testing_table’, expr)n”, “con.table(‘testing_table’)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.drop_table(‘testing_table’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Uploading / downloading data from HDFS”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“If you’ve set up an HDFS connection, you can use the Ibis HDFS interface to look through your data and read and write files to and from HDFS:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“hdfs = con.hdfsn”, “hdfs.ls(‘/__ibis/ibis-testing-data’)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“hdfs.ls(‘/__ibis/ibis-testing-data/parquet’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“Suppose we wanted to download /__ibis/ibis-testing-data/parquet/functional_alltypes, which is a directory. We need only do:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“!rm -rf parquet_dir/n”, “hdfs.get(‘/__ibis/ibis-testing-data/parquet/functional_alltypes’, ‘parquet_dir’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“Now we have that directory locally:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“!ls parquet_dir/”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“Files and directories can be written to HDFS just as easily using put:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“path = ‘/__ibis/dir-write-example’n”, “if hdfs.exists(path):n”, ” hdfs.rmdir(path)n”, “hdfs.put(path, ‘parquet_dir’, verbose=True)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“hdfs.ls(‘/__ibis/dir-write-example’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“Delete files with rm or directories with rmdir:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“hdfs.rmdir(‘/__ibis/dir-write-example’)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“!rm -rf parquet_dir/”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Queries on Parquet, Avro, and Delimited files in HDFSn”, “n”, “n”, “Ibis can easily create temporary or persistent Impala tables that reference data in the following formats:n”, “n”, “- Parquet (parquet_file)n”, “- Avro (avro_file)n”, “- Delimited text formats (CSV, TSV, etc.) (delimited_file)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“Parquet is the easiest because the schema can be read from the data files:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“path = ‘/__ibis/ibis-testing-data/parquet/tpch_lineitem’n”, “n”, “lineitem = con.parquet_file(path)n”, “lineitem.limit(2)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“lineitem.l_extendedprice.sum()”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“If you want to query a Parquet file and also create a table in Impala that remains after your session, you can pass more information to parquet_file:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“table = con.parquet_file(path, name=’my_parquet_table’, n”, ” database=’ibis_testing’,n”, ” persist=True)n”, “table.l_extendedprice.sum()”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.table(‘my_parquet_table’).l_extendedprice.sum()”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.drop_table(‘my_parquet_table’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“To query delimited files, you need to write down an Ibis schema. At some point we’d like to build some helper tools that will infer the schema for you, all in good time.n”, “n”, “There’s some CSV files in the test folder, so let’s use those:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“hdfs.get(‘/__ibis/ibis-testing-data/csv’, ‘csv-files’)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“!cat csv-files/0.csv”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“!rm -rf csv-files/”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“The schema here is pretty simple (see ibis.schema for more):”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“schema = ibis.schema([(‘foo’, ‘string’),n”, ” (‘bar’, ‘double’),n”, ” (‘baz’, ‘int32’)])n”, “n”, “table = con.delimited_file(‘/__ibis/ibis-testing-data/csv’,n”, ” schema)n”, “table.limit(10)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“table.bar.summary()”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“For functions like parquet_file and delimited_file, an HDFS directory must be passed (we’ll add support for S3 and other filesystems later) and the directory must contain files all having the same schema.”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“If you have Avro data, you can query it too if you have the full avro schema:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {

“scrolled”: true

}, “outputs”: [], “source”: [

“avro_schema = {n”, ” "fields": [n”, ” {"type": ["int", "null"], "name": "R_REGIONKEY"},n”, ” {"type": ["string", "null"], "name": "R_NAME"},n”, ” {"type": ["string", "null"], "name": "R_COMMENT"}],n”, ” "type": "record",n”, ” "name": "a"n”, “}n”, “n”, “path = ‘/__ibis/ibis-testing-data/avro/tpch.region’n”, “n”, “hdfs.mkdir(path)n”, “table = con.avro_file(path, avro_schema)n”, “table”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Other helper functions for interacting with the databasen”, “n”, “n”, “We’re adding a growing list of useful utility functions for interacting with an Impala cluster on the client object. The idea is that you should be able to do any database-admin-type work with Ibis and not have to switch over to the Impala SQL shell. Any ways we can make this more pleasant, please let us know.n”, “n”, “Here’s some of the features, which we’ll give examples for:n”, “n”, “- Listing and searching for available databases and tablesn”, “- Creating and dropping databasesn”, “- Getting table schemas”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.list_databases(like=’ibis*’)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.list_tables(database=’ibis_testing’, like=’tpch*’)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“schema = con.get_schema(‘functional_alltypes’)n”, “schema”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“Databases can be created, too, and you can set the storage path in HDFS you want for the data files”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“db = ‘ibis_testing2’n”, “con.create_database(db, path=’/__ibis/my-test-database’, force=True)n”, “n”, “# you may or may not have to give the impala user write and execute permissions to ‘/__ibis/my-test-database’n”, “hdfs.chmod(‘/__ibis/my-test-database’, ‘777’)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.create_table(‘example_table’, con.table(‘functional_alltypes’),n”, ” database=db, force=True)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“Hopefully, there will be data files in the indicated spot in HDFS:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“hdfs.ls(‘/__ibis/my-test-database’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“To drop a database, including all tables in it, you can use drop_database with force=True:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.drop_database(db, force=True)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Dealing with Partitioned tables in Impala”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

Placeholder: This is not yet implemented. If you have use cases, please let us know.”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Faster queries on small data in Impalan”, “n”, “n”, “Since Impala internally uses LLVM to compile parts of queries (aka "codegen") to make them faster on large data sets there is a certain amount of overhead with running many kinds of queries, even on small datasets. You can disable LLVM code generation when using Ibis, which may significantly speed up queries on smaller datasets:”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“from numpy.random import rand”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“con.disable_codegen()”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“t = con.table(‘ibis_testing.functional_alltypes’)n”, “n”, “%timeit (t.double_col + rand()).sum().execute()”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“# Turn codegen back onn”, “con.disable_codegen(False)”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: [

“%timeit (t.double_col + rand()).sum().execute()”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“It’s important to remember that codegen is a fixed overhead and will significantly speed up queries on big data”

]

}

], “metadata”: {

“kernelspec”: {

“display_name”: “Python 3”, “language”: “python”, “name”: “python3”

}, “language_info”: {

“codemirror_mode”: {

“name”: “ipython”, “version”: 3

}, “file_extension”: “.py”, “mimetype”: “text/x-python”, “name”: “python”, “nbconvert_exporter”: “python”, “pygments_lexer”: “ipython3”, “version”: “3.6.3”

}

}, “nbformat”: 4, “nbformat_minor”: 1

}