Skip to main content

Generate ES Indexes, load and extract data, based on JSON Table Schema descriptors.

Project description

tableschema-elasticsearch-py

Travis Coveralls PyPi Github Gitter

Generate and load ElasticSearch indexes based on Table Schema descriptors.

Features

  • implements tableschema.Storage interface

Contents

Getting Started

Installation

The package use semantic versioning. It means that major versions could include breaking changes. It's highly recommended to specify package version range in your setup/requirements file e.g. package>=1.0,<2.0.

pip install tableschema-elasticsearch

Documentation

Usage overview

import elasticsearch
import jsontableschema_es

INDEX_NAME = 'testing_index'

# Connect to Elasticsearch instance running on localhost
es=elasticsearch.Elasticsearch()
storage=jsontableschema_es.Storage(es)

# List all indexes
print(list(storage.buckets))

# Create a new index
storage.create('test', {
         'fields': [
             {
                 'name': 'num',
                 'type': 'number'
             }
         ]
     }
)

# Write data to index
l=list(storage.write(INDEX_NAME, ({'num':i} for i in range(1000)), ['num']))
print(len(l))
print(l[:10], '...')

l=list(storage.write(INDEX_NAME, ({'num':i} for i in range(500,1500)), ['num']))
print(len(l))
print(l[:10], '...')

# Read all data from index
storage=jsontableschema_es.Storage(es)
print(list(storage.buckets))
l=list(storage.read(INDEX_NAME))
print(len(l))
print(l[:10])

In this driver elasticsearch is used as the db wrapper. We can get storage this way:

from elasticsearch import Elasticsearch
from jsontableschema_elasticsearch import Storage

engine = Elasticsearch()
storage = Storage(engine)

Then we could interact with storage ('buckets' are ElasticSearch indexes in this context):

storage.buckets # iterator over bucket names
storage.create('bucket', descriptor,
               reindex=False,
               always_recreate=False,
               mapping_generator_cls=None)
        # reindex will copy existing documents from an existing index with the same name (in case of a mapping conflict)
        # always_recreate will always recreate an index, even if it already exists. default is to update mappings only.
        # mapping_generator_cls allows customization of the generated mapping
storage.delete('bucket')
storage.describe('bucket') # return descriptor, not implemented yet
storage.iter('bucket') # yield rows
storage.read('bucket') # return rows
storage.write('bucket', rows, primary_key,
              as_generator=False)
        # primary_key is a list of field names which will be used to generate document ids

When creating indexes, we always create an index with a semi-random name and a matching alias that points to it. This allows us to decide whether to re-index documents whenever we're re-creating an index, or to discard the existing records.

Mappings

When creating indexes, the tableschema types are converted to ES types and a mapping is generated for the index.

Some special properties in the schema provide extra information for generating the mapping:

  • array types need also to have the es:itemType property which specifies the inner data type of array items.
  • object types need also to have the es:schema property which provides a tableschema for the inner document contained in that object (or have es:enabled=false to disable indexing of that field).

Example:

{
  "fields": [
    {
      "name": "my-number",
      "type": "number"
    },
    {
      "name": "my-array-of-dates",
      "type": "array",
      "es:itemType": "date"
    },
    {
      "name": "my-person-object",
      "type": "object",
      "es:schema": {
        "fields": [
          {"name": "name", "type": "string"},
          {"name": "surname", "type": "string"},
          {"name": "age", "type": "integer"},
          {"name": "date-of-birth", "type": "date", "format": "%Y-%m-%d"}
        ]
      }
    },
    {
      "name": "my-library",
      "type": "array",
      "es:itemType": "object",
      "es:schema": {
        "fields": [
          {"name": "title", "type": "string"},
          {"name": "isbn", "type": "string"},
          {"name": "num-of-pages", "type": "integer"}
        ]
      }
    },
    {
      "name": "my-user-provded-object",
      "type": "object",
      "es:enabled": false
    }
  ]
}

Custom mappings

By providing a custom mapping generator class (via mapping_generator_cls), inheriting from the MappingGenerator class you should be able

API Reference

Storage

Storage(self, es=None)

Elasticsearch Tabular Storage.

Package implements Tabular Storage interface (see full documentation on the link):

Storage

Only additional API is documented

Arguments

  • es (object): ElasticSearch instance

storage.create

storage.create(self, bucket, descriptor, reindex=False, always_recreate=False, mapping_generator_cls=None, index_settings=None)

Create index with mapping by schema.

Arguments

  • bucket(str): Name of index to be created
  • descriptor: dDscriptor of index to be created
  • always_recreate: Delete index if already exists (otherwise just update mapping)
  • reindex: On mapping mismath, automatically create new index and migrate existing indexes to it
  • mapping_generator_cls: subclass of MappingGenerator
  • index_settings: settings which will be used in index creation

storage.delete

storage.delete(self, bucket=None)

Delete index with mapping by schema.

Arguments

  • bucket(str): Name of index to delete

Contributing

The project follows the Open Knowledge International coding standards.

Recommended way to get started is to create and activate a project virtual environment. To install package and development dependencies into active environment:

$ make install

To run tests with linting and coverage:

$ make test

Changelog

Here described only breaking and the most important changes. The full changelog and documentation for all released versions could be found in nicely formatted commit history.

v1.0

  • Initial driver implementation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tableschema-elasticsearch-2.1.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

tableschema_elasticsearch-2.1.0-py2.py3-none-any.whl (9.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tableschema-elasticsearch-2.1.0.tar.gz.

File metadata

  • Download URL: tableschema-elasticsearch-2.1.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for tableschema-elasticsearch-2.1.0.tar.gz
Algorithm Hash digest
SHA256 bf3bfe1d6815b25eb3c2d3f19b59b0206b1eb24c1a53f9efadf7730fde8e199f
MD5 7d42931a2f56ffb0f3794102b13ea966
BLAKE2b-256 c014035714701afe4d0298134957227da91d28e9c2b3af91f31d47c0afc32ef2

See more details on using hashes here.

File details

Details for the file tableschema_elasticsearch-2.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: tableschema_elasticsearch-2.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for tableschema_elasticsearch-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 be68d5feb51651ca2c223a2edd918ee7d8cba32cf1a8bad6bba95e5cda10bc08
MD5 b4a349d4d4f248fc648b29c737a8b6bc
BLAKE2b-256 48573353bd841c73a75f1bf63541f1057682dd1263afad1c312762fa076ab80f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page