TensorFlow 2.9的零零碎碎(六)-模型训练和评价

目录

fit函数的定义在哪里?

fit函数的参数

x

y

epochs

evaluate函数的定义在哪里?

evaluate函数的参数

fit函数源码

evaluate函数源码


TensorFlow 2.9的零零碎碎(二)-TensorFlow 2.9的零零碎碎(六)都是围绕使用TensorFlow 2.9在MNIST数据集上训练和评价模型来展开。

Python环境3.8。

代码调试都用的PyCharm。

在构建好数据、模型,完成模型编译之后,接下来的就是模型训练,使用fit函数,以及模型评价,使用evaluate函数。

import tensorflow as tf

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Input(shape=(28, 28)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['acc'])

model.fit(x_train, y_train, epochs=5)

model.evaluate(x_test, y_test, verbose=2)

 其实代码写到这,也就写完了,看看代码的运行结果

fit函数的定义在哪里?

通过model = tf.keras.models.Sequential(),我们知道model是Sequential类的一个对象,所以我们去Sequential类里看看

层层跳转,在keras.engine.sequential找到Sequential类的定义

@keras_export('keras.Sequential', 'keras.models.Sequential')
class Sequential(functional.Functional):
  """`Sequential` groups a linear stack of layers into a `tf.keras.Model`.

  `Sequential` provides training and inference features on this model.

可以看出,Sequential类里并没有fit函数

但是我们发现Sequential类继承了functional模块的Functional类,继续跳转

# pylint: disable=g-classes-have-attributes
class Functional(training_lib.Model):
  """A `Functional` model is a `Model` defined as a directed graph of layers.

  Three types of `Model` exist: subclassed `Model`, `Functional` model,
  and `Sequential` (a special case of `Functional`).
  In general, more Keras features are supported with `Functional`
  than with subclassed `Model`s, specifically:

  - Model cloning (`keras.models.clone`)
  - Serialization (`model.get_config()/from_config`, `model.to_json()`
  - Whole-model saving (`model.save()`)

  A `Functional` model can be instantiated by passing two arguments to
  `__init__`. The first argument is the `keras.Input` Tensors that represent
  the inputs to the model. The second argument specifies the output
  tensors that represent the outputs of this model. Both arguments can be a
  nested structure of tensors.

同样,Functional类里并没有fit函数

但是我们发现Functional类继承了training模块的Model类

注意不是training_lib模块,因为training_lib只是一个别名(from keras.engine import training as training_lib)

继续跳转

@keras_export('keras.Model', 'keras.models.Model')
class Model(base_layer.Layer, version_utils.ModelVersionSelector):
  """`Model` groups layers into an object with training and inference features.

在training模块的Model类中定义了fit函数。源码太长了,这里给一部分,全部的版本贴在最后。

  def fit(self,
          x=None,
          y=None,
          batch_size=None,
          epochs=1,
          verbose='auto',
          callbacks=None,
          validation_split=0.,
          validation_data=None,
          shuffle=True,
          class_weight=None,
          sample_weight=None,
          initial_epoch=0,
          steps_per_epoch=None,
          validation_steps=None,
          validation_batch_size=None,
          validation_freq=1,
          max_queue_size=10,
          workers=1,
          use_multiprocessing=False):
    """Trains the model for a fixed number of epochs (iterations on a dataset).

fit函数的参数

fit函数的参数比较多,但在上面我们训练模型的代码里其实就用了3个参数,分别是x(输入的数据)、y(输入数据的标签)、epochs(训练的轮数)。

x

x也就是输入的数据,可以是多种类型,比如Numpy数组、TensorFlow张量。

        x: Input data. It could be:
          - A Numpy array (or array-like), or a list of arrays
            (in case the model has multiple inputs).
          - A TensorFlow tensor, or a list of tensors
            (in case the model has multiple inputs).
          - A dict mapping input names to the corresponding array/tensors,
            if the model has named inputs.
          - A `tf.data` dataset. Should return a tuple
            of either `(inputs, targets)` or
            `(inputs, targets, sample_weights)`.
          - A generator or `keras.utils.Sequence` returning `(inputs, targets)`
            or `(inputs, targets, sample_weights)`.
          - A `tf.keras.utils.experimental.DatasetCreator`, which wraps a
            callable that takes a single argument of type
            `tf.distribute.InputContext`, and returns a `tf.data.Dataset`.
            `DatasetCreator` should be used when users prefer to specify the
            per-replica batching and sharding logic for the `Dataset`.
            See `tf.keras.utils.experimental.DatasetCreator` doc for more
            information.
          A more detailed description of unpacking behavior for iterator types
          (Dataset, generator, Sequence) is given below. If using
          `tf.distribute.experimental.ParameterServerStrategy`, only
          `DatasetCreator` type is supported for `x`.

TensorFlow前面的版本中,还有一个函数叫fit_generator。传进去的不是数组或者张量,而是生成器,生成器里会同时生成输入数据和输入数据的标签,主要用于资源有限(如GPU资源)、数据量比较大的情况,将大规模的数据切片,分别传入进行模型训练。

  def fit_generator(self,
                    generator,
                    steps_per_epoch=None,
                    epochs=1,
                    verbose=1,
                    callbacks=None,
                    validation_data=None,
                    validation_steps=None,
                    validation_freq=1,
                    class_weight=None,
                    max_queue_size=10,
                    workers=1,
                    use_multiprocessing=False,
                    shuffle=True,
                    initial_epoch=0):
    """Fits the model on data yielded batch-by-batch by a Python generator.

但在TensorFlow 2.9中,fit_generator函数已经被弃用了,这是因为fit函数将fit_generator函数的功能集成了进去。

现在的fit函数的x参数一样接受生成器作为参数。

y

y是输入数据的标签,数据类型和x类似,比如可以是Numpy数组或者TensorFlow张量,但y的数据类型需要和x一致

比如x是Numpy数组,则y也必须是Numpy数组

如果x是一个生成器,由于生成器里会同时生成输入数据和输入数据的标签,所以这里的y应该留空(不指定)

        y: Target data. Like the input data `x`,
          it could be either Numpy array(s) or TensorFlow tensor(s).
          It should be consistent with `x` (you cannot have Numpy inputs and
          tensor targets, or inversely). If `x` is a dataset, generator,
          or `keras.utils.Sequence` instance, `y` should
          not be specified (since targets will be obtained from `x`).

epochs

epochs是训练的轮数,也有用“迭代数”这个词的

如果不设置steps_per_epoch参数,则一个epoch(轮)指完整的在x和y的所有数据上训练一次

        epochs: Integer. Number of epochs to train the model.
            An epoch is an iteration over the entire `x` and `y`
            data provided
            (unless the `steps_per_epoch` flag is set to
            something other than None).
            Note that in conjunction with `initial_epoch`,
            `epochs` is to be understood as "final epoch".
            The model is not trained for a number of iterations
            given by `epochs`, but merely until the epoch
            of index `epochs` is reached.

evaluate函数的定义在哪里?

和fit函数一样的原理,在training模块下可以找到。

evaluate函数的参数

evaluate函数的参数和fit函数的参数类似

  @traceback_utils.filter_traceback
  def evaluate(self,
               x=None,
               y=None,
               batch_size=None,
               verbose='auto',
               sample_weight=None,
               steps=None,
               callbacks=None,
               max_queue_size=10,
               workers=1,
               use_multiprocessing=False,
               return_dict=False,
               **kwargs):
    """Returns the loss value & metrics values for the model in test mode.

    Computation is done in batches (see the `batch_size` arg.)

在上面我们评价模型的代码里 ,有一个verbose=2

这个参数在fit函数中也有,用于控制打印过程信息

verbose参数有4个取值,auto、0、1、2,默认值是auto

verbose=2指的是逐行打印

这个自己试试看看效果就很清楚了

        verbose: `"auto"`, 0, 1, or 2. Verbosity mode.
            0 = silent, 1 = progress bar, 2 = single line.
            `"auto"` defaults to 1 for most cases, and to 2 when used with
            `ParameterServerStrategy`. Note that the progress bar is not
            particularly useful when logged to a file, so `verbose=2` is
            recommended when not running interactively (e.g. in a production
            environment).

fit函数源码

  @traceback_utils.filter_traceback
  def fit(self,
          x=None,
          y=None,
          batch_size=None,
          epochs=1,
          verbose='auto',
          callbacks=None,
          validation_split=0.,
          validation_data=None,
          shuffle=True,
          class_weight=None,
          sample_weight=None,
          initial_epoch=0,
          steps_per_epoch=None,
          validation_steps=None,
          validation_batch_size=None,
          validation_freq=1,
          max_queue_size=10,
          workers=1,
          use_multiprocessing=False):
    """Trains the model for a fixed number of epochs (iterations on a dataset).

    Args:
        x: Input data. It could be:
          - A Numpy array (or array-like), or a list of arrays
            (in case the model has multiple inputs).
          - A TensorFlow tensor, or a list of tensors
            (in case the model has multiple inputs).
          - A dict mapping input names to the corresponding array/tensors,
            if the model has named inputs.
          - A `tf.data` dataset. Should return a tuple
            of either `(inputs, targets)` or
            `(inputs, targets, sample_weights)`.
          - A generator or `keras.utils.Sequence` returning `(inputs, targets)`
            or `(inputs, targets, sample_weights)`.
          - A `tf.keras.utils.experimental.DatasetCreator`, which wraps a
            callable that takes a single argument of type
            `tf.distribute.InputContext`, and returns a `tf.data.Dataset`.
            `DatasetCreator` should be used when users prefer to specify the
            per-replica batching and sharding logic for the `Dataset`.
            See `tf.keras.utils.experimental.DatasetCreator` doc for more
            information.
          A more detailed description of unpacking behavior for iterator types
          (Dataset, generator, Sequence) is given below. If using
          `tf.distribute.experimental.ParameterServerStrategy`, only
          `DatasetCreator` type is supported for `x`.
        y: Target data. Like the input data `x`,
          it could be either Numpy array(s) or TensorFlow tensor(s).
          It should be consistent with `x` (you cannot have Numpy inputs and
          tensor targets, or inversely). If `x` is a dataset, generator,
          or `keras.utils.Sequence` instance, `y` should
          not be specified (since targets will be obtained from `x`).
        batch_size: Integer or `None`.
            Number of samples per gradient update.
            If unspecified, `batch_size` will default to 32.
            Do not specify the `batch_size` if your data is in the
            form of datasets, generators, or `keras.utils.Sequence` instances
            (since they generate batches).
        epochs: Integer. Number of epochs to train the model.
            An epoch is an iteration over the entire `x` and `y`
            data provided
            (unless the `steps_per_epoch` flag is set to
            something other than None).
            Note that in conjunction with `initial_epoch`,
            `epochs` is to be understood as "final epoch".
            The model is not trained for a number of iterations
            given by `epochs`, but merely until the epoch
            of index `epochs` is reached.
        verbose: 'auto', 0, 1, or 2. Verbosity mode.
            0 = silent, 1 = progress bar, 2 = one line per epoch.
            'auto' defaults to 1 for most cases, but 2 when used with
            `ParameterServerStrategy`. Note that the progress bar is not
            particularly useful when logged to a file, so verbose=2 is
            recommended when not running interactively (eg, in a production
            environment).
        callbacks: List of `keras.callbacks.Callback` instances.
            List of callbacks to apply during training.
            See `tf.keras.callbacks`. Note `tf.keras.callbacks.ProgbarLogger`
            and `tf.keras.callbacks.History` callbacks are created automatically
            and need not be passed into `model.fit`.
            `tf.keras.callbacks.ProgbarLogger` is created or not based on
            `verbose` argument to `model.fit`.
            Callbacks with batch-level calls are currently unsupported with
            `tf.distribute.experimental.ParameterServerStrategy`, and users are
            advised to implement epoch-level calls instead with an appropriate
            `steps_per_epoch` value.
        validation_split: Float between 0 and 1.
            Fraction of the training data to be used as validation data.
            The model will set apart this fraction of the training data,
            will not train on it, and will evaluate
            the loss and any model metrics
            on this data at the end of each epoch.
            The validation data is selected from the last samples
            in the `x` and `y` data provided, before shuffling. This argument is
            not supported when `x` is a dataset, generator or
            `keras.utils.Sequence` instance.
            If both `validation_data` and `validation_split` are provided,
            `validation_data` will override `validation_split`.
            `validation_split` is not yet supported with
            `tf.distribute.experimental.ParameterServerStrategy`.
        validation_data: Data on which to evaluate
            the loss and any model metrics at the end of each epoch.
            The model will not be trained on this data. Thus, note the fact
            that the validation loss of data provided using `validation_split`
            or `validation_data` is not affected by regularization layers like
            noise and dropout.
            `validation_data` will override `validation_split`.
            `validation_data` could be:
              - A tuple `(x_val, y_val)` of Numpy arrays or tensors.
              - A tuple `(x_val, y_val, val_sample_weights)` of NumPy arrays.
              - A `tf.data.Dataset`.
              - A Python generator or `keras.utils.Sequence` returning
              `(inputs, targets)` or `(inputs, targets, sample_weights)`.
            `validation_data` is not yet supported with
            `tf.distribute.experimental.ParameterServerStrategy`.
        shuffle: Boolean (whether to shuffle the training data
            before each epoch) or str (for 'batch'). This argument is ignored
            when `x` is a generator or an object of tf.data.Dataset.
            'batch' is a special option for dealing
            with the limitations of HDF5 data; it shuffles in batch-sized
            chunks. Has no effect when `steps_per_epoch` is not `None`.
        class_weight: Optional dictionary mapping class indices (integers)
            to a weight (float) value, used for weighting the loss function
            (during training only).
            This can be useful to tell the model to
            "pay more attention" to samples from
            an under-represented class.
        sample_weight: Optional Numpy array of weights for
            the training samples, used for weighting the loss function
            (during training only). You can either pass a flat (1D)
            Numpy array with the same length as the input samples
            (1:1 mapping between weights and samples),
            or in the case of temporal data,
            you can pass a 2D array with shape
            `(samples, sequence_length)`,
            to apply a different weight to every timestep of every sample. This
            argument is not supported when `x` is a dataset, generator, or
           `keras.utils.Sequence` instance, instead provide the sample_weights
            as the third element of `x`.
        initial_epoch: Integer.
            Epoch at which to start training
            (useful for resuming a previous training run).
        steps_per_epoch: Integer or `None`.
            Total number of steps (batches of samples)
            before declaring one epoch finished and starting the
            next epoch. When training with input tensors such as
            TensorFlow data tensors, the default `None` is equal to
            the number of samples in your dataset divided by
            the batch size, or 1 if that cannot be determined. If x is a
            `tf.data` dataset, and 'steps_per_epoch'
            is None, the epoch will run until the input dataset is exhausted.
            When passing an infinitely repeating dataset, you must specify the
            `steps_per_epoch` argument. If `steps_per_epoch=-1` the training
            will run indefinitely with an infinitely repeating dataset.
            This argument is not supported with array inputs.
            When using `tf.distribute.experimental.ParameterServerStrategy`:
              * `steps_per_epoch=None` is not supported.
        validation_steps: Only relevant if `validation_data` is provided and
            is a `tf.data` dataset. Total number of steps (batches of
            samples) to draw before stopping when performing validation
            at the end of every epoch. If 'validation_steps' is None, validation
            will run until the `validation_data` dataset is exhausted. In the
            case of an infinitely repeated dataset, it will run into an
            infinite loop. If 'validation_steps' is specified and only part of
            the dataset will be consumed, the evaluation will start from the
            beginning of the dataset at each epoch. This ensures that the same
            validation samples are used every time.
        validation_batch_size: Integer or `None`.
            Number of samples per validation batch.
            If unspecified, will default to `batch_size`.
            Do not specify the `validation_batch_size` if your data is in the
            form of datasets, generators, or `keras.utils.Sequence` instances
            (since they generate batches).
        validation_freq: Only relevant if validation data is provided. Integer
            or `collections.abc.Container` instance (e.g. list, tuple, etc.).
            If an integer, specifies how many training epochs to run before a
            new validation run is performed, e.g. `validation_freq=2` runs
            validation every 2 epochs. If a Container, specifies the epochs on
            which to run validation, e.g. `validation_freq=[1, 2, 10]` runs
            validation at the end of the 1st, 2nd, and 10th epochs.
        max_queue_size: Integer. Used for generator or `keras.utils.Sequence`
            input only. Maximum size for the generator queue.
            If unspecified, `max_queue_size` will default to 10.
        workers: Integer. Used for generator or `keras.utils.Sequence` input
            only. Maximum number of processes to spin up
            when using process-based threading. If unspecified, `workers`
            will default to 1.
        use_multiprocessing: Boolean. Used for generator or
            `keras.utils.Sequence` input only. If `True`, use process-based
            threading. If unspecified, `use_multiprocessing` will default to
            `False`. Note that because this implementation relies on
            multiprocessing, you should not pass non-picklable arguments to
            the generator as they can't be passed easily to children processes.

    Unpacking behavior for iterator-like inputs:
        A common pattern is to pass a tf.data.Dataset, generator, or
      tf.keras.utils.Sequence to the `x` argument of fit, which will in fact
      yield not only features (x) but optionally targets (y) and sample weights.
      Keras requires that the output of such iterator-likes be unambiguous. The
      iterator should return a tuple of length 1, 2, or 3, where the optional
      second and third elements will be used for y and sample_weight
      respectively. Any other type provided will be wrapped in a length one
      tuple, effectively treating everything as 'x'. When yielding dicts, they
      should still adhere to the top-level tuple structure.
      e.g. `({"x0": x0, "x1": x1}, y)`. Keras will not attempt to separate
      features, targets, and weights from the keys of a single dict.
        A notable unsupported data type is the namedtuple. The reason is that
      it behaves like both an ordered datatype (tuple) and a mapping
      datatype (dict). So given a namedtuple of the form:
          `namedtuple("example_tuple", ["y", "x"])`
      it is ambiguous whether to reverse the order of the elements when
      interpreting the value. Even worse is a tuple of the form:
          `namedtuple("other_tuple", ["x", "y", "z"])`
      where it is unclear if the tuple was intended to be unpacked into x, y,
      and sample_weight or passed through as a single element to `x`. As a
      result the data processing code will simply raise a ValueError if it
      encounters a namedtuple. (Along with instructions to remedy the issue.)

    Returns:
        A `History` object. Its `History.history` attribute is
        a record of training loss values and metrics values
        at successive epochs, as well as validation loss values
        and validation metrics values (if applicable).

    Raises:
        RuntimeError: 1. If the model was never compiled or,
        2. If `model.fit` is  wrapped in `tf.function`.

        ValueError: In case of mismatch between the provided input data
            and what the model expects or when the input data is empty.
    """
    base_layer.keras_api_gauge.get_cell('fit').set(True)
    # Legacy graph support is contained in `training_v1.Model`.
    version_utils.disallow_legacy_graph('Model', 'fit')
    self._assert_compile_was_called()
    self._check_call_args('fit')
    _disallow_inside_tf_function('fit')

    verbose = _get_verbosity(verbose, self.distribute_strategy)

    if validation_split and validation_data is None:
      # Create the validation data using the training data. Only supported for
      # `Tensor` and `NumPy` input.
      (x, y, sample_weight), validation_data = (
          data_adapter.train_validation_split(
              (x, y, sample_weight), validation_split=validation_split))

    if validation_data:
      val_x, val_y, val_sample_weight = (
          data_adapter.unpack_x_y_sample_weight(validation_data))

    if self.distribute_strategy._should_use_with_coordinator:  # pylint: disable=protected-access
      self._cluster_coordinator = tf.distribute.experimental.coordinator.ClusterCoordinator(
          self.distribute_strategy)

    with self.distribute_strategy.scope(), \
         training_utils.RespectCompiledTrainableState(self):
      # Creates a `tf.data.Dataset` and handles batch and epoch iteration.
      data_handler = data_adapter.get_data_handler(
          x=x,
          y=y,
          sample_weight=sample_weight,
          batch_size=batch_size,
          steps_per_epoch=steps_per_epoch,
          initial_epoch=initial_epoch,
          epochs=epochs,
          shuffle=shuffle,
          class_weight=class_weight,
          max_queue_size=max_queue_size,
          workers=workers,
          use_multiprocessing=use_multiprocessing,
          model=self,
          steps_per_execution=self._steps_per_execution)

      # Container that configures and calls `tf.keras.Callback`s.
      if not isinstance(callbacks, callbacks_module.CallbackList):
        callbacks = callbacks_module.CallbackList(
            callbacks,
            add_history=True,
            add_progbar=verbose != 0,
            model=self,
            verbose=verbose,
            epochs=epochs,
            steps=data_handler.inferred_steps)

      self.stop_training = False
      self.train_function = self.make_train_function()
      self._train_counter.assign(0)
      callbacks.on_train_begin()
      training_logs = None
      # Handle fault-tolerance for multi-worker.
      # TODO(omalleyt): Fix the ordering issues that mean this has to
      # happen after `callbacks.on_train_begin`.
      data_handler._initial_epoch = (  # pylint: disable=protected-access
          self._maybe_load_initial_epoch_from_ckpt(initial_epoch))
      logs = None
      for epoch, iterator in data_handler.enumerate_epochs():
        self.reset_metrics()
        callbacks.on_epoch_begin(epoch)
        with data_handler.catch_stop_iteration():
          data_handler._initial_step = self._maybe_load_initial_step_from_ckpt()  # pylint: disable=protected-access
          for step in data_handler.steps():
            with tf.profiler.experimental.Trace(
                'train',
                epoch_num=epoch,
                step_num=step,
                batch_size=batch_size,
                _r=1):
              callbacks.on_train_batch_begin(step)
              tmp_logs = self.train_function(iterator)
              if data_handler.should_sync:
                context.async_wait()
              logs = tmp_logs  # No error, now safe to assign to logs.
              end_step = step + data_handler.step_increment
              callbacks.on_train_batch_end(end_step, logs)
              if self.stop_training:
                break

        logs = tf_utils.sync_to_numpy_or_python_type(logs)
        if logs is None:
          raise ValueError('Unexpected result of `train_function` '
                           '(Empty logs). Please use '
                           '`Model.compile(..., run_eagerly=True)`, or '
                           '`tf.config.run_functions_eagerly(True)` for more '
                           'information of where went wrong, or file a '
                           'issue/bug to `tf.keras`.')
        epoch_logs = copy.copy(logs)

        # Run validation.
        if validation_data and self._should_eval(epoch, validation_freq):
          # Create data_handler for evaluation and cache it.
          if getattr(self, '_eval_data_handler', None) is None:
            self._eval_data_handler = data_adapter.get_data_handler(
                x=val_x,
                y=val_y,
                sample_weight=val_sample_weight,
                batch_size=validation_batch_size or batch_size,
                steps_per_epoch=validation_steps,
                initial_epoch=0,
                epochs=1,
                max_queue_size=max_queue_size,
                workers=workers,
                use_multiprocessing=use_multiprocessing,
                model=self,
                steps_per_execution=self._steps_per_execution)
          val_logs = self.evaluate(
              x=val_x,
              y=val_y,
              sample_weight=val_sample_weight,
              batch_size=validation_batch_size or batch_size,
              steps=validation_steps,
              callbacks=callbacks,
              max_queue_size=max_queue_size,
              workers=workers,
              use_multiprocessing=use_multiprocessing,
              return_dict=True,
              _use_cached_eval_dataset=True)
          val_logs = {'val_' + name: val for name, val in val_logs.items()}
          epoch_logs.update(val_logs)

        callbacks.on_epoch_end(epoch, epoch_logs)
        training_logs = epoch_logs
        if self.stop_training:
          break

      if isinstance(self.optimizer, optimizer_experimental.Optimizer):
        self.optimizer.finalize_variable_values(self.trainable_variables)

      # If eval data_handler exists, delete it after all epochs are done.
      if getattr(self, '_eval_data_handler', None) is not None:
        del self._eval_data_handler
      callbacks.on_train_end(logs=training_logs)
      return self.history

evaluate函数源码

  @traceback_utils.filter_traceback
  def evaluate(self,
               x=None,
               y=None,
               batch_size=None,
               verbose='auto',
               sample_weight=None,
               steps=None,
               callbacks=None,
               max_queue_size=10,
               workers=1,
               use_multiprocessing=False,
               return_dict=False,
               **kwargs):
    """Returns the loss value & metrics values for the model in test mode.

    Computation is done in batches (see the `batch_size` arg.)

    Args:
        x: Input data. It could be:
          - A Numpy array (or array-like), or a list of arrays
            (in case the model has multiple inputs).
          - A TensorFlow tensor, or a list of tensors
            (in case the model has multiple inputs).
          - A dict mapping input names to the corresponding array/tensors,
            if the model has named inputs.
          - A `tf.data` dataset. Should return a tuple
            of either `(inputs, targets)` or
            `(inputs, targets, sample_weights)`.
          - A generator or `keras.utils.Sequence` returning `(inputs, targets)`
            or `(inputs, targets, sample_weights)`.
          A more detailed description of unpacking behavior for iterator types
          (Dataset, generator, Sequence) is given in the `Unpacking behavior
          for iterator-like inputs` section of `Model.fit`.
        y: Target data. Like the input data `x`, it could be either Numpy
          array(s) or TensorFlow tensor(s). It should be consistent with `x`
          (you cannot have Numpy inputs and tensor targets, or inversely). If
          `x` is a dataset, generator or `keras.utils.Sequence` instance, `y`
          should not be specified (since targets will be obtained from the
          iterator/dataset).
        batch_size: Integer or `None`. Number of samples per batch of
          computation. If unspecified, `batch_size` will default to 32. Do not
          specify the `batch_size` if your data is in the form of a dataset,
          generators, or `keras.utils.Sequence` instances (since they generate
          batches).
        verbose: `"auto"`, 0, 1, or 2. Verbosity mode.
            0 = silent, 1 = progress bar, 2 = single line.
            `"auto"` defaults to 1 for most cases, and to 2 when used with
            `ParameterServerStrategy`. Note that the progress bar is not
            particularly useful when logged to a file, so `verbose=2` is
            recommended when not running interactively (e.g. in a production
            environment).
        sample_weight: Optional Numpy array of weights for the test samples,
          used for weighting the loss function. You can either pass a flat (1D)
          Numpy array with the same length as the input samples
            (1:1 mapping between weights and samples), or in the case of
              temporal data, you can pass a 2D array with shape `(samples,
              sequence_length)`, to apply a different weight to every timestep
              of every sample. This argument is not supported when `x` is a
              dataset, instead pass sample weights as the third element of `x`.
        steps: Integer or `None`. Total number of steps (batches of samples)
          before declaring the evaluation round finished. Ignored with the
          default value of `None`. If x is a `tf.data` dataset and `steps` is
          None, 'evaluate' will run until the dataset is exhausted. This
          argument is not supported with array inputs.
        callbacks: List of `keras.callbacks.Callback` instances. List of
          callbacks to apply during evaluation. See
          [callbacks](/api_docs/python/tf/keras/callbacks).
        max_queue_size: Integer. Used for generator or `keras.utils.Sequence`
          input only. Maximum size for the generator queue. If unspecified,
          `max_queue_size` will default to 10.
        workers: Integer. Used for generator or `keras.utils.Sequence` input
          only. Maximum number of processes to spin up when using process-based
          threading. If unspecified, `workers` will default to 1.
        use_multiprocessing: Boolean. Used for generator or
          `keras.utils.Sequence` input only. If `True`, use process-based
          threading. If unspecified, `use_multiprocessing` will default to
          `False`. Note that because this implementation relies on
          multiprocessing, you should not pass non-picklable arguments to the
          generator as they can't be passed easily to children processes.
        return_dict: If `True`, loss and metric results are returned as a dict,
          with each key being the name of the metric. If `False`, they are
          returned as a list.
        **kwargs: Unused at this time.

    See the discussion of `Unpacking behavior for iterator-like inputs` for
    `Model.fit`.

    Returns:
        Scalar test loss (if the model has a single output and no metrics)
        or list of scalars (if the model has multiple outputs
        and/or metrics). The attribute `model.metrics_names` will give you
        the display labels for the scalar outputs.

    Raises:
        RuntimeError: If `model.evaluate` is wrapped in a `tf.function`.
    """
    base_layer.keras_api_gauge.get_cell('evaluate').set(True)
    version_utils.disallow_legacy_graph('Model', 'evaluate')
    self._assert_compile_was_called()
    self._check_call_args('evaluate')
    self._check_sample_weight_warning(x, sample_weight)
    _disallow_inside_tf_function('evaluate')
    use_cached_eval_dataset = kwargs.pop('_use_cached_eval_dataset', False)
    if kwargs:
      raise TypeError(f'Invalid keyword arguments: {list(kwargs.keys())}')

    if self.distribute_strategy._should_use_with_coordinator:  # pylint: disable=protected-access
      self._cluster_coordinator = tf.distribute.experimental.coordinator.ClusterCoordinator(
          self.distribute_strategy)

    verbose = _get_verbosity(verbose, self.distribute_strategy)
    with self.distribute_strategy.scope():
      # Use cached evaluation data only when it's called in `Model.fit`
      if (use_cached_eval_dataset
          and getattr(self, '_eval_data_handler', None) is not None):
        data_handler = self._eval_data_handler
      else:
        # Creates a `tf.data.Dataset` and handles batch and epoch iteration.
        data_handler = data_adapter.get_data_handler(
            x=x,
            y=y,
            sample_weight=sample_weight,
            batch_size=batch_size,
            steps_per_epoch=steps,
            initial_epoch=0,
            epochs=1,
            max_queue_size=max_queue_size,
            workers=workers,
            use_multiprocessing=use_multiprocessing,
            model=self,
            steps_per_execution=self._steps_per_execution)

      # Container that configures and calls `tf.keras.Callback`s.
      if not isinstance(callbacks, callbacks_module.CallbackList):
        callbacks = callbacks_module.CallbackList(
            callbacks,
            add_history=True,
            add_progbar=verbose != 0,
            model=self,
            verbose=verbose,
            epochs=1,
            steps=data_handler.inferred_steps)

      logs = {}
      self.test_function = self.make_test_function()
      self._test_counter.assign(0)
      callbacks.on_test_begin()
      for _, iterator in data_handler.enumerate_epochs():  # Single epoch.
        self.reset_metrics()
        with data_handler.catch_stop_iteration():
          for step in data_handler.steps():
            with tf.profiler.experimental.Trace('test', step_num=step, _r=1):
              callbacks.on_test_batch_begin(step)
              tmp_logs = self.test_function(iterator)
              if data_handler.should_sync:
                context.async_wait()
              logs = tmp_logs  # No error, now safe to assign to logs.
              end_step = step + data_handler.step_increment
              callbacks.on_test_batch_end(end_step, logs)
      logs = tf_utils.sync_to_numpy_or_python_type(logs)
      callbacks.on_test_end(logs=logs)

      if return_dict:
        return logs
      else:
        return flatten_metrics_in_order(logs, self.metrics_names)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lua-Resty-Checkups是一个基于lua的upstream管理和健康检查模块,由又拍云开源。特点:支持周期性upstream服务管理操作支持管理和健康检查支持upstream动态更新有利于加权轮询或哈希平衡支持 Nginx C upstream同步操作可使用级别和键值实现集群使用简介:-- config.lua _M = {} _M.global = {     checkup_timer_interval = 15,     checkup_shd_sync_enable = true,     shd_config_timer_interval = 1, } _M.ups1 = {     cluster = {         {             servers = {                 {host="127.0.0.1", port=4444, weight=10, max_fails=3, fail_timeout=10},             }         },     }, }lua_package_path "/path/to/lua-resty-checkups/lib/checkups/?.lua;/path/to/config.lua;;"; lua_shared_dict state 10m; lua_shared_dict mutex 1m; lua_shared_dict locks 1m; lua_shared_dict config 10m; server {     listen 12350;     return 200 12350; } server {     listen 12351;     return 200 12351; } init_worker_by_lua_block {     local config = require "config"     local checkups = require "resty.checkups.api"     checkups.prepare_checker(config)     checkups.create_checker() } server {     location = /12350 {         proxy_pass http://127.0.0.1:12350/;     }     location = /12351 {         proxy_pass http://127.0.0.1:12351/;     }     location = /t {         content_by_lua_block {             local checkups = require "resty.checkups.api"             local callback = function(host, port)                 local res = ngx.location.capture("/" .. port)                 ngx.say(res.body)                 return 1             end             local ok, err             -- connect to a dead server, no upstream available             ok, err = checkups.ready_ok("ups1", callback)             if err then ngx.say(err) end             -- add server to ups1             ok, err = checkups.update_upstream("ups1", {                     {                         servers = {                             {host="127.0.0.1", port=12350, weight=10, max_fails=3, fail_timeout=10},                         }                     },                 })             if err then ngx.say(err) end             ngx.sleep(1)             ok, err = checkups.ready_ok("ups1", callback)             if err then ngx.say(err) end             ok, err = checkups.ready_ok("ups1", callback)             if err then ngx.say(err) end             -- add server to new upstream             ok, err = checkups.update_upstream("ups2", {                     {                         servers = {                             {host="127.0.0.1", port=12351},                         }                     },                 })             if err then ngx.say(err) end             ngx.sleep(1)             ok, err = checkups.ready_ok("ups2", callback)             if err then ngx.say(err) end             -- add server to ups2, reset rr state             ok, err = checkups.update_upstream("ups2", {                     {                         servers = {                             {host="127.0.0.1", port=12350, weight=10, max_fails=3, fail_timeout=10},                             {host="127.0.0.1", port=12351, weight=10, max_fails=3, fail_timeout=10},                         }                     },                 })             if err then ngx.say(err) end             ngx.sleep(1)             ok, err = checkups.ready_ok("ups2", callback)             if err then ngx.say(err) end             ok, err = checkups.ready_ok("ups2", callback)             if err then ngx.say(err) end     } }Lua 配置示例:_M = {} -- Here is the global part _M.global = {     checkup_timer_interval = 15,     checkup_timer_overtime = 60,     default_heartbeat_enable = true,     checkup_shd_sync_enable = true,     shd_config_timer_interval = 1, } -- The rests parts are cluster configurations _M.redis = {     enable = true,     typ = "redis",     timeout = 2,     read_timeout = 15,     send_timeout = 15,     protected = true,     cluster = {         {   -- level 1             try = 2,             servers = {                 { host = "192.168.0.1", port = 6379, weight=10, max_fails=3, fail_timeout=10 },                 { host = "192.168.0.2", port = 6379, weight=10, max_fails=3, fail_timeout=10 },             }         },         {   -- level 2             servers = {                 { host = "192.168.0.3", port = 6379, weight=10, max_fails=3, fail_timeout=10 },             }         },     }, } _M.api = {     enable = false,     typ = "http",     http_opts = {         query = "GET /status HTTP/1.1\r\nHost: localhost\r\n\r\n",         statuses = {             [500] = false,             [502] = false,             [503] = false,             [504] = false,         },     },     mode = "hash",     cluster = {         dc1 = {             servers = {                 { host = "192.168.1.1", port = 1234, weight=10, max_fails=3, fail_timeout=10 },             }         },         dc2 = {             servers = {                 { host = "192.168.1.2", port = 1234, weight=10, max_fails=3, fail_timeout=10 },             }         }     } } _M.ups_from_nginx = {     timeout = 2,     cluster = {         {   -- level 1             upstream = "api.com",         },         {   -- level 2             upstream = "api.com",             upstream_only_backup = true,         },     }, } return _M
ORACLE常用命令 一、ORACLE的启动和关闭 1、在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 su - oracle a、启动ORACLE系统 oracle>svrmgrl SVRMGR>connect internal SVRMGR>startup SVRMGR>quit b、关闭ORACLE系统 oracle>svrmgrl SVRMGR>connect internal SVRMGR>shutdown SVRMGR>quit 启动oracle9i数据库命令: $ sqlplus /nolog SQL*Plus: Release 9.2.0.1.0 - Production on Fri Oct 31 13:53:53 2003 Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved. SQL> connect / as sysdba Connected to an idle instance. SQL> startup^C SQL> startup ORACLE instance started. 2、在双机环境下 要想启动或关闭ORACLE系统必须首先切换到root用户,如下 su - root a、启动ORACLE系统 hareg -y oracle b、关闭ORACLE系统 hareg -n oracle Oracle数据库有哪几种启动方式 说明: 有以下几种启动方式: 1、startup nomount 非安装启动,这种方式启动下可执行:重建控制文件、重建数据库 读取init.ora文件,启动instance,即启动SGA和后台进程,这种启动只需要init.ora文件。 2、startup mount dbname 安装启动,这种方式启动下可执行: 数据库日志归档、 数据库介质恢复、 使数据文件联机或脱机, 重新定位数据文件、重做日志文件。 执行“nomount”,然后打开控制文件,确认数据文件和联机日志文件的位置, 但此时不对数据文件和日志文件进行校验检查。 3、startup open dbname 先执行“nomount”,然后执行“mount”,再打开包括Redo log文件在内的所有数据库文件, 这种方式下可访问数据库中的数据。 4、startup,等于以下三个命令 startup nomount alter database mount alter database open 5、startup restrict 约束方式启动 这种方式能够启动数据库,但只允许具有一定特权的用户访问 非特权用户访问时,会出现以下提示: ERROR: ORA-01035: ORACLE 只允许具有 RESTRICTED SESSION 权限的用户使用 6、startup force 强制启动方式 当不能关闭数据库时,可以用startup force来完成数据库的关闭 先关闭数据库,再执行正常启动数据库命令 7、startup pfile=参数文件名 带初始化参数文件的启动方式 先读取参数文件,再按参数文件中的设置启动数据库 例:startup pfile=E:Oracleadminoradbpfileinit.ora 8、startup EXCLUSIVE 二、用户如何有效地利用数据字典  ORACLE的数据字典是数据库的重要组成部分之一,它随着数据库的产生而产生, 随着数据库的变化而变化, 体现为sys用户下的一些表和视图。数据字典名称是大写的英文字符。 数据字典里存有用户信息、用户的权限信息、所有数据对象信息、表的约束条件、统计分析数据库的视图等。 我们不能手工修改数据字典里的信息。   很多时候,一般的ORACLE用户不知道如何有效地利用它。   dictionary   全部数据字典表的名称和解释,它有一个同义词dict dict_column   全部数据字典表里字段名称和解释 如果我们想查询跟索引有关的数据字典时,可以用下面这条SQL语句: SQL>select * from dictionary where instr(comments,'index')>0; 如果我们想知道user_indexes表各字段名称的详细含义,可以用下面这条SQL语句: SQL>select column_name,comments from dict_columns where table_name='USER_INDEXES'; 依此类推,就可以轻松知道数据字典的详细名称和解释,不用查看ORACLE的其它文档资料了。 下面按类别列出一些ORACLE用户常用数据字典的查询使用方法。 1、用户 查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * from user_role_privs; 查看当前用户的系统权限和表级权限 SQL>select * from user_sys_privs; SQL>select * from user_tab_privs; 2、表 查看用户下所有的表 SQL>select * from user_tables; 查看名称包含log字符的表 SQL>select object_name,object_id from user_objects where instr(object_name,'LOG')>0; 查看某表的创建时间 SQL>select object_name,created from user_objects where object_name=upper('&table_name'); 查看某表的大小 SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&table_name'); 查看放在ORACLE的内存区里的表 SQL>select table_name,cache from user_tables where instr(cache,'Y')>0; 3、索引 查看索引个数和类别 SQL>select index_name,index_type,table_name from user_indexes order by table_name; 查看索引被索引的字段 SQL>select * from user_ind_columns where index_name=upper('&index_name'); 查看索引的大小 SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&index_name'); 4、序列号 查看序列号,last_number是当前值 SQL>select * from user_sequences; 5、视图 查看视图的名称 SQL>select view_name from user_views; 查看创建视图的select语句 SQL>set view_name,text_length from user_views; SQL>set long 2000; 说明:可以根据视图的text_length值设定set long 的大小 SQL>select text from user_views where view_name=upper('&view_name'); 6、同义词 查看同义词的名称 SQL>select * from user_synonyms; 7、约束条件 查看某表的约束条件 SQL>select constraint_name, constraint_type,search_condition, r_constraint_name from user_constraints where table_name = upper('&table_name'); SQL>select c.constraint_name,c.constraint_type,cc.column_name from user_constraints c,user_cons_columns cc where c.owner = upper('&table_owner') and c.table_name = upper('&table_name') and c.owner = cc.owner and c.constraint_name = cc.constraint_name order by cc.position; 8、存储函数和过程 查看函数和过程的状态 SQL>select object_name,status from user_objects where object_type='FUNCTION'; SQL>select object_name,status from user_objects where object_type='PROCEDURE'; 查看函数和过程的源代码 SQL>select text from all_source where owner=user and name=upper('&plsql_name'); 三、查看数据库的SQL 1、查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name group by t.tablespace_name; 2、查看表空间物理文件的名称及大小 select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name; 3、查看回滚段名称及大小 select segment_name, tablespace_name, r.status, (initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent, max_extents, v.curext CurExtent From dba_rollback_segs r, v$rollstat v Where r.segment_id = v.usn(+) order by segment_name ; 4、查看控制文件 select name from v$controlfile; 5、查看日志文件 select member from v$logfile; 6、查看表空间的使用情况 select sum(bytes)/(1024*1024) as free_space,tablespace_name from dba_free_space group by tablespace_name; SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE, (B.BYTES*100)/A.BYTES "% USED",(C.BYTES*100)/A.BYTES "% FREE" FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_USED B,SYS.SM$TS_FREE C WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND A.TABLESPACE_NAME=C.TABLESPACE_NAME; 7、查看数据库库对象 select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status; 8、查看数据库的版本 Select version FROM Product_component_version Where SUBSTR(PRODUCT,1,6)='Oracle'; 9、查看数据库的创建日期和归档方式 Select Created, Log_Mode, Log_Mode From V$Database; 四、ORACLE用户连接的管理 用系统管理员,查看当前数据库有几个用户连接: SQL> select username,sid,serial# from v$session; 如果要停某个连接用 SQL> alter system kill session 'sid,serial#'; 如果这命令不行,找它UNIX的进程数 SQL> select pro.spid from v$session ses,v$process pro where ses.sid=21 and ses.paddr=pro.addr; 说明:21是某个连接的sid数 然后用 kill 命令杀此进程号。 五、SQL*PLUS使用 a、近入SQL*Plus $sqlplus 用户名/密码 退出SQL*Plus SQL>exit b、在sqlplus下得到帮助信息 列出全部SQL命令和SQL*Plus命令 SQL>help 列出某个特定的命令的信息 SQL>help 命令名 c、显示表结构命令DESCRIBE SQL>DESC 表名 d、SQL*Plus中的编辑命令 显示SQL缓冲区命令 SQL>L 修改SQL命令 首先要将待改正行变为当前行 SQL>n 用CHANGE命令修改内容 SQL>c/旧/新 重新确认是否已正确 SQL>L 使用INPUT命令可以在SQL缓冲区中增加一行或多行 SQL>i SQL>输入内容 e、调用外部系统编辑器 SQL>edit 文件名 可以使用DEFINE命令设置系统变量EDITOR来改变文本编辑器的类型,在login.sql文件中定义如下一行 DEFINE_EDITOR=vi f、运行命令文件 SQL>START test SQL>@test 常用SQL*Plus语句 a、表的创建、修改、删除 创建表的命令格式如下: create table 表名 (列说明列表); 为基表增加新列命令如下: ALTER TABLE 表名 ADD (列说明列表) 例:为test表增加一列Age,用来存放年龄 sql>alter table test add (Age number(3)); 修改基表列定义命令如下: ALTER TABLE 表名 MODIFY (列名 数据类型) 例:将test表中的Count列宽度加长为10个字符 sql>alter atble test modify (County char(10)); b、将一张表删除语句的格式如下: DORP TABLE 表名; 例:表删除将同时删除表的数据和表的定义 sql>drop table test c、表空间的创建、删除 、ORACLE逻辑备份的SH文件 完全备份的SH文件:exp_comp.sh rq=` date +"%m%d" ` su - oracle -c "exp system/manager full=y inctype=complete file=/oracle/export/db_comp$rq.dmp" 累计备份的SH文件:exp_cumu.sh rq=` date +"%m%d" ` su - oracle -c "exp system/manager full=y inctype=cumulative file=/oracle/export/db_cumu$rq.dmp" 增量备份的SH文件: exp_incr.sh rq=` date +"%m%d" ` su - oracle -c "exp system/manager full=y inctype=incremental file=/oracle/export/db_incr$rq.dmp" root用户crontab文件 /var/spool/cron/crontabs/root增加以下内容 0 2 1 * * /oracle/exp_comp.sh 30 2 * * 0-5 /oracle/exp_incr.sh 45 2 * * 6 /oracle/exp_cumu.sh 当然这个时间表可以根据不同的需求来改变的,这只是一个例子。 七、ORACLE 常用的SQL语法和数据对象 一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSERT INTO 表名(字段名1, 字段名2, ……) SELECT (字段名1, 字段名2, ……) FROM 另外的表名; 字符串类型的字段值必须用单引号括起来, 例如: ’GOOD DAY’ 如果字段值里包含单引号’ 需要进行字符串转换, 我们把它替换成两个单引号''. 字符串类型的字段值超过定义的长度会出错, 最好在插入前进行长度校验. 日期字段的字段值可以用当前数据库的系统时间SYSDATE, 精确到秒 或者用字符串转换成日期型函数TO_DATE(‘2001-08-01’,’YYYY-MM-DD’) TO_DATE()还有很多种日期格式, 可以参看ORACLE DOC. 年-月-日 小时:分钟:秒 的格式YYYY-MM-DD HH24:MI:SS INSERT时最大可操作的字符串长度小于等于4000个单字节, 如果要插入更长的字符串, 请考虑字段用CLOB类型, 方法借用ORACLE里自带的DBMS_LOB程序包. INSERT时如果要用到从1开始自动增长的序列号, 应该先建立一个序列号 CREATE SEQUENCE 序列号的名称 (最好是表名+序列号标记) INCREMENT BY 1 START WITH 1 MAXVALUE 99999 CYCLE NOCACHE; 其中最大的值按字段的长度来定, 如果定义的自动增长的序列号 NUMBER(6) , 最大值为999999 INSERT 语句插入这个字段值为: 序列号的名称.NEXTVAL 2.DELETE (删除数据表里记录的语句) DELETE FROM表名 WHERE 条件; 注意:删除记录并不能释放ORACLE里被占用的数据块表空间. 它只把那些被删除的数据块标成unused. 如果确实要删除一个大表里的全部记录, 可以用 TRUNCATE 命令, 它可以释放占用的数据块表空间 TRUNCATE TABLE 表名; 此操作不可回退. 3.UPDATE (修改数据表里记录的语句) UPDATE表名 SET 字段名1=值1, 字段名2=值2, …… WHERE 条件; 如果修改的值N没有赋值或定义时, 将把原来的记录内容清为NULL, 最好在修改前进行非空校验; 值N超过定义的长度会出错, 最好在插入前进行长度校验.. 注意事项: A. 以上SQL语句对表都加上了行级锁, 确认完成后, 必须加上事物处理结束的命令 COMMIT 才能正式生效, 否则改变不一定写入数据库里. 如果想撤回这些操作, 可以用命令 ROLLBACK 复原. B. 在运行INSERT, DELETE 和 UPDATE 语句前最好估算一下可能操作的记录范围, 应该把它限定在较小 (一万条记录) 范围内,. 否则ORACLE处理这个事物用到很大的回退段. 程序响应慢甚至失去响应. 如果记录数上十万以上这些操作, 可以把这些SQL语句分段分次完成, 其间加上COMMIT 确认事物处理. 二.数据定义 (DDL) 部分 1.CREATE (创建表, 索引, 视图, 同义词, 过程, 函数, 数据库链接等) ORACLE常用的字段类型有 CHAR 固定长度的字符串 VARCHAR2 可变长度的字符串 NUMBER(M,N) 数字型M是位数总长度, N是小数的长度 DATE 日期类型 创建表时要把较小的不为空的字段放在前面, 可能为空的字段放在后面 创建表时可以用中文的字段名, 但最好还是用英文的字段名 创建表时可以给字段加上默认值, 例如 DEFAULT SYSDATE 这样每次插入和修改时, 不用程序操作这个字段都能得到动作的时间 创建表时可以给字段加上约束条件 例如 不允许重复 UNIQUE, 关键字 PRIMARY KEY 2.ALTER (改变表, 索引, 视图等) 改变表的名称 ALTER TABLE 表名1 TO 表名2; 在表的后面增加一个字段 ALTER TABLE表名 ADD 字段名 字段名描述; 修改表里字段的定义描述 ALTER TABLE表名 MODIFY字段名 字段名描述; 给表里的字段加上约束条件 ALTER TABLE 表名 ADD CONSTRAINT 约束名 PRIMARY KEY (字段名); ALTER TABLE 表名 ADD CONSTRAINT 约束名 UNIQUE (字段名); 把表放在或取出数据库的内存区 ALTER TABLE 表名 CACHE; ALTER TABLE 表名 NOCACHE; 3.DROP (删除表, 索引, 视图, 同义词, 过程, 函数, 数据库链接等) 删除表和它所有的约束条件 DROP TABLE 表名 CASCADE CONSTRAINTS; 4.TRUNCATE (清空表里的所有记录, 保留表的结构) TRUNCATE 表名; 三.查询语句 (SELECT) 部分 SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE 条件; 字段名可以带入函数 例如: COUNT(*), MIN(字段名), MAX(字段名), AVG(字段名), DISTINCT(字段名), TO_CHAR(DATE字段名,'YYYY-MM-DD HH24:MI:SS') NVL(EXPR1, EXPR2)函数 解释: IF EXPR1=NULL RETURN EXPR2 ELSE RETURN EXPR1 DECODE(AA﹐V1﹐R1﹐V2﹐R2....)函数 解释: IF AA=V1 THEN RETURN R1 IF AA=V2 THEN RETURN R2 ..… ELSE RETURN NULL LPAD(char1,n,char2)函数 解释: 字符char1按制定的位数n显示,不足的位数用char2字符串替换左边的空位 字段名之间可以进行算术运算 例如: (字段名1*字段名1)/3 查询语句可以嵌套 例如: SELECT …… FROM (SELECT …… FROM表名1, [表名2, ……] WHERE 条件) WHERE 条件2; 两个查询语句的结果可以做集合操作 例如: 并集UNION(去掉重复记录), 并集UNION ALL(不去掉重复记录), 差集MINUS, 交集INTERSECT 分组查询 SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] GROUP BY字段名1 [HAVING 条件] ; 两个以上表之间的连接查询 SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE 表名1.字段名 = 表名2. 字段名 [ AND ……] ; SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE 表名1.字段名 = 表名2. 字段名(+) [ AND ……] ; 有(+)号的字段位置自动补空值 查询结果集的排序操作, 默认的排序是升序ASC, 降序是DESC SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] ORDER BY字段名1, 字段名2 DESC; 字符串模糊比较的方法 INSTR(字段名, ‘字符串’)>0 字段名 LIKE ‘字符串%’ [‘%字符串%’] 每个表都有一个隐含的字段ROWID, 它标记着记录的唯一性. 四.ORACLE里常用的数据对象 (SCHEMA) 1.索引 (INDEX) CREATE INDEX 索引名ON 表名 ( 字段1, [字段2, ……] ); ALTER INDEX 索引名 REBUILD; 一个表的索引最好不要超过三个 (特殊的大表除外), 最好用单字段索引, 结合SQL语句的分析执行情况, 也可以建立多字段的组合索引和基于函数的索引 ORACLE8.1.7字符串可以索引的最大长度为1578 单字节 ORACLE8.0.6字符串可以索引的最大长度为758 单字节 2.视图 (VIEW) CREATE VIEW 视图名AS SELECT …. FROM …..; ALTER VIEW视图名 COMPILE; 视图仅是一个SQL查询语句, 它可以把表之间复杂的关系简洁化. 3.同义词 (SYNONMY) CREATE SYNONYM同义词名FOR 表名; CREATE SYNONYM同义词名FOR 表名@数据库链接名; 4.数据库链接 (DATABASE LINK) CREATE DATABASE LINK数据库链接名CONNECT TO 用户名 IDENTIFIED BY 密码 USING ‘数据库连接字符串’; 数据库连接字符串可以用NET8 EASY CONFIG或者直接修改TNSNAMES.ORA里定义. 数据库参数global_name=true时要求数据库链接名称跟远端数据库名称一样 数据库全局名称可以用以下命令查出 SELECT * FROM GLOBAL_NAME; 查询远端数据库里的表 SELECT …… FROM 表名@数据库链接名; 五.权限管理 (DCL) 语句 1.GRANT 赋于权限 常用的系统权限集合有以下三个: CONNECT(基本的连接), RESOURCE(程序开发), DBA(数据库管理) 常用的数据对象权限有以下五个: ALL ON 数据对象名, SELECT ON 数据对象名, UPDATE ON 数据对象名, DELETE ON 数据对象名, INSERT ON 数据对象名, ALTER ON 数据对象名 GRANT CONNECT, RESOURCE TO 用户名; GRANT SELECT ON 表名 TO 用户名; GRANT SELECT, INSERT, DELETE ON表名 TO 用户名1, 用户名2; 2.REVOKE 回收权限 REVOKE CONNECT, RESOURCE FROM 用户名; REVOKE SELECT ON 表名 FROM 用户名; REVOKE SELECT, INSERT, DELETE ON表名 FROM 用户名1, 用户名2; 查询数据库中第63号错误: select orgaddr,destaddr from sm_histable0116 where error_code='63'; 查询数据库中开户用户最大提交和最大下发数: select MSISDN,TCOS,OCOS from ms_usertable; 查询数据库中各种错误代码的总和: select error_code,count(*) from sm_histable0513 group by error_code order by error_code; 查询报表数据库中话单统计种类查询。 select sum(Successcount) from tbl_MiddleMt0411 where ServiceType2=111 select sum(successcount),servicetype from tbl_middlemt0411 group by servicetype 原文地址:http://www.cnoug.org/viewthread.php?tid=60293 //创建一个控制文件命令到跟踪文件 alter database backup controlfile to trace; //增加一个新的日志文件组的语句 connect internal as sysdba alter database add logfile group 4 (’/db01/oracle/CC1/log_1c.dbf’, ’/db02/oracle/CC1/log_2c.dbf’) size 5M; alter database add logfile member ’/db03/oracle/CC1/log_3c.dbf’ to group 4; //在Server Manager上MOUNT并打开一个数据库: connect internal as sysdba startup mount ORA1 exclusive; alter database open; //生成数据字典 @catalog @catproc //在init.ora 中备份数据库的位置 log_archive_dest_1 = ’/db00/arch’ log_archive_dest_state_1 = enable log_archive_dest_2 = "service=stby.world mandatory reopen=60" log_archive_dest_state_2 = enable //对用户的表空间的指定和管理相关的语句 create user USERNAME identified by PASSWORD default tablespace TABLESPACE_NAME; alter user USERNAME default tablespace TABLESPACE_NAME; alter user SYSTEM quota 0 on SYSTEM; alter user SYSTEM quota 50M on TOOLS; create user USERNAME identified by PASSWORD default tablespace DATA temporary tablespace TEMP; alter user USERNAME temporary tablespace TEMP; //重新指定一个数据文件的大小 : alter database datafile ’/db05/oracle/CC1/data01.dbf’ resize 200M; //创建一个自动扩展的数据文件: create tablespace DATA datafile ’/db05/oracle/CC1/data01.dbf’ size 200M autoextend ON next 10M maxsize 250M; //在表空间上增加一个自动扩展的数据文件: alter tablespace DATA add datafile ’/db05/oracle/CC1/data02.dbf’ size 50M autoextend ON maxsize 300M; //修改参数: alter database datafile ’/db05/oracle/CC1/data01.dbf’ autoextend ON maxsize 300M; //在数据文件移动期间重新命名: alter database rename file ’/db01/oracle/CC1/data01.dbf’ to ’/db02/oracle/CC1/data01.dbf’; alter tablespace DATA rename datafile ’/db01/oracle/CC1/data01.dbf’ to ’/db02/oracle/CC1/data01.dbf’; alter database rename file ’/db05/oracle/CC1/redo01CC1.dbf’ to ’/db02/oracle/CC1/redo01CC1.dbf’; alter database datafile ’/db05/oracle/CC1/data01.dbf’ resize 80M; //创建和使用角色: create role APPLICATION_USER; grant CREATE SESSION to APPLICATION_USER; grant APPLICATION_USER to username; //回滚段的管理 create rollback segment SEGMENT_NAME tablespace RBS; alter rollback segment SEGMENT_NAME offline; drop rollback segment SEGMENT_NAME; alter rollback segment SEGMENT_NAME online; //回滚段上指定事务 commit; set transaction use rollback segment ROLL_BATCH; insert into TABLE_NAME select * from DATA_LOAD_TABLE; commit; //查询回滚段的 大小和优化参数 select * from DBA_SEGMENTS where Segment_Type = ’ROLLBACK’; select N.Name, /* rollback segment name */ S.OptSize /* rollback segment OPTIMAL size */ from V$ROLLNAME N, V$ROLLSTAT S where N.USN=S.USN; //回收回滚段 alter rollback segment R1 shrink to 15M; alter rollback segment R1 shrink; //例子 set transaction use rollback segment SEGMENT_NAME alter tablespace RBS default storage (initial 125K next 125K minextents 18 maxextents 249) create rollback segment R4 tablespace RBS storage (optimal 2250K); alter rollback segment R4 online; select Sessions_Highwater from V$LICENSE; grant select on EMPLOYEE to PUBLIC; //用户和角色 create role ACCOUNT_CREATOR; grant CREATE SESSION, CREATE USER, ALTER USER to ACCOUNT_CREATOR; alter user THUMPER default role NONE; alter user THUMPER default role CONNECT; alter user THUMPER default role all except ACCOUNT_CREATOR; alter profile DEFAULT limit idle_time 60; create profile LIMITED_PROFILE limit FAILED_LOGIN_ATTEMPTS 5; create user JANE identified by EYRE profile LIMITED_PROFILE; grant CREATE SESSION to JANE; alter user JANE account unlock; alter user JANE account lock; alter profile LIMITED_PROFILE limit PASSWORD_LIFE_TIME 30; alter user jane password expire; //创建操作系统用户 REM Creating OPS$ accounts create user OPS$FARMER identified by SOME_PASSWORD default tablespace USERS temporary tablespace TEMP; REM Using identified externally create user OPS$FARMER identified externally default tablespace USERS temporary tablespace TEMP; //执行ORAPWD ORAPWD FILE=filename PASSWORD=password ENTRIES=max_users create role APPLICATION_USER; grant CREATE SESSION to APPLICATION_USER; create role DATA_ENTRY_CLERK; grant select, insert on THUMPER.EMPLOYEE to DATA_ENTRY_CLERK; grant select, insert on THUMPER.TIME_CARDS to DATA_ENTRY_CLERK; grant select, insert on THUMPER.DEPARTMENT to DATA_ENTRY_CLERK; grant APPLICATION_USER to DATA_ENTRY_CLERK; grant DATA_ENTRY_CLERK to MCGREGOR; grant DATA_ENTRY_CLERK to BPOTTER with admin option; //设置角色 set role DATA_ENTRY_CLERK; set role NONE; //回收权利: revoke delete on EMPLOYEE from PETER; revoke all on EMPLOYEE from MCGREGOR; //回收角色: revoke ACCOUNT_CREATOR from HELPDESK; drop user USERNAME cascade; grant SELECT on EMPLOYEE to MCGREGOR with grant option; grant SELECT on THUMPER.EMPLOYEE to BPOTTER with grant option; revoke SELECT on EMPLOYEE from MCGREGOR; create user MCGREGOR identified by VALUES ’1A2DD3CCEE354DFA’; alter user OPS$FARMER identified by VALUES ’no way’; //备份与恢复 使用 export 程序 exp system/manager file=expdat.dmp compress=Y owner=(HR,THUMPER) exp system/manager file=hr.dmp owner=HR indexes=Y compress=Y imp system/manager file=hr.dmp full=Y buffer=64000 commit=Y //备份表 exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES) //备份分区 exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES:Part1) //输入例子 imp system/manager file=expdat.dmp imp system/manager file=expdat.dmp buffer=64000 commit=Y exp system/manager file=thumper.dat owner=thumper grants=N indexes=Y compress=Y rows=Y imp system/manager file=thumper.dat FROMUSER=thumper TOUSER=flower rows=Y indexes=Y imp system/manager file=expdat.dmp full=Y commit=Y buffer=64000 imp system/manager file=expdat.dmp ignore=N rows=N commit=Y buffer=64000 //使用操作系统备份命令 REM TAR examples tar -cvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 tar -rvf /dev/rmt/0hc /orasw/app/oracle/CC1/pfile/initcc1.ora tar -rvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 /orasw/app/oracle/CC1/pfile/initcc1.ora //离线备份的shell脚本 ORACLE_SID=cc1; export ORACLE_SID ORAENV_ASK=NO; export ORAENV_ASK . oraenv svrmgrl <<EOF1 connect internal as sysdba shutdown immediate; exit EOF1 insert backup commands like the "tar" commands here svrmgrl <<EOF2 connect internal as sysdba startup EOF2 //在Server Manager上设置为archivelog mode: connect internal as sysdba startup mount cc1; alter database archivelog; archive log start; alter database open; //在Server Manager上设置为archivelog mode: connect internal as sysdba startup mount cc1; alter database noarchivelog; alter database open; select Name, Value from V$PARAMETER where Name like ’log_archive%’; //联机备份的脚本 # # Sample Hot Backup Script for a UNIX File System database # # Set up environment variables: ORACLE_SID=cc1; export ORACLE_SID ORAENV_ASK=NO; export ORAENV_ASK . oraenv svrmgrl <<EOFarch1 connect internal as sysdba REM REM 备份 SYSTEM tablespace REM alter tablespace SYSTEM begin backup; !tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf alter tablespace SYSTEM end backup; REM REM The SYSTEM tablespace has now been written to a REM tar saveset on the tape device /dev/rmt/0hc. The REM rest of the tars must use the "-rvf" clause to append REM to that saveset. REM REM 备份 RBS tablespace REM alter tablespace RBS begin backup; !tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf alter tablespace RBS end backup; REM REM 备份 DATA tablespace REM For the purposes of this example, this tablespace REM will contain two files, data01.dbf and data02.dbf. REM The * wildcard will be used in the filename. REM alter tablespace DATA begin backup; !tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf alter tablespace DATA end backup; REM REM 备份 INDEXES tablespace REM alter tablespace INDEXES begin backup; !tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf alter tablespace INDEXES end backup; REM REM 备份 TEMP tablespace REM alter tablespace TEMP begin backup; !tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf alter tablespace TEMP end backup; REM REM Follow the same pattern to back up the rest REM of the tablespaces. REM REM REM Step 2. 备份归档日志文件. archive log stop REM REM Exit Server Manager, using the indicator set earlier. exit EOFarch1 # # Record which files are in the destination directory. # Do this by setting an environment variable that is # equal to the directory listing for the destination # directory. # For this example, the log_archive_dest is # /db01/oracle/arch/CC1. # FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES # # Now go back into Server Manager and restart the # archiving process. Set an indicator (called EOFarch2 # in this example). # svrmgrl <<EOFarch2 connect internal archive log start; exit EOFarch2 # # Now back up the archived redo logs to the tape # device via the "tar" command, then delete them # from the destination device via the "rm" command. # You may choose to compress them instead. # tar -rvf /dev/rmt/0hc $FILES rm -f $FILES # # Step 3. 备份控制文件到磁盘. # svrmgrl <<EOFarch3 connect internal alter database backup controlfile to ’db01/oracle/CC1/CC1controlfile.bck’; exit EOFarch3 # # 备份控制文件到磁带. # tar -rvf /dev/rmt/0hc /db01/oracle/CC1/CC1controlfile.bck # # End of hot backup script. //自动生成开始备份的脚本 set pagesize 0 feedback off select ’alter tablespace ’||Tablespace_Name||’ begin backup;’ from DBA_TABLESPACES where Status ’INVALID’ spool alter_begin.sql / spool off //自动生成备份结束的脚本 set pagesize 0 feedback off select ’alter tablespace ’||Tablespace_Name||’ end backup;’ from DBA_TABLESPACES where Status ’INVALID’ spool alter_end.sql / spool off //备份归档日志文件的脚本. REM See text for alternatives. # Step 1: Stop the archiving process. This will keep # additional archived redo log files from being written # to the destination directory during this process. # svrmgrl <<EOFarch1 connect internal as sysdba archive log stop; REM REM Exit Server Manager using the indicator set earlier. exit EOFarch1 # # Step 2: Record which files are in the destination # directory. # Do this by setting an environment variable that is # equal to the directory listing for the destination # directory. # For this example, the log_archive_dest is # /db01/oracle/arch/CC1. # FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES # # Step 3: Go back into Server Manager and restart the # archiving process. Set an indicator (called EOFarch2 # in this example). # svrmgrl <<EOFarch2 connect internal as sysdba archive log start; exit EOFarch2 # # Step 4. Back up the archived redo logs to the tape # device via the "tar" command, then delete them # from the destination device via the "rm" command. # tar -rvf /dev/rmt/0hc $FILES # # Step 5. Delete those files from the destination directory. # rm -f $FILES # # End of archived redo log file backup script. REM 磁盘到磁盘的备份 REM REM Back up the RBS tablespace - to another disk (UNIX) REM alter tablespace RBS begin backup; !cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups alter tablespace RBS end backup; REM REM 移动归档日志文件的shell脚本 # # Procedure for moving archived redo logs to another device # svrmgrl <<EOFarch2 connect internal as sysdba archive log stop; !mv /db01/oracle/arch/CC1 /db10/oracle/arch/CC1 archive log start; exit EOFarch2 # # end of archived redo log directory move. //生成创建控制文件命令 alter database backup controlfile to trace; //时间点恢复的例子 connect internal as sysdba startup mount instance_name; recover database until time ’1999-08-07:14:40:00’; //创建恢复目录 rman rcvcat rman/rman@ // 在(UNIX)下创建恢复目录 RMAN> create catalog tablespace rcvcat; // 在(NT)下创建恢复目录 RMAN> create catalog tablespace "RCVCAT"; //连接描述符范例 (DESCRIPTION= (ADDRESS= (PROTOCOL=TCP) (HOST=HQ) (PORT=1521)) (CONNECT DATA= (SID=loc))) // listener.ora 的条目entry // listener.ora 的条目entry LISTENER = (ADDRESS_LIST = (ADDRESS= (PROTOCOL=IPC) (KEY= loc.world) ) ) SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = loc) (ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1) ) ) // tnsnames.ora 的条目 LOC= (DESCRIPTION= (ADDRESS = (PROTOCOL = TCP) (HOST = HQ) (PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = loc) (INSTANCE_NAME = loc) ) ) //连接参数的设置(sql*net) LOC =(DESCRIPTION= (ADDRESS= (COMMUNITY=TCP.HQ.COMPANY) (PROTOCOL=TCP) (HOST=HQ) (PORT=1521)) (CONNECT DATA= (SID=loc))) //参数文件配置范例 // tnsnames.ora HQ =(DESCRIPTION= (ADDRESS= (PROTOCOL=TCP) (HOST=HQ) (PORT=1521)) (CONNECT DATA= (SID=loc))) // listener.ora LISTENER = (ADDRESS_LIST = (ADDRESS= (PROTOCOL=IPC) (KEY= loc) ) ) SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = loc) (ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1) ) ) // Oracle8I tnsnames.ora LOC= (DESCRIPTION= (ADDRESS = (PROTOCOL = TCP) (HOST = HQ) (PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = loc) (INSTANCE_NAME = loc) ) ) //使用 COPY 实现数据库之间的复制 copy from remote_username/remote_password@service_name to username/password@service_name [append|create|insert|replace] TABLE_NAME using subquery; REM COPY example set copycommit 1 set arraysize 1000 copy from HR/PUFFINSTUFF@loc - create EMPLOYEE - using - select * from EMPLOYEE //监视器的管理 lsnrctl start lsnrctl start my_lsnr lsnrctl status lsnrctl status hq 检查监视器的进程 ps -ef | grep tnslsnr //在 lsnrctl 内停止监视器 set password lsnr_password stop //在lsnrctl 内列出所有的服务 set password lsnr_password services //启动或停止一个NT的listener net start OracleTNSListener net stop OracleTNSListener // tnsnames.ora 文件的内容 fld1 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = server1.fld.com)(PORT = 1521)) ) (CONNECT_DATA = (SID = fld1) ) ) //操作系统网络的管理 telnet host_name ping host_name /etc/hosts 文件 130.110.238.109 nmhost 130.110.238.101 txhost 130.110.238.102 azhost arizona //oratab 表项 loc:/orasw/app/oracle/product/8.1.5.1:Y cc1:/orasw/app/oracle/product/8.1.5.1:N old:/orasw/app/oracle/product/8.1.5.0:Y //创建一个控制文件命令到跟踪文件 alter database backup controlfile to trace; //增加一个新的日志文件组的语句 connect internal as sysdba alter database add logfile group 4 (’/db01/oracle/CC1/log_1c.dbf’, ’/db02/oracle/CC1/log_2c.dbf’) size 5M; alter database add logfile member ’/db03/oracle/CC1/log_3c.dbf’ to group 4; //在Server Manager上MOUNT并打开一个数据库: connect internal as sysdba startup mount ORA1 exclusive; alter database open; //生成数据字典 @catalog @catproc //在init.ora 中备份数据库的位置 log_archive_dest_1 = ’/db00/arch’ log_archive_dest_state_1 = enable log_archive_dest_2 = "service=stby.world mandatory reopen=60" log_archive_dest_state_2 = enable //对用户的表空间的指定和管理相关的语句 create user USERNAME identified by PASSWORD default tablespace TABLESPACE_NAME; alter user USERNAME default tablespace TABLESPACE_NAME; alter user SYSTEM quota 0 on SYSTEM; alter user SYSTEM quota 50M on TOOLS; create user USERNAME identified by PASSWORD default tablespace DATA temporary tablespace TEMP; alter user USERNAME temporary tablespace TEMP; //重新指定一个数据文件的大小 : alter database datafile ’/db05/oracle/CC1/data01.dbf’ resize 200M; //创建一个自动扩展的数据文件: create tablespace DATA datafile ’/db05/oracle/CC1/data01.dbf’ size 200M autoextend ON next 10M maxsize 250M; //在表空间上增加一个自动扩展的数据文件: alter tablespace DATA add datafile ’/db05/oracle/CC1/data02.dbf’ size 50M autoextend ON maxsize 300M; //修改参数: alter database datafile ’/db05/oracle/CC1/data01.dbf’ autoextend ON maxsize 300M; //在数据文件移动期间重新命名: alter database rename file ’/db01/oracle/CC1/data01.dbf’ to ’/db02/oracle/CC1/data01.dbf’; alter tablespace DATA rename datafile ’/db01/oracle/CC1/data01.dbf’ to ’/db02/oracle/CC1/data01.dbf’; alter database rename file ’/db05/oracle/CC1/redo01CC1.dbf’ to ’/db02/oracle/CC1/redo01CC1.dbf’; alter database datafile ’/db05/oracle/CC1/data01.dbf’ resize 80M; //创建和使用角色: create role APPLICATION_USER; grant CREATE SESSION to APPLICATION_USER; grant APPLICATION_USER to username; //回滚段的管理 create rollback segment SEGMENT_NAME tablespace RBS; alter rollback segment SEGMENT_NAME offline; drop rollback segment SEGMENT_NAME; alter rollback segment SEGMENT_NAME online; //回滚段上指定事务 commit; set transaction use rollback segment ROLL_BATCH; insert into TABLE_NAME select * from DATA_LOAD_TABLE; commit; //查询回滚段的 大小和优化参数 select * from DBA_SEGMENTS where Segment_Type = ’ROLLBACK’; select N.Name, /* rollback segment name */ S.OptSize /* rollback segment OPTIMAL size */ from V$ROLLNAME N, V$ROLLSTAT S where N.USN=S.USN; //回收回滚段 alter rollback segment R1 shrink to 15M; alter rollback segment R1 shrink; //例子 set transaction use rollback segment SEGMENT_NAME alter tablespace RBS default storage (initial 125K next 125K minextents 18 maxextents 249) create rollback segment R4 tablespace RBS storage (optimal 2250K); alter rollback segment R4 online; select Sessions_Highwater from V$LICENSE; grant select on EMPLOYEE to PUBLIC; //用户和角色 create role ACCOUNT_CREATOR; grant CREATE SESSION, CREATE USER, ALTER USER to ACCOUNT_CREATOR; alter user THUMPER default role NONE; alter user THUMPER default role CONNECT; alter user THUMPER default role all except ACCOUNT_CREATOR; alter profile DEFAULT limit idle_time 60; create profile LIMITED_PROFILE limit FAILED_LOGIN_ATTEMPTS 5; create user JANE identified by EYRE profile LIMITED_PROFILE; grant CREATE SESSION to JANE; alter user JANE account unlock; alter user JANE account lock; alter profile LIMITED_PROFILE limit PASSWORD_LIFE_TIME 30; alter user jane password expire; //创建操作系统用户 REM Creating OPS$ accounts create user OPS$FARMER identified by SOME_PASSWORD default tablespace USERS temporary tablespace TEMP; REM Using identified externally create user OPS$FARMER identified externally default tablespace USERS temporary tablespace TEMP; //执行ORAPWD ORAPWD FILE=filename PASSWORD=password ENTRIES=max_users create role APPLICATION_USER; grant CREATE SESSION to APPLICATION_USER; create role DATA_ENTRY_CLERK; grant select, insert on THUMPER.EMPLOYEE to DATA_ENTRY_CLERK; grant select, insert on THUMPER.TIME_CARDS to DATA_ENTRY_CLERK; grant select, insert on THUMPER.DEPARTMENT to DATA_ENTRY_CLERK; grant APPLICATION_USER to DATA_ENTRY_CLERK; grant DATA_ENTRY_CLERK to MCGREGOR; grant DATA_ENTRY_CLERK to BPOTTER with admin option; //设置角色 set role DATA_ENTRY_CLERK; set role NONE; //回收权利: revoke delete on EMPLOYEE from PETER; revoke all on EMPLOYEE from MCGREGOR; //回收角色: revoke ACCOUNT_CREATOR from HELPDESK; drop user USERNAME cascade; grant SELECT on EMPLOYEE to MCGREGOR with grant option; grant SELECT on THUMPER.EMPLOYEE to BPOTTER with grant option; revoke SELECT on EMPLOYEE from MCGREGOR; create user MCGREGOR identified by VALUES ’1A2DD3CCEE354DFA’; alter user OPS$FARMER identified by VALUES ’no way’; //备份与恢复 使用 export 程序 exp system/manager file=expdat.dmp compress=Y owner=(HR,THUMPER) exp system/manager file=hr.dmp owner=HR indexes=Y compress=Y imp system/manager file=hr.dmp full=Y buffer=64000 commit=Y //备份表 exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES) //备份分区 exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES:Part1) //输入例子 imp system/manager file=expdat.dmp imp system/manager file=expdat.dmp buffer=64000 commit=Y exp system/manager file=thumper.dat owner=thumper grants=N indexes=Y compress=Y rows=Y imp system/manager file=thumper.dat FROMUSER=thumper TOUSER=flower rows=Y indexes=Y imp system/manager file=expdat.dmp full=Y commit=Y buffer=64000 imp system/manager file=expdat.dmp ignore=N rows=N commit=Y buffer=64000 //使用操作系统备份命令 REM TAR examples tar -cvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 tar -rvf /dev/rmt/0hc /orasw/app/oracle/CC1/pfile/initcc1.ora tar -rvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 /orasw/app/oracle/CC1/pfile/initcc1.ora //离线备份的shell脚本 ORACLE_SID=cc1; export ORACLE_SID ORAENV_ASK=NO; export ORAENV_ASK . oraenv svrmgrl <<EOF1 connect internal as sysdba shutdown immediate; exit EOF1 insert backup commands like the "tar" commands here svrmgrl <<EOF2 connect internal as sysdba startup EOF2 //在Server Manager上设置为archivelog mode: connect internal as sysdba startup mount cc1; alter database archivelog; archive log start; alter database open; //在Server Manager上设置为archivelog mode: connect internal as sysdba startup mount cc1; alter database noarchivelog; alter database open; select Name, Value from V$PARAMETER where Name like ’log_archive%’; //联机备份的脚本 # # Sample Hot Backup Script for a UNIX File System database # # Set up environment variables: ORACLE_SID=cc1; export ORACLE_SID ORAENV_ASK=NO; export ORAENV_ASK . oraenv svrmgrl <<EOFarch1 connect internal as sysdba REM REM 备份 SYSTEM tablespace REM alter tablespace SYSTEM begin backup; !tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf alter tablespace SYSTEM end backup; REM REM The SYSTEM tablespace has now been written to a REM tar saveset on the tape device /dev/rmt/0hc. The REM rest of the tars must use the "-rvf" clause to append REM to that saveset. REM REM 备份 RBS tablespace REM alter tablespace RBS begin backup; !tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf alter tablespace RBS end backup; REM REM 备份 DATA tablespace REM For the purposes of this example, this tablespace REM will contain two files, data01.dbf and data02.dbf. REM The * wildcard will be used in the filename. REM alter tablespace DATA begin backup; !tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf alter tablespace DATA end backup; REM REM 备份 INDEXES tablespace REM alter tablespace INDEXES begin backup; !tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf alter tablespace INDEXES end backup; REM REM 备份 TEMP tablespace REM alter tablespace TEMP begin backup; !tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf alter tablespace TEMP end backup; REM REM Follow the same pattern to back up the rest REM of the tablespaces. REM REM REM Step 2. 备份归档日志文件. archive log stop REM REM Exit Server Manager, using the indicator set earlier. exit EOFarch1 # # Record which files are in the destination directory. # Do this by setting an environment variable that is # equal to the directory listing for the destination # directory. # For this example, the log_archive_dest is # /db01/oracle/arch/CC1. # FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES # # Now go back into Server Manager and restart the # archiving process. Set an indicator (called EOFarch2 # in this example). # svrmgrl <<EOFarch2 connect internal archive log start; exit EOFarch2 # # Now back up the archived redo logs to the tape # device via the "tar" command, then delete them # from the destination device via the "rm" command. # You may choose to compress them instead. # tar -rvf /dev/rmt/0hc $FILES rm -f $FILES # # Step 3. 备份控制文件到磁盘. # svrmgrl <<EOFarch3 connect internal alter database backup controlfile to ’db01/oracle/CC1/CC1controlfile.bck’; exit EOFarch3 # # 备份控制文件到磁带. # tar -rvf /dev/rmt/0hc /db01/oracle/CC1/CC1controlfile.bck # # End of hot backup script. //自动生成开始备份的脚本 set pagesize 0 feedback off select ’alter tablespace ’||Tablespace_Name||’ begin backup;’ from DBA_TABLESPACES where Status ’INVALID’ spool alter_begin.sql / spool off //自动生成备份结束的脚本 set pagesize 0 feedback off select ’alter tablespace ’||Tablespace_Name||’ end backup;’ from DBA_TABLESPACES where Status ’INVALID’ spool alter_end.sql / spool off //备份归档日志文件的脚本. REM See text for alternatives. # Step 1: Stop the archiving process. This will keep # additional archived redo log files from being written # to the destination directory during this process. # svrmgrl <<EOFarch1 connect internal as sysdba archive log stop; REM REM Exit Server Manager using the indicator set earlier. exit EOFarch1 # # Step 2: Record which files are in the destination # directory. # Do this by setting an environment variable that is # equal to the directory listing for the destination # directory. # For this example, the log_archive_dest is # /db01/oracle/arch/CC1. # FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES # # Step 3: Go back into Server Manager and restart the # archiving process. Set an indicator (called EOFarch2 # in this example). # svrmgrl <<EOFarch2 connect internal as sysdba archive log start; exit EOFarch2 # # Step 4. Back up the archived redo logs to the tape # device via the "tar" command, then delete them # from the destination device via the "rm" command. # tar -rvf /dev/rmt/0hc $FILES # # Step 5. Delete those files from the destination directory. # rm -f $FILES # # End of archived redo log file backup script. REM 磁盘到磁盘的备份 REM REM Back up the RBS tablespace - to another disk (UNIX) REM alter tablespace RBS begin backup; !cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups alter tablespace RBS end backup; REM REM 移动归档日志文件的shell脚本 # # Procedure for moving archived redo logs to another device # svrmgrl <<EOFarch2 connect internal as sysdba archive log stop; !mv /db01/oracle/arch/CC1 /db10/oracle/arch/CC1 archive log start; exit EOFarch2 # # end of archived redo log directory move. //生成创建控制文件命令 alter database backup controlfile to trace; //时间点恢复的例子 connect internal as sysdba startup mount instance_name; recover database until time ’1999-08-07:14:40:00’; //创建恢复目录 rman rcvcat rman/rman@ // 在(UNIX)下创建恢复目录 RMAN> create catalog tablespace rcvcat; // 在(NT)下创建恢复目录 RMAN> create catalog tablespace "RCVCAT"; //连接描述符范例 (DESCRIPTION= (ADDRESS= (PROTOCOL=TCP) (HOST=HQ) (PORT=1521)) (CONNECT DATA= (SID=loc))) // listener.ora 的条目entry //创建一个控制文件命令到跟踪文件 alter database backup controlfile to trace; //增加一个新的日志文件组的语句 connect internal as sysdba alter database add logfile group 4 (’/db01/oracle/CC1/log_1c.dbf’, ’/db02/oracle/CC1/log_2c.dbf’) size 5M; alter database add logfile member ’/db03/oracle/CC1/log_3c.dbf’ to group 4; //在Server Manager上MOUNT并打开一个数据库: connect internal as sysdba startup mount ORA1 exclusive; alter database open; //生成数据字典 @catalog @catproc //在init.ora 中备份数据库的位置 log_archive_dest_1 = ’/db00/arch’ log_archive_dest_state_1 = enable log_archive_dest_2 = "service=stby.world mandatory reopen=60" log_archive_dest_state_2 = enable //对用户的表空间的指定和管理相关的语句 create user USERNAME identified by PASSWORD default tablespace TABLESPACE_NAME; alter user USERNAME default tablespace TABLESPACE_NAME; alter user SYSTEM quota 0 on SYSTEM; alter user SYSTEM quota 50M on TOOLS; create user USERNAME identified by PASSWORD default tablespace DATA temporary tablespace TEMP; alter user USERNAME temporary tablespace TEMP; //重新指定一个数据文件的大小 : alter database datafile ’/db05/oracle/CC1/data01.dbf’ resize 200M; //创建一个自动扩展的数据文件: create tablespace DATA datafile ’/db05/oracle/CC1/data01.dbf’ size 200M autoextend ON next 10M maxsize 250M; //在表空间上增加一个自动扩展的数据文件: alter tablespace DATA add datafile ’/db05/oracle/CC1/data02.dbf’ size 50M autoextend ON maxsize 300M; //修改参数: alter database datafile ’/db05/oracle/CC1/data01.dbf’ autoextend ON maxsize 300M; //在数据文件移动期间重新命名: alter database rename file ’/db01/oracle/CC1/data01.dbf’ to ’/db02/oracle/CC1/data01.dbf’; alter tablespace DATA rename datafile ’/db01/oracle/CC1/data01.dbf’ to ’/db02/oracle/CC1/data01.dbf’; alter database rename file ’/db05/oracle/CC1/redo01CC1.dbf’ to ’/db02/oracle/CC1/redo01CC1.dbf’; alter database datafile ’/db05/oracle/CC1/data01.dbf’ resize 80M; //创建和使用角色: create role APPLICATION_USER; grant CREATE SESSION to APPLICATION_USER; grant APPLICATION_USER to username; //回滚段的管理 create rollback segment SEGMENT_NAME tablespace RBS; alter rollback segment SEGMENT_NAME offline; drop rollback segment SEGMENT_NAME; alter rollback segment SEGMENT_NAME online; //回滚段上指定事务 commit; set transaction use rollback segment ROLL_BATCH; insert into TABLE_NAME select * from DATA_LOAD_TABLE; commit; //查询回滚段的 大小和优化参数 select * from DBA_SEGMENTS where Segment_Type = ’ROLLBACK’; select N.Name, /* rollback segment name */ S.OptSize /* rollback segment OPTIMAL size */ from V$ROLLNAME N, V$ROLLSTAT S where N.USN=S.USN; //回收回滚段 alter rollback segment R1 shrink to 15M; alter rollback segment R1 shrink; //例子 set transaction use rollback segment SEGMENT_NAME alter tablespace RBS default storage (initial 125K next 125K minextents 18 maxextents 249) create rollback segment R4 tablespace RBS storage (optimal 2250K); alter rollback segment R4 online; select Sessions_Highwater from V$LICENSE; grant select on EMPLOYEE to PUBLIC; //用户和角色 create role ACCOUNT_CREATOR; grant CREATE SESSION, CREATE USER, ALTER USER to ACCOUNT_CREATOR; alter user THUMPER default role NONE; alter user THUMPER default role CONNECT; alter user THUMPER default role all except ACCOUNT_CREATOR; alter profile DEFAULT limit idle_time 60; create profile LIMITED_PROFILE limit FAILED_LOGIN_ATTEMPTS 5; create user JANE identified by EYRE profile LIMITED_PROFILE; grant CREATE SESSION to JANE; alter user JANE account unlock; alter user JANE account lock; alter profile LIMITED_PROFILE limit PASSWORD_LIFE_TIME 30; alter user jane password expire; //创建操作系统用户 REM Creating OPS$ accounts create user OPS$FARMER identified by SOME_PASSWORD default tablespace USERS temporary tablespace TEMP; REM Using identified externally create user OPS$FARMER identified externally default tablespace USERS temporary tablespace TEMP; //执行ORAPWD ORAPWD FILE=filename PASSWORD=password ENTRIES=max_users create role APPLICATION_USER; grant CREATE SESSION to APPLICATION_USER; create role DATA_ENTRY_CLERK; grant select, insert on THUMPER.EMPLOYEE to DATA_ENTRY_CLERK; grant select, insert on THUMPER.TIME_CARDS to DATA_ENTRY_CLERK; grant select, insert on THUMPER.DEPARTMENT to DATA_ENTRY_CLERK; grant APPLICATION_USER to DATA_ENTRY_CLERK; grant DATA_ENTRY_CLERK to MCGREGOR; grant DATA_ENTRY_CLERK to BPOTTER with admin option; //设置角色 set role DATA_ENTRY_CLERK; set role NONE; //回收权利: revoke delete on EMPLOYEE from PETER; revoke all on EMPLOYEE from MCGREGOR; //回收角色: revoke ACCOUNT_CREATOR from HELPDESK; drop user USERNAME cascade; grant SELECT on EMPLOYEE to MCGREGOR with grant option; grant SELECT on THUMPER.EMPLOYEE to BPOTTER with grant option; revoke SELECT on EMPLOYEE from MCGREGOR; create user MCGREGOR identified by VALUES ’1A2DD3CCEE354DFA’; alter user OPS$FARMER identified by VALUES ’no way’; //备份与恢复 使用 export 程序 exp system/manager file=expdat.dmp compress=Y owner=(HR,THUMPER) exp system/manager file=hr.dmp owner=HR indexes=Y compress=Y imp system/manager file=hr.dmp full=Y buffer=64000 commit=Y //备份表 exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES) //备份分区 exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES:Part1) //输入例子 imp system/manager file=expdat.dmp imp system/manager file=expdat.dmp buffer=64000 commit=Y exp system/manager file=thumper.dat owner=thumper grants=N indexes=Y compress=Y rows=Y imp system/manager file=thumper.dat FROMUSER=thumper TOUSER=flower rows=Y indexes=Y imp system/manager file=expdat.dmp full=Y commit=Y buffer=64000 imp system/manager file=expdat.dmp ignore=N rows=N commit=Y buffer=64000 //使用操作系统备份命令 REM TAR examples tar -cvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 tar -rvf /dev/rmt/0hc /orasw/app/oracle/CC1/pfile/initcc1.ora tar -rvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 /orasw/app/oracle/CC1/pfile/initcc1.ora //离线备份的shell脚本 ORACLE_SID=cc1; export ORACLE_SID ORAENV_ASK=NO; export ORAENV_ASK . oraenv svrmgrl <<EOF1 connect internal as sysdba shutdown immediate; exit EOF1 insert backup commands like the "tar" commands here svrmgrl <<EOF2 connect internal as sysdba startup EOF2 //在Server Manager上设置为archivelog mode: connect internal as sysdba startup mount cc1; alter database archivelog; archive log start; alter database open; //在Server Manager上设置为archivelog mode: connect internal as sysdba startup mount cc1; alter database noarchivelog; alter database open; select Name, Value from V$PARAMETER where Name like ’log_archive%’; //联机备份的脚本 # # Sample Hot Backup Script for a UNIX File System database # # Set up environment variables: ORACLE_SID=cc1; export ORACLE_SID ORAENV_ASK=NO; export ORAENV_ASK . oraenv svrmgrl <<EOFarch1 connect internal as sysdba REM REM 备份 SYSTEM tablespace REM alter tablespace SYSTEM begin backup; !tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf alter tablespace SYSTEM end backup; REM REM The SYSTEM tablespace has now been written to a REM tar saveset on the tape device /dev/rmt/0hc. The REM rest of the tars must use the "-rvf" clause to append REM to that saveset. REM REM 备份 RBS tablespace REM alter tablespace RBS begin backup; !tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf alter tablespace RBS end backup; REM REM 备份 DATA tablespace REM For the purposes of this example, this tablespace REM will contain two files, data01.dbf and data02.dbf. REM The * wildcard will be used in the filename. REM alter tablespace DATA begin backup; !tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf alter tablespace DATA end backup; REM REM 备份 INDEXES tablespace REM alter tablespace INDEXES begin backup; !tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf alter tablespace INDEXES end backup; REM REM 备份 TEMP tablespace REM alter tablespace TEMP begin backup; !tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf alter tablespace TEMP end backup; REM REM Follow the same pattern to back up the rest REM of the tablespaces. REM REM REM Step 2. 备份归档日志文件. archive log stop REM REM Exit Server Manager, using the indicator set earlier. exit EOFarch1 # # Record which files are in the destination directory. # Do this by setting an environment variable that is # equal to the directory listing for the destination # directory. # For this example, the log_archive_dest is # /db01/oracle/arch/CC1. # FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES # # Now go back into Server Manager and restart the # archiving process. Set an indicator (called EOFarch2 # in this example). # svrmgrl <<EOFarch2 connect internal archive log start; exit EOFarch2 # # Now back up the archived redo logs to the tape # device via the "tar" command, then delete them # from the destination device via the "rm" command. # You may choose to compress them instead. # tar -rvf /dev/rmt/0hc $FILES rm -f $FILES # # Step 3. 备份控制文件到磁盘. # svrmgrl <<EOFarch3 connect internal alter database backup controlfile to ’db01/oracle/CC1/CC1controlfile.bck’; exit EOFarch3 # # 备份控制文件到磁带. # tar -rvf /dev/rmt/0hc /db01/oracle/CC1/CC1controlfile.bck # # End of hot backup script. //自动生成开始备份的脚本 set pagesize 0 feedback off select ’alter tablespace ’||Tablespace_Name||’ begin backup;’ from DBA_TABLESPACES where Status ’INVALID’ spool alter_begin.sql / spool off //自动生成备份结束的脚本 set pagesize 0 feedback off select ’alter tablespace ’||Tablespace_Name||’ end backup;’ from DBA_TABLESPACES where Status ’INVALID’ spool alter_end.sql / spool off //备份归档日志文件的脚本. REM See text for alternatives. # Step 1: Stop the archiving process. This will keep # additional archived redo log files from being written # to the destination directory during this process. # svrmgrl <<EOFarch1 connect internal as sysdba archive log stop; REM REM Exit Server Manager using the indicator set earlier. exit EOFarch1 # # Step 2: Record which files are in the destination # directory. # Do this by setting an environment variable that is # equal to the directory listing for the destination # directory. # For this example, the log_archive_dest is # /db01/oracle/arch/CC1. # FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES # # Step 3: Go back into Server Manager and restart the # archiving process. Set an indicator (called EOFarch2 # in this example). # svrmgrl <<EOFarch2 connect internal as sysdba archive log start; exit EOFarch2 # # Step 4. Back up the archived redo logs to the tape # device via the "tar" command, then delete them # from the destination device via the "rm" command. # tar -rvf /dev/rmt/0hc $FILES # # Step 5. Delete those files from the destination directory. # rm -f $FILES # # End of archived redo log file backup script. REM 磁盘到磁盘的备份 REM REM Back up the RBS tablespace - to another disk (UNIX) REM alter tablespace RBS begin backup; !cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups alter tablespace RBS end backup; REM REM 移动归档日志文件的shell脚本 # # Procedure for moving archived redo logs to another device # svrmgrl <<EOFarch2 connect internal as sysdba archive log stop; !mv /db01/oracle/arch/CC1 /db10/oracle/arch/CC1 archive log start; exit EOFarch2 # # end of archived redo log directory move. //生成创建控制文件命令 alter database backup controlfile to trace; //时间点恢复的例子 connect internal as sysdba startup mount instance_name; recover database until time ’1999-08-07:14:40:00’; //创建恢复目录 rman rcvcat rman/rman@ // 在(UNIX)下创建恢复目录 RMAN> create catalog tablespace rcvcat; // 在(NT)下创建恢复目录 RMAN> create catalog tablespace "RCVCAT"; //连接描述符范例 (DESCRIPTION= (ADDRESS= (PROTOCOL=TCP) (HOST=HQ) (PORT=1521)) (CONNECT DATA= (SID=loc))) // listener.ora 的条目entry ……………………………………………………………………………………
======================= MessagePack for Python ======================= :author: INADA Naoki :version: 0.4.1 :date: 2014-02-17 .. image:: https://secure.travis-ci.org/msgpack/msgpack-python.png :target: https://travis-ci.org/#!/msgpack/msgpack-python What's this ------------ `MessagePack <http://msgpack.org/>`_ is a fast, compact binary serialization format, suitable for similar data to JSON. This package provides CPython bindings for reading and writing MessagePack data. Install --------- You can use ``pip`` or ``easy_install`` to install msgpack:: $ easy_install msgpack-python or $ pip install msgpack-python PyPy ^^^^^ msgpack-python provides pure python implementation. PyPy can use this. Windows ^^^^^^^ When you can't use binary distribution, you need to install Visual Studio or Windows SDK on Windows. (NOTE: Visual C++ Express 2010 doesn't support amd64. Windows SDK is recommanded way to build amd64 msgpack without any fee.) Without extension, using pure python implementation on CPython runs slowly. Notes ----- Note for msgpack 2.0 support ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ msgpack 2.0 adds two types: *bin* and *ext*. *raw* was bytes or string type like Python 2's ``str``. To distinguish string and bytes, msgpack 2.0 adds *bin*. It is non-string binary like Python 3's ``bytes``. To use *bin* type for packing ``bytes``, pass ``use_bin_type=True`` to packer argument. >>> import msgpack >>> packed = msgpack.packb([b'spam', u'egg'], use_bin_type=True) >>> msgpack.unpackb(packed, encoding='utf-8') ['spam', u'egg'] You shoud use it carefully. When you use ``use_bin_type=True``, packed binary can be unpacked by unpackers supporting msgpack-2.0. To use *ext* type, pass ``msgpack.ExtType`` object to packer. >>> import msgpack >>> packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy')) >>> msgpack.unpackb(packed) ExtType(code=42, data='xyzzy') You can use it with ``default`` and ``ext_hook``. See below. Note for msgpack 0.2.x users ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The msgpack 0.3 have some incompatible changes. The default value of ``use_list`` keyword argument is ``True`` from 0.3. You should pass the argument explicitly for backward compatibility. `Unpacker.unpack()` and some unpack methods now raises `OutOfData` instead of `StopIteration`. `StopIteration` is used for iterator protocol only. How to use ----------- One-shot pack & unpack ^^^^^^^^^^^^^^^^^^^^^^ Use ``packb`` for packing and ``unpackb`` for unpacking. msgpack provides ``dumps`` and ``loads`` as alias for compatibility with ``json`` and ``pickle``. ``pack`` and ``dump`` packs to file-like object. ``unpack`` and ``load`` unpacks from file-like object. :: >>> import msgpack >>> msgpack.packb([1, 2, 3]) '\x93\x01\x02\x03' >>> msgpack.unpackb(_) [1, 2, 3] ``unpack`` unpacks msgpack's array to Python's list, but can unpack to tuple:: >>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False) (1, 2, 3) You should always pass the ``use_list`` keyword argument. See performance issues relating to use_list_ below. Read the docstring for other options. Streaming unpacking ^^^^^^^^^^^^^^^^^^^ ``Unpacker`` is a "streaming unpacker". It unpacks multiple objects from one stream (or from bytes provided through its ``feed`` method). :: import msgpack from io import BytesIO buf = BytesIO() for i in range(100): buf.write(msgpack.packb(range(i))) buf.seek(0) unpacker = msgpack.Unpacker(buf) for unpacked in unpacker: print unpacked Packing/unpacking of custom data type ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ It is also possible to pack/unpack custom data types. Here is an example for ``datetime.datetime``. :: import datetime import msgpack useful_dict = { "id": 1, "created": datetime.datetime.now(), } def decode_datetime(obj): if b'__datetime__' in obj: obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f") return obj def encode_datetime(obj): if isinstance(obj, datetime.datetime): return {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f")} return obj packed_dict = msgpack.packb(useful_dict, default=encode_datetime) this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime) ``Unpacker``'s ``object_hook`` callback receives a dict; the ``object_pairs_hook`` callback may instead be used to receive a list of key-value pairs. Extended types ^^^^^^^^^^^^^^^ It is also possible to pack/unpack custom data types using the msgpack 2.0 feature. >>> import msgpack >>> import array >>> def default(obj): ... if isinstance(obj, array.array) and obj.typecode == 'd': ... return msgpack.ExtType(42, obj.tostring()) ... raise TypeError("Unknown type: %r" % (obj,)) ... >>> def ext_hook(code, data): ... if code == 42: ... a = array.array('d') ... a.fromstring(data) ... return a ... return ExtType(code, data) ... >>> data = array.array('d', [1.2, 3.4]) >>> packed = msgpack.packb(data, default=default) >>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook) >>> data == unpacked True Advanced unpacking control ^^^^^^^^^^^^^^^^^^^^^^^^^^ As an alternative to iteration, ``Unpacker`` objects provide ``unpack``, ``skip``, ``read_array_header`` and ``read_map_header`` methods. The former two read an entire message from the stream, respectively deserialising and returning the result, or ignoring it. The latter two methods return the number of elements in the upcoming container, so that each element in an array, or key-value pair in a map, can be unpacked or skipped individually. Each of these methods may optionally write the packed data it reads to a callback function: :: from io import BytesIO def distribute(unpacker, get_worker): nelems = unpacker.read_map_header() for i in range(nelems): # Select a worker for the given key key = unpacker.unpack() worker = get_worker(key) # Send the value as a packed message to worker bytestream = BytesIO() unpacker.skip(bytestream.write) worker.send(bytestream.getvalue()) Note about performance ------------------------ GC ^^ CPython's GC starts when growing allocated object. This means unpacking may cause useless GC. You can use ``gc.disable()`` when unpacking large message. `use_list` option ^^^^^^^^^^^^^^^^^^ List is the default sequence type of Python. But tuple is lighter than list. You can use ``use_list=False`` while unpacking when performance is important. Python's dict can't use list as key and MessagePack allows array for key of mapping. ``use_list=False`` allows unpacking such message. Another way to unpacking such object is using ``object_pairs_hook``. Test ---- MessagePack uses `pytest` for testing. Run test with following command: $ py.test .. vim: filetype=rst

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值