2222"""
2323
2424import sys
25+ from pypaimon .common .json_util import JSON
2526
2627
2728def cmd_table_read (args ):
@@ -85,7 +86,7 @@ def cmd_table_get(args):
8586 """
8687 Execute the 'table get' command.
8788
88- Gets and displays table schema information.
89+ Gets and displays table schema information in JSON format .
8990
9091 Args:
9192 args: Parsed command line arguments.
@@ -116,52 +117,9 @@ def cmd_table_get(args):
116117 print (f"Error: Failed to get table '{ table_identifier } ': { e } " , file = sys .stderr )
117118 sys .exit (1 )
118119
119- # Get table schema
120- schema = table .table_schema
121-
122- # Display table information
123- print ("=" * 80 )
124- print (f"Table: { database_name } .{ table_name } " )
125- print ("=" * 80 )
126-
127- # Display schema ID
128- print (f"\n Schema ID: { schema .id } " )
129-
130- # Display comment if exists
131- if schema .comment :
132- print (f"\n Comment: { schema .comment } " )
133-
134- # Display fields
135- print (f"\n { '=' * 80 } " )
136- print ("Fields:" )
137- print (f"{ '=' * 80 } " )
138- print (f"{ 'ID' :<5} { 'Name' :<18} { 'Type' :<20} { 'Nullable' :<9} { 'Description' } " )
139- print (f"{ '-' * 5 } { '-' * 18 } { '-' * 20 } { '-' * 9 } { '-' * 26 } " )
140-
141- for field in schema .fields :
142- nullable = "YES" if field .type .nullable else "NO"
143- description = field .description or ""
144- print (f"{ field .id :<5} { field .name :<18} { str (field .type ):<20} { nullable :<9} { description } " )
145-
146- # Display partition keys
147- if schema .partition_keys :
148- print (f"\n { '=' * 80 } " )
149- print (f"Partition Keys: { ', ' .join (schema .partition_keys )} " )
150-
151- # Display primary keys
152- if schema .primary_keys :
153- print (f"\n { '=' * 80 } " )
154- print (f"Primary Keys: { ', ' .join (schema .primary_keys )} " )
155-
156- # Display options
157- if schema .options :
158- print (f"\n { '=' * 80 } " )
159- print ("Table Options:" )
160- print (f"{ '=' * 80 } " )
161- for key , value in sorted (schema .options .items ()):
162- print (f" { key :<40} = { value } " )
163-
164- print (f"\n { '=' * 80 } \n " )
120+ # Get table schema and convert to Schema, then output as JSON
121+ schema = table .table_schema .to_schema ()
122+ print (JSON .to_json (schema , indent = 2 ))
165123
166124
167125def cmd_table_create (args ):
@@ -174,7 +132,6 @@ def cmd_table_create(args):
174132 args: Parsed command line arguments.
175133 """
176134 import json
177- import yaml
178135 from pypaimon .cli .cli import load_catalog_config , create_catalog
179136 from pypaimon import Schema
180137
@@ -195,72 +152,29 @@ def cmd_table_create(args):
195152
196153 database_name , table_name = parts
197154
198- # Load schema from file
199- schema_file = args .schema_file
155+ # Load schema from JSON file
156+ schema_file = args .schema
200157 if not schema_file :
201- print ("Error: Schema file is required. Use --schema-file option." , file = sys .stderr )
158+ print ("Error: Schema is required. Use --schema option." , file = sys .stderr )
202159 sys .exit (1 )
203160
204161 try :
205162 with open (schema_file , 'r' , encoding = 'utf-8' ) as f :
206- if schema_file .endswith ('.json' ):
207- schema_def = json .load (f )
208- elif schema_file .endswith ('.yaml' ) or schema_file .endswith ('.yml' ):
209- schema_def = yaml .safe_load (f )
210- else :
211- print ("Error: Unsupported schema file format. Use .json, .yaml, or .yml" , file = sys .stderr )
212- sys .exit (1 )
163+ schema_json = f .read ()
164+ paimon_schema = JSON .from_json (schema_json , Schema )
165+
213166 except FileNotFoundError :
214167 print (f"Error: Schema file not found: { schema_file } " , file = sys .stderr )
215168 sys .exit (1 )
169+ except json .JSONDecodeError as e :
170+ print (f"Error: Invalid JSON format in schema file: { e } " , file = sys .stderr )
171+ sys .exit (1 )
216172 except Exception as e :
217- print (f"Error: Failed to read schema file : { e } " , file = sys .stderr )
173+ print (f"Error: Failed to parse schema: { e } " , file = sys .stderr )
218174 sys .exit (1 )
219175
220- # Parse schema definition
176+ # Create table
221177 try :
222- # Validate required fields
223- if 'fields' not in schema_def :
224- print ("Error: Schema must contain 'fields' section" , file = sys .stderr )
225- sys .exit (1 )
226-
227- # Build PyArrow schema from definition
228- import pyarrow as pa
229- pa_fields = []
230- for field in schema_def ['fields' ]:
231- field_name = field .get ('name' )
232- field_type = field .get ('type' )
233-
234- if not field_name or not field_type :
235- print ("Error: Each field must have 'name' and 'type'" , file = sys .stderr )
236- sys .exit (1 )
237-
238- # Convert type string to PyArrow type
239- from pypaimon .schema .data_types import DataTypeParser , PyarrowFieldParser
240-
241- # Parse type string to Paimon DataType, then convert to PyArrow type
242- paimon_type = DataTypeParser .parse_data_type (field_type )
243- pa_type = PyarrowFieldParser .from_paimon_type (paimon_type )
244- pa_fields .append (pa .field (field_name , pa_type ))
245-
246- pa_schema = pa .schema (pa_fields )
247-
248- # Extract optional parameters
249- partition_keys = schema_def .get ('partition_keys' , [])
250- primary_keys = schema_def .get ('primary_keys' , [])
251- options = schema_def .get ('options' , {})
252- comment = schema_def .get ('comment' )
253-
254- # Create Paimon schema
255- paimon_schema = Schema .from_pyarrow_schema (
256- pa_schema ,
257- partition_keys = partition_keys if partition_keys else None ,
258- primary_keys = primary_keys if primary_keys else None ,
259- options = options if options else None ,
260- comment = comment
261- )
262-
263- # Create table
264178 ignore_if_exists = args .ignore_if_exists
265179 catalog .create_table (f"{ database_name } .{ table_name } " , paimon_schema , ignore_if_exists )
266180
@@ -309,9 +223,9 @@ def add_table_subcommands(table_parser):
309223 help = 'Table identifier in format: database.table'
310224 )
311225 create_parser .add_argument (
312- '--schema-file ' , '-s' ,
226+ '--schema' , '-s' ,
313227 required = True ,
314- help = 'Path to schema definition file ( JSON or YAML) '
228+ help = 'Path to schema JSON file '
315229 )
316230 create_parser .add_argument (
317231 '--ignore-if-exists' , '-i' ,
0 commit comments