Exemple #1
0
class CreateByImportFileForm(forms.Form):
  """Form for step 1 (specifying file) of the import wizard"""

  # Basic Data
  name = common.HiveIdentifierField(label=_t("Table Name"), required=True)
  comment = forms.CharField(label=_t("Description"), required=False)

  # File info
  path = PathField(label=_t("Input File or Directory"))
  load_data = forms.ChoiceField(required=True,
    choices=[
      ("IMPORT", _("Import data")),
      ("EXTERNAL", _("Create External Table")),
      ("EMPTY", ("Leave Empty")),
    ],
    help_text=_t("Select 'import' to load data from the file into the Hive warehouse directory after creation. "
       "Select 'external' if the table is an external table and the data files should not be moved. " +
       "Select 'empty' if the file should only be used to define the table schema but not loaded (table will be empty).")
  )

  def __init__(self, *args, **kwargs):
    self.db = kwargs.pop('db', None)
    super(CreateByImportFileForm, self).__init__(*args, **kwargs)

  def clean_name(self):
    return _clean_tablename(self.db, self.cleaned_data['name'])

  def clean_path(self):
    path = self.cleaned_data['path']
    if path.lower().startswith(S3_ROOT):
      path = path.lower().replace(S3_ROOT, S3A_ROOT)
    if not path.endswith('/'):
      path = '%s/' % path
    return path
Exemple #2
0
class SaveResultsForm(DependencyAwareForm):
    """Used for saving the query result data"""

    SAVE_TYPES = (SAVE_TYPE_TBL, SAVE_TYPE_DIR) = (_('to a new table'),
                                                   _('to HDFS directory'))
    save_target = forms.ChoiceField(required=True,
                                    choices=common.to_choices(SAVE_TYPES),
                                    widget=forms.RadioSelect)
    target_table = common.HiveIdentifierField(
        label=_t("Table Name"),
        required=False,
        help_text=_t("Name of the new table"))
    target_dir = PathField(
        label=_t("Results Location"),
        required=False,
        help_text=_t("Empty directory in HDFS to put the results"))
    dependencies = [
        ('save_target', SAVE_TYPE_TBL, 'target_table'),
        ('save_target', SAVE_TYPE_DIR, 'target_dir'),
    ]

    def clean_target_table(self):
        tbl = self.cleaned_data.get('target_table')
        if tbl:
            try:
                db_utils.meta_client().get_table("default", tbl)
                raise forms.ValidationError(_('Table already exists'))
            except hive_metastore.ttypes.NoSuchObjectException:
                pass
        return tbl
Exemple #3
0
class LoadDataForm(forms.Form):
    """Form used for loading data into an existing table."""
    path = PathField(label=_t("Path"))
    overwrite = forms.BooleanField(required=False,
                                   initial=False,
                                   label=_t("Overwrite?"))
    is_embeddable = forms.BooleanField(required=False, initial=False)

    def __init__(self, table_obj, *args, **kwargs):
        """
    @param table_obj is a hive_metastore.thrift Table object,
    used to add fields corresponding to partition keys.
    """
        super(LoadDataForm, self).__init__(*args, **kwargs)
        self.partition_columns = dict()
        for i, column in enumerate(table_obj.partition_keys):
            # We give these numeric names because column names
            # may be unpleasantly arbitrary.
            name = "partition_%d" % i
            char_field = forms.CharField(
                required=True,
                label=_t(
                    "%(column_name)s (partition key with type %(column_type)s)"
                ) % {
                    'column_name': column.name,
                    'column_type': column.type
                })
            self.fields[name] = char_field
            self.partition_columns[name] = column.name
Exemple #4
0
class SaveResultsForm(DependencyAwareForm):
    """Used for saving the query result data"""

    SAVE_TYPES = (SAVE_TYPE_TBL, SAVE_TYPE_DIR) = ('to a new table',
                                                   'to HDFS directory')
    save_target = forms.ChoiceField(required=True,
                                    choices=common.to_choices(SAVE_TYPES),
                                    widget=forms.RadioSelect,
                                    initial=SAVE_TYPE_TBL)
    target_table = common.HiveIdentifierField(
        label=_t("Table Name"),
        required=False,
        help_text=_t("Name of the new table"))
    target_dir = PathField(
        label=_t("Results Location"),
        required=False,
        help_text=_t("Empty directory in HDFS to store results."))
    dependencies = [
        ('save_target', SAVE_TYPE_TBL, 'target_table'),
        ('save_target', SAVE_TYPE_DIR, 'target_dir'),
    ]

    def __init__(self, *args, **kwargs):
        self.db = kwargs.pop('db', None)
        self.fs = kwargs.pop('fs', None)
        super(SaveResultsForm, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(SaveResultsForm, self).clean()

        if cleaned_data.get('save_target') == SaveResultsForm.SAVE_TYPE_TBL:
            tbl = cleaned_data.get('target_table')
            if tbl:
                try:
                    if self.db is not None:
                        self.db.get_table('default',
                                          tbl)  # Assumes 'default' DB
                    self._errors['target_table'] = self.error_class(
                        [_('Table already exists')])
                    del cleaned_data['target_table']
                except Exception:
                    pass
        elif cleaned_data['save_target'] == SaveResultsForm.SAVE_TYPE_DIR:
            target_dir = cleaned_data['target_dir']
            if not target_dir.startswith('/'):
                self._errors['target_dir'] = self.error_class(
                    [_('Directory should start with /')])
            elif self.fs.exists(target_dir):
                self._errors['target_dir'] = self.error_class([
                    _('Directory already exists.')
                ])  # Overwrite destination directory content

        return cleaned_data
Exemple #5
0
class CreateByImportFileForm(forms.Form):
    """Form for step 1 (specifying file) of the import wizard"""
    # Basic Data
    name = common.HiveIdentifierField(label=_t("Table Name"), required=True)
    comment = forms.CharField(label=_t("Description"), required=False)

    # File info
    path = PathField(label=_t("Input File"))
    do_import = forms.BooleanField(
        required=False,
        initial=True,
        label=_t("Import data from file"),
        help_text=_t(
            "Automatically load this file into the table after creation."))

    def clean_name(self):
        return _clean_tablename(self.cleaned_data['name'])