download.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. from fate_arch import storage
  18. from fate_flow.manager.data_manager import TableStorage
  19. from fate_flow.utils.log_utils import getLogger
  20. from fate_arch.storage import DEFAULT_ID_DELIMITER
  21. from fate_flow.components._base import (
  22. BaseParam,
  23. ComponentBase,
  24. ComponentMeta,
  25. ComponentInputProtocol,
  26. )
  27. from fate_flow.entity import Metric, MetricMeta
  28. LOGGER = getLogger()
  29. download_cpn_meta = ComponentMeta("Download")
  30. @download_cpn_meta.bind_param
  31. class DownloadParam(BaseParam):
  32. def __init__(
  33. self,
  34. output_path="",
  35. delimiter=DEFAULT_ID_DELIMITER,
  36. namespace="",
  37. name="",
  38. ):
  39. self.output_path = output_path
  40. self.delimiter = delimiter
  41. self.namespace = namespace
  42. self.name = name
  43. def check(self):
  44. return True
  45. @download_cpn_meta.bind_runner.on_local
  46. class Download(ComponentBase):
  47. def __init__(self):
  48. super(Download, self).__init__()
  49. self.parameters = {}
  50. def _run(self, cpn_input: ComponentInputProtocol):
  51. self.parameters = cpn_input.parameters
  52. self.parameters["role"] = cpn_input.roles["role"]
  53. self.parameters["local"] = cpn_input.roles["local"]
  54. data_table_meta = storage.StorageTableMeta(name=self.parameters.get("name"), namespace=self.parameters.get("namespace"))
  55. TableStorage.send_table(
  56. output_tables_meta={"table": data_table_meta},
  57. output_data_file_path = os.path.abspath(self.parameters["output_path"]),
  58. local_download=True
  59. )
  60. self.callback_metric(
  61. metric_name="data_access",
  62. metric_namespace="download",
  63. metric_data=[Metric("count", data_table_meta.count)]
  64. )
  65. LOGGER.info("===== export {} lines totally =====".format(data_table_meta.count))
  66. LOGGER.info("===== export data finish =====")
  67. LOGGER.info(
  68. "===== export data file path:{} =====".format(
  69. os.path.abspath(self.parameters["output_path"])
  70. )
  71. )
  72. def callback_metric(self, metric_name, metric_namespace, metric_data):
  73. self.tracker.log_metric_data(
  74. metric_name=metric_name,
  75. metric_namespace=metric_namespace,
  76. metrics=metric_data,
  77. )
  78. self.tracker.set_metric_meta(
  79. metric_namespace,
  80. metric_name,
  81. MetricMeta(name="download", metric_type="DOWNLOAD"),
  82. )