Plywood

repository·master·Indexed 18 days ago

https://github.com/implydata/plywood

A JavaScript middle-layer library and query planner/executor (v0.36.0) designed for building interactive data visualizations for large datasets. It bridges data stores like Apache Druid and MySQL with visualization libraries like D3.js using a nested Split-Apply-Combine architecture and a custom expression language.

Tokens
19.4K
Snippets
75
Records
88
Agent score
61%

What's inside plywood

  1. What is Plywood?

    master

    Plywood is a JavaScript library designed to simplify building interactive visualizations and applications for large datasets. It functions as a middle-layer between data visualizations (like D3.js) and data stores (like Apache Druid).

    Key features include:

    • Split-Apply-Combine Architecture: Uses a nested divide-and-conquer algorithm to construct various data visualizations.
    • Expression Language: Features a custom expression language where a single expression can translate into multiple database queries.
    • Nested Data Structures: Returns results in nested formats optimized for consumption by visualization libraries.
    • Druid Query Planning: Acts as an advanced query planner for Druid, determining the most optimal execution paths for queries.
  2. Compose expressions using method chaining

    master

    Plywood expressions are designed to be composed by method chaining. Most functions operate on an existing expression and return a new expression, allowing you to build complex logic step-by-step. For example, you can take a variable, perform arithmetic, and then apply a boolean predicate.

    var ex = $('x').add(1).is(11);
    ex.compute({ x: 10 }).then(console.log); // => true
  3. Use NUMBER and NUMBER_RANGE types

    master

    Numeric data is handled via NUMBER and NUMBER_RANGE:

    • NUMBER: Represents standard numeric attributes.
    • NUMBER_RANGE: Represents an interval of numbers, commonly used for bucketing results.
    // NUMBER
    5
    
    // NUMBER_RANGE
    NumberRange.fromJS({ start: 4, end: 7.5 })
  4. Core Datatypes in Plywood

    master
    Plywood uses several core datatypes to represent data. These datatypes function both as the return values of expressions and as native objects within the system. The available types include NULL, BOOLEAN, NUMBER, NUMBER_RANGE, TIME, TIME_RANGE, STRING, SET/X (where X is the element type), and DATASET.
  5. Use DATASET to represent tables or data sources

    master

    A DATASET is an abstract representation of a table (like in SQL) or a data source (like in Druid). It is a (potentially ordered) collection of datums, where each datum is a collection of attributes of the core types described in Plywood.

    Dataset.fromJS([
      { x: 1, y: "USA" },
      { x: 2, y: "UK" }
    ])
  6. How Plywood expressions work

    master

    Expressions are the fundamental building blocks of Plywood. The standard workflow involves constructing an expression (either via JSON or by composing operands) and then passing it to Plywood for evaluation using the .compute() method.

    To evaluate an expression, you call ex.compute(context), where context is an object containing the values for any named references used within the expression.

    var ex = $('x');
    ex.compute({ x: 10 }).then(console.log); // => 10
  7. Use TIME and TIME_RANGE types

    master

    Time-based attributes are represented by TIME and TIME_RANGE:

    • TIME: Represents a specific point in time.
    • TIME_RANGE: Represents an interval of time, often used for bucketing.
    // TIME
    new Date('2015-02-24T18:00:00Z')
    
    // TIME_RANGE
    TimeRange.fromJS({
      start: new Date('2015-02-24T18:00:00Z'), 
      end: new Date('2015-02-24T19:00:00Z') 
    })
  8. Use STRING and SET types

    master

    Categorical and collection data is handled via STRING and SET:

    • STRING: Represents a categorical attribute.
    • SET/X: Represents a set of distinct elements where all elements must share the same type X. For example, SET/NUMBER is a set of numbers.
    // STRING
    'USA'
    
    // SET
    Set.fromJS(['USA', 'UK', 'Japan'])
  9. How Plywood works: The middle-layer concept

    master

    Plywood acts as a middle-layer between data visualizations and data stores. It is architected around the Split-Apply-Combine principle, allowing you to construct complex data visualizations through nested queries.

    Key characteristics:

    • Expression Language: A single Plywood expression can translate to multiple database queries.
    • Nested Results: Results are returned in nested data structures, making them easy to consume by visualization libraries like D3.js.
    • Query Planning: When used with Druid, Plywood acts as an advanced query planner to determine the most optimal execution path.
  10. How Plywood's core architecture works

    master

    Plywood is organized into three logical components that work together to build and execute data queries:

    1. Expression Language (plywood): A domain-specific language (DSL) used to describe data queries. It is inspired by the split-apply-combine principle and the D3 API. Expressions are high-level, serializable (to/from JSON), immutable, and capable of complex internal rewriting for query simplification.
    2. Externals: These act as query planners and schedulers. Instead of executing logic in native JavaScript, externals translate Plywood expressions into queries for specific databases (e.g., Druid, MySQL).
    3. Helpers: A collection of utility functions designed to simplify the construction of the query layer.
    var ex = ply()
      .apply('Count', $('wiki').count())
      .apply('TotalAdded', '$wiki.sum($added)')
      .apply('Pages',
        $('wiki').split('$page', 'Page')
          .apply('Count', $('wiki').count())
          .sort('$Count', 'descending')
          .limit(6)
      );
  11. View Plywood visualization examples in the Gallery

    master
    The Plywood gallery provides live, streaming data visualization examples. These examples demonstrate how to use Plywood in conjunction with plywood-proxy to query streaming data and render it using various charting techniques. You can inspect the specific Plywood queries and the corresponding rendering code for each example by visiting the provided links.