-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·215 lines (169 loc) · 6.1 KB
/
main.py
File metadata and controls
executable file
·215 lines (169 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
"""
TableStore Data Exporter - Main CLI Entry Point
A general-purpose exporter for Aliyun TableStore with declarative configuration.
"""
import argparse
import sys
from typing import Optional
from config.manager import ConfigManager
from exporter.core import TableStoreExporter
from utils.logger import setup_logger, get_logger
def parse_args() -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(
description='Export data from Aliyun TableStore to CSV files',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Basic export
python main.py --config=export_config.json
# With custom threads and resume
python main.py --config=export_config.json --threads=8 --resume
# Override connection config
python main.py --config=export_config.json --connection=prod.json
# Dry run to validate configuration
python main.py --config=export_config.json --dry-run
# Verbose logging
python main.py --config=export_config.json --verbose
For more information, see README.md
"""
)
# Required arguments
parser.add_argument(
'--config',
required=True,
help='Path to export configuration file (required)'
)
# Optional arguments
parser.add_argument(
'--connection',
default='config/connection.json',
help='Path to connection configuration file (default: config/connection.json)'
)
parser.add_argument(
'--threads',
type=int,
default=4,
help='Number of concurrent worker threads (default: 4)'
)
parser.add_argument(
'--resume',
action='store_true',
help='Resume from checkpoint if available'
)
parser.add_argument(
'--output-dir',
help='Override output directory from config'
)
parser.add_argument(
'--progress-file',
default='.export_progress.json',
help='Path to progress checkpoint file (default: .export_progress.json)'
)
parser.add_argument(
'--dry-run',
action='store_true',
help='Validate configuration without executing export'
)
parser.add_argument(
'--verbose',
action='store_true',
help='Enable verbose (debug) logging'
)
parser.add_argument(
'--no-progress-bar',
action='store_true',
help='Disable progress bars'
)
return parser.parse_args()
def validate_config(config_manager: ConfigManager) -> bool:
"""
Validate configuration.
Args:
config_manager: Configuration manager
Returns:
True if valid, False otherwise
"""
logger = get_logger()
try:
# Validation happens in ConfigManager.__init__
logger.info("✓ Export configuration is valid")
logger.info("✓ Connection configuration is valid")
# Print config summary
logger.info("=" * 80)
logger.info("Configuration Summary")
logger.info("=" * 80)
logger.info(f"Table: {config_manager.get_table_name()}")
logger.info(f"Partition key: {config_manager.get_schema()['partition_key']}")
logger.info(f"Sort key: {config_manager.get_schema()['sort_key']}")
logger.info(f"Append columns: {config_manager.get_append_columns()}")
logger.info(f"Output directory: {config_manager.get_output_directory()}")
logger.info("=" * 80)
return True
except Exception as e:
logger.error(f"✗ Configuration validation failed: {e}")
return False
def main() -> int:
"""Main entry point."""
# Parse arguments
args = parse_args()
# Setup logging
logger = setup_logger(verbose=args.verbose)
logger.info("=" * 80)
logger.info("TableStore Data Exporter")
logger.info("=" * 80)
try:
# Load configuration
logger.info(f"Loading configuration from: {args.config}")
config_manager = ConfigManager(
config_path=args.config,
connection_path=args.connection
)
# Override output directory if specified
if args.output_dir:
config_manager.export_config.output['directory'] = args.output_dir
logger.info(f"Output directory overridden: {args.output_dir}")
# Validate configuration
if not validate_config(config_manager):
return 1
# Dry run mode
if args.dry_run:
logger.info("=" * 80)
logger.info("Dry run completed successfully!")
logger.info("Configuration is valid and ready for export.")
logger.info("=" * 80)
return 0
# Create exporter
logger.info("Initializing exporter...")
exporter = TableStoreExporter(
config_manager=config_manager,
max_workers=args.threads
)
# Run export
logger.info("Starting export...")
logger.info("=" * 80)
summary = exporter.export_all_tasks(resume=args.resume)
# Print final summary
logger.info("=" * 80)
logger.info("Export completed successfully!")
logger.info(f"Total tasks: {summary['total_tasks']}")
logger.info(f"Completed: {summary['completed']}")
logger.info(f"Failed: {summary['failed']}")
logger.info(f"Total rows exported: {summary['total_rows_exported']:,}")
logger.info(f"Duration: {summary['duration']:.2f}s")
logger.info("=" * 80)
# Return non-zero if any failures
return 0 if summary['failed'] == 0 else 1
except KeyboardInterrupt:
logger.warning("\n\nExport interrupted by user!")
logger.info("Progress has been saved. Use --resume to continue.")
return 130 # Standard exit code for Ctrl+C
except Exception as e:
logger.error(f"Fatal error: {e}")
if args.verbose:
import traceback
traceback.print_exc()
return 1
if __name__ == '__main__':
sys.exit(main())