|
import pyarrow.parquet as pq |
|
import os |
|
import json |
|
|
|
|
|
def check_row_count(parquet_file_path): |
|
try: |
|
parquet_file = pq.ParquetFile(parquet_file_path) |
|
num_rows = parquet_file.metadata.num_rows |
|
print(f"The Parquet file '{parquet_file_path}' contains {num_rows} rows.") |
|
return num_rows |
|
except Exception as e: |
|
print(f"Error reading Parquet file: {e}") |
|
|
|
|
|
def get_specific_row(parquet_file_path, row_index): |
|
try: |
|
|
|
parquet_file = pq.ParquetFile(parquet_file_path) |
|
|
|
|
|
current_row = 0 |
|
|
|
|
|
for row_group in range(parquet_file.num_row_groups): |
|
|
|
table = parquet_file.read_row_group(row_group) |
|
|
|
|
|
if current_row <= row_index < current_row + table.num_rows: |
|
|
|
row_in_table = row_index - current_row |
|
|
|
|
|
row = {col: table.column(col)[row_in_table].as_py() for col in table.column_names} |
|
|
|
return row |
|
|
|
|
|
current_row += table.num_rows |
|
|
|
|
|
raise IndexError(f"Row index {row_index} is out of bounds.") |
|
|
|
except Exception as e: |
|
print(f"Error retrieving row: {e}") |
|
return None |
|
|
|
|
|
def save_image_from_row(row, output_image_path): |
|
try: |
|
image_content = row['image_content'] |
|
with open(output_image_path, 'wb') as img_file: |
|
img_file.write(image_content) |
|
print(f"Image saved successfully at {output_image_path}") |
|
except Exception as e: |
|
print(f"Error saving image: {e}") |
|
|
|
|
|
def save_json_from_row(row, output_json_path): |
|
try: |
|
json_content = row['json_content'] |
|
json_data = json.loads(json_content) |
|
with open(output_json_path, 'w', encoding='utf-8') as json_file: |
|
json.dump(json_data, json_file, indent=4, ensure_ascii=False) |
|
print(f"JSON saved successfully at {output_json_path}") |
|
except Exception as e: |
|
print(f"Error saving JSON: {e}") |
|
|
|
|
|
def save_all_images_and_jsons(parquet_file_path, output_folder): |
|
try: |
|
|
|
os.makedirs(output_folder, exist_ok=True) |
|
|
|
|
|
parquet_file = pq.ParquetFile(parquet_file_path) |
|
num_rows = parquet_file.metadata.num_rows |
|
|
|
|
|
for row_index in range(num_rows): |
|
row = get_specific_row(parquet_file_path, row_index) |
|
if row is not None: |
|
|
|
output_image_path = os.path.join(output_folder, f"image_{row_index}.jpg") |
|
output_json_path = os.path.join(output_folder, f"data_{row_index}.json") |
|
|
|
|
|
save_image_from_row(row, output_image_path) |
|
save_json_from_row(row, output_json_path) |
|
|
|
except Exception as e: |
|
print(f"Error saving all images and JSONs: {e}") |
|
|
|
|
|
def save_n_images_and_jsons(parquet_file_path, output_folder, num_to_save): |
|
try: |
|
|
|
os.makedirs(output_folder, exist_ok=True) |
|
|
|
|
|
parquet_file = pq.ParquetFile(parquet_file_path) |
|
num_rows = parquet_file.metadata.num_rows |
|
|
|
|
|
limit = min(num_to_save, num_rows) |
|
|
|
|
|
for row_index in range(limit): |
|
row = get_specific_row(parquet_file_path, row_index) |
|
if row is not None: |
|
|
|
output_image_path = os.path.join(output_folder, f"image_{row_index}.jpg") |
|
output_json_path = os.path.join(output_folder, f"data_{row_index}.json") |
|
|
|
|
|
save_image_from_row(row, output_image_path) |
|
save_json_from_row(row, output_json_path) |
|
|
|
except Exception as e: |
|
print(f"Error saving N images and JSONs: {e}") |
|
|
|
|
|
parquet_file_path = 'path/to/MobileViews_xxx-xxx.parquet' |
|
output_folder_all = 'path/to/all_extracted_images_jsons' |
|
output_folder_n = 'path/to/n_extracted_images_jsons' |
|
|
|
check_row_count('parquet_file_path') |
|
|
|
|
|
|
|
|
|
save_n_images_and_jsons(parquet_file_path, output_folder_n, num_to_save=100) |
|
|
|
|
|
|