예제 #1
0
def get_coder_from_spec(coder_spec):
    """Return a coder instance from a coder spec.

  Args:
    coder_spec: A dict where the value of the '@type' key is a pickled instance
      of a Coder instance.

  Returns:
    A coder instance (has encode/decode methods).
  """
    assert coder_spec is not None

    # Ignore the wrappers in these encodings.
    # TODO(silviuc): Make sure with all the renamings that names below are ok.
    if coder_spec['@type'] in ignored_wrappers:
        assert len(coder_spec['component_encodings']) == 1
        coder_spec = coder_spec['component_encodings'][0]
        return get_coder_from_spec(coder_spec)

    # Handle a few well known types of coders.
    if coder_spec['@type'] == 'kind:pair':
        assert len(coder_spec['component_encodings']) == 2
        component_coders = [
            get_coder_from_spec(c) for c in coder_spec['component_encodings']
        ]
        return coders.TupleCoder(component_coders)
    elif coder_spec['@type'] == 'kind:stream':
        assert len(coder_spec['component_encodings']) == 1
        return coders.IterableCoder(
            get_coder_from_spec(coder_spec['component_encodings'][0]))
    elif coder_spec['@type'] == 'kind:windowed_value':
        assert len(coder_spec['component_encodings']) == 2
        value_coder, window_coder = [
            get_coder_from_spec(c) for c in coder_spec['component_encodings']
        ]
        return coders.WindowedValueCoder(value_coder,
                                         window_coder=window_coder)
    elif coder_spec['@type'] == 'kind:interval_window':
        assert ('component_encodings' not in coder_spec
                or len(coder_spec['component_encodings'] == 0))
        return coders.IntervalWindowCoder()
    elif coder_spec['@type'] == 'kind:global_window':
        assert ('component_encodings' not in coder_spec
                or not coder_spec['component_encodings'])
        return coders.GlobalWindowCoder()
    elif coder_spec['@type'] == 'kind:length_prefix':
        assert len(coder_spec['component_encodings']) == 1
        return coders.LengthPrefixCoder(
            get_coder_from_spec(coder_spec['component_encodings'][0]))

    # We pass coders in the form "<coder_name>$<pickled_data>" to make the job
    # description JSON more readable.
    return coders.deserialize_coder(coder_spec['@type'])
예제 #2
0
 def get_window_coder(self):
     return coders.GlobalWindowCoder()