<img src="images/continuum_analytics_logo.png" alt="Continuum Logo", align="right", width="30%">,
into
Into migrates data between formats and locations.
Before we can use a database we need to move data into it. The into
project provides a single consistent interface to move data between formats and between locations.
We'll start with local data and eventually move out to remote data.
Into moves data into a target from a source
>>> into(target, source)
The target and source can be either a Python object or a string URI. The following are all valid calls to into
>>> into(pd.DataFrame, 'iris.csv') # Load CSV file into new DataFrame
>>> into('iris.json', my_df) # Write DataFrame into JSON file
>>> into('iris.json', 'iris.csv') # Migrate data from CSV to JSON
In [ ]:
from into import into
import numpy as np
import pandas as pd
Into refers to foreign data either with a Python object like a sqlalchemy.Table
object for a SQL table, or with a string URI, like postgresql://hostname::tablename
.
URI's often take on the following form
protocol://path-to-resource::path-within-resource
Where path-to-resource
might point to a file, a database hostname, etc. while path-within-resource
might refer to a datapath or table name. Note the two main separators
://
separates the protocol on the left (sqlite
, mongodb
, ssh
, hdfs
, hive
, ...)::
separates the path within the database on the right (e.g. tablename)
In [ ]:
What kind of object did you get receive as output? Call type
on your result.
In [ ]:
In [ ]:
Verify that your data arrived safely by converting your mongo collection into a list.
In [ ]:
Finally, clearn up and remove your collection from the MongoDB by calling the drop
function.
In [ ]:
from into import drop
Into is a network of fast pairwise conversions between pairs of formats. We when we migrate between two formats we traverse a path of pairwise conversions.
We visualize that network below:
Each node represents a data format. Each directed edge represents a function to transform data between two formats. A single call to into may traverse multiple edges and multiple intermediate formats. Red nodes support larger-than-memory data.
A single call to into may traverse several intermediate formats calling on several conversion functions. For example, we when migrate a CSV file to a Mongo database we might take the following route:
DataFrame
(pandas.read_csv
)np.recarray
(DataFrame.to_records
)Iterator
(np.ndarray.tolist
)pymongo.Collection.insert
)Alternatively we could write a special function that uses MongoDB's native CSV
loader and shortcut this entire process with a direct edge CSV -> Mongo
.
These functions are chosen because they are fast, often far faster than converting through a central serialization format.
This picture is actually from an older version of into
, when the graph was still small enough to visualize pleasantly. See into docs for a more updated version.
We can interact with remote data in three locations
ssh
For most of this we'll wait until we've seen Blaze, briefly we'll use S3.
For now, we quickly grab a file from Amazon's S3
.
This example depends on boto
to interact with S3.
conda install boto
In [ ]:
into(pd.DataFrame, 's3://nyqpug/tips.csv')