concept schema in category data

appears as: schemas, A schema, schema, schema, schemas, The schema
The Art of Data Usability

This is an excerpt from Manning's book The Art of Data Usability.

In chapter 15 we’re going to discuss validation using schemas. A schema is nothing more than a description of a data resource. Validating against a schema tells us whether the data resource adheres to the description or not.

What has that got to do with reproducibility you may ask yourself. Even if reproducibility is different from validity, you can use you schemas for reproducibility as well; Not to tell you if the description fits, but to help you pick the appropriate processes.

Instead of using schema to determine whether you got the right data, you use them to record the data you got. The idea is to create and keep old schemas around and use them to pick the right ETL process. Let’s build something to see his idea in action.

Listing 11.8. reproducibility.py
from original_etl import transform as original_transform #1 from later_etl import transform as later_transform from later_etl import extract, load from goodtables import validate import itertools import click schema_map = [ ('schemas/later_memes.json', later_transform), #2 ('schemas/first_memes.json', original_transform) ] def get_transform_function(csvfile_obj): #3 for (schema, transform_function) in schema_map: if validate(csvfile_obj, schema=schema): return transform_function raise ValueError('Unable to find correct transform function') def get_memes(file_obj): #4 transform_function = get_transform_function(file_obj) extracted_data = extract(file_obj) return transform_function(extracted_data) @click.command() #5 @click.argument('csvfile', nargs=-1, type=click.File('r')) def reproducibility(csvfile): load(itertools.chain.from_iterable( [get_memes(file_obj) for file_obj in csvfile])) if __name__ == '__main__': reproducibility() #1 Load different transform functions #2 Schema to transform function map #3 Get appropriate transform function #4 Transform data #5 Load the memes
Hadoop in Action

This is an excerpt from Manning's book Hadoop in Action.

A fundamental tenet of relational databases is that data resides in tables having relational structure defined by a schema. Although the relational model has great formal properties, many modern applications deal with data types that don’t fit well into this model. Text documents, images, and XML files are popular examples. Also, large data sets are often unstructured or semistructured. Hadoop uses key/value pairs as its basic data unit, which is flexible enough to work with the less-structured data types. In Hadoop, data can originate in any form, but it eventually transforms into (key/value) pairs for the processing functions to work on.

We’ve created a new relation, g, from grouping tuples in c having the same value on the third column ($2, also named a3). The output of GROUP always has two fields. The first field is group key, which is a3 in this case. The second field is a bag containing all the tuples with the same group key. Looking at g’s dump, we see that it has three tuples, corresponding to the three unique values in c’s third column. The bag in the first tuple represents all tuples in c with the third column equal to 2. The bag in the second tuple represents all tuples in c with the third column equal to 4. And so forth. After you understand how g’s data came about, you’ll feel more comfortable looking at its schema. The first field of GROUP’s output relation is always named group, for the group key. In this case it may seem more natural to call the first field a3, but currently Pig doesn’t allow you to assign a name to replace group. You’ll have to adapt yourself to refer to it as group. The second field of GROUP’s output relation is always named after the relation it’s operating on, which is c in this case, and as we said earlier it’s always a bag. As we use this bag to hold tuples from c, the schema for this bag is exactly the schema for c—three fields of integers named a1, a2, and a3.

A field in a tuple or a value in a map can be null or any atomic or complex type. This enables nesting and complex data structures. Whereas data structures can be arbitrarily complex, some are definitely more useful and occur more often than others, and nesting usually doesn’t go deeper than two levels. In the Excite log example earlier, the GROUP BY operator generated a relation grpd where each tuple has a field that is a bag. The schema for the relation seems more natural once you think of grpd as the query history of each user. Each tuple represents one user and has a field that is a bag of the user’s queries.

Besides declaring types for fields, schemas can also assign names to fields to make them easier to reference. Users can define schemas for relations using the AS keyword with the LOAD, STREAM, and FOREACH operators. For example, in the LOAD statement for getting the Excite query log, we defined the data types for the fields in log, as well as named the fields user, time, and query.

grunt> log  = LOAD 'tutorial/data/excite-small.log'
AS (user:chararray, time:long, query:chararray);

In defining a schema, if you leave out the type, Pig will default to bytearray as the most generic type. You can also leave out the name, in which case a field would be unnamed and you can only reference it by position.