1- from datapackage_pipelines_elasticsearch . processors . dump . to_index import ESDumper
1+ from dataflows_elasticsearch import dump_to_es
22from tableschema_elasticsearch .mappers import MappingGenerator
33import dataflows as DF
44
55import logging
6- import collections
7- import datetime
86
97
108class BoostingMappingGenerator (MappingGenerator ):
@@ -34,85 +32,20 @@ def _convert_type(cls, schema_type, field, prefix):
3432 return prop
3533
3634
37- class DumpToElasticSearch (ESDumper ):
38-
39- def __init__ (self , indexes , ** parameters ):
40- parameters ['indexes' ] = indexes
41- self .mapper_cls = BoostingMappingGenerator
42- self .index_settings = {'index.mapping.coerce' : True }
43- self .__params = parameters
44- self .stats = {}
45- counters = self .__params .get ('counters' , {})
46- self .datapackage_rowcount = counters .get ('datapackage-rowcount' , 'count_of_rows' )
47- self .datapackage_bytes = counters .get ('datapackage-bytes' , 'bytes' )
48- self .datapackage_hash = counters .get ('datapackage-hash' , 'hash' )
49- self .resource_rowcount = counters .get ('resource-rowcount' , 'count_of_rows' )
50- self .resource_bytes = counters .get ('resource-bytes' , 'bytes' )
51- self .resource_hash = counters .get ('resource-hash' , 'hash' )
52- self .add_filehash_to_path = self .__params .get ('add-filehash-to-path' , False )
53-
54- def __call__ (self ):
55-
56- def step (package ):
57- self .initialize (self .__params )
58- self .__datapackage = self .prepare_datapackage (package .pkg .descriptor , self .__params )
59- yield package .pkg
60- for resource in package :
61- resource .spec = resource .res .descriptor
62- ret = self .handle_resource (self .schema_validator (resource ),
63- resource .res .descriptor , self .__params , package .pkg .descriptor )
64- ret = self .row_counter (package .pkg .descriptor , resource .res .descriptor , ret )
65- yield ret
66- self .finalize ()
67-
68- return step
69-
70-
71- def format_datetime_rows (self , spec , rows ):
72- yield from rows
73- # formatters = {}
74- # for f in spec['schema']['fields']:
75- # if f['type'] == 'datetime':
76- # logging.info('FIELD datetime: %r', f)
77- # if f.get('format', 'default') in ('any', 'default'):
78- # formatters[f['name']] = lambda x: None if x is None else x.strftime('%Y-%m-%dT%H:%M:%SZ')
79- # else:
80- # def formatter(f):
81- # fmt = f['format']
82- # def func(x):
83- # if x is None:
84- # return None
85- # else:
86- # return x.strftime(fmt)
87- # return func
88- # formatters[f['name']] = formatter(f)
89- # id = lambda x: x
90- #
91- # for row in rows:
92- # yield dict((k, formatters.get(k, id)(v)) for k, v in row.items())
93-
94- def initialize (self , parameters ):
95- parameters ['reindex' ] = False
96- return super (DumpToElasticSearch , self ).initialize (parameters )
97-
98- def handle_resource (self , resource , spec , parameters , datapackage ):
99- return super (DumpToElasticSearch , self )\
100- .handle_resource (resource ,
101- spec , parameters , datapackage )
35+ class my_dump_to_es (dump_to_es ):
10236
10337 def finalize (self ):
10438 for index_name , configs in self .index_to_resource .items ():
10539 for config in configs :
10640 if 'revision' in config :
10741 revision = config ['revision' ]
108- doc_type = config ['doc-type' ]
10942 resource_name = config ['resource-name' ]
110- if doc_type == 'document' :
43+ if index_name . endswith ( '__docs' ) :
11144 continue
11245 if resource_name != 'publications' :
11346 continue
114- logging .info ('DELETING from "%s", "%s" items with revision < %d' ,
115- index_name , doc_type , revision )
47+ logging .info ('DELETING from "%s" items with revision < %d' ,
48+ index_name , revision )
11649 queries = [
11750 {
11851 "bool" : {
@@ -136,8 +69,7 @@ def finalize(self):
13669 index_name ,
13770 {
13871 "query" : q
139- },
140- doc_type = doc_type
72+ }
14173 )
14274 logging .info ('GOT (%d) %r' , i , ret )
14375
@@ -185,16 +117,32 @@ def es_dumper(resource_name, revision, path):
185117 return DF .Flow (
186118 update_pk ('doc_id' ),
187119 DF .add_field ('revision' , 'integer' , default = revision ),
188- DumpToElasticSearch ({'migdar__' + resource_name :
189- [{'resource-name' : resource_name ,
190- 'doc-type' : None ,
191- 'revision' : revision }]})(),
120+ my_dump_to_es (
121+ indexes = {
122+ 'migdar__' + resource_name : [
123+ {
124+ 'resource-name' : resource_name ,
125+ 'revision' : revision
126+ }
127+ ]
128+ },
129+ mapper_cls = BoostingMappingGenerator ,
130+ index_settings = {'index.mapping.coerce' : True }
131+ ),
192132 DF .dump_to_path ('data/{}' .format (path )),
193133 collate (revision ),
194- DumpToElasticSearch ({'migdar__docs' :
195- [{'resource-name' : resource_name ,
196- 'doc-type' : None ,
197- 'revision' : revision }]})(),
134+ my_dump_to_es (
135+ indexes = {
136+ 'migdar__docs' : [
137+ {
138+ 'resource-name' : resource_name ,
139+ 'revision' : revision
140+ }
141+ ]
142+ },
143+ mapper_cls = BoostingMappingGenerator ,
144+ index_settings = {'index.mapping.coerce' : True }
145+ ),
198146 DF .update_resource (None , ** {'dpp:streaming' : True }),
199147 DF .printer (),
200148 )
0 commit comments