File size: 1,264 Bytes
3bdc46a |
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 |
#!/bin/bash
# Check if exactly two arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <dump_folder> <duckdb_file>"
exit 1
fi
# Assign the folder and DuckDB file arguments to variables
FOLDER=$1
DUCKDB_FILE=$2
# Check if the provided folder is a directory
if [ ! -d "$FOLDER" ]; then
echo "Error: $FOLDER is not a directory"
exit 1
fi
# Check if pv is installed
PV_INSTALLED=false
if command -v pv &> /dev/null; then
PV_INSTALLED=true
fi
# List of table names
TABLES=("authors" "works" "editions")
# Function to process each file
process_file() {
local file=$1
local table=$2
if $PV_INSTALLED; then
pv -N "$table" < "$file"
else
cat "$file"
fi | gunzip -c | cut -f5 | duckdb "$DUCKDB_FILE" -c "CREATE TABLE $table AS SELECT * FROM read_json_auto('/dev/stdin', union_by_name=true, ignore_errors=true);"
}
# Process each table
for TABLE in "${TABLES[@]}"; do
# Search for the file using a regex pattern
FILE=$(ls "$FOLDER"/ol_dump_${TABLE}_*.txt.gz 2> /dev/null | head -n 1)
# Check if the file exists
if [ -n "$FILE" ]; then
process_file "$FILE" "$TABLE"
else
echo "Warning: Dump file for $TABLE not found in $FOLDER. Skipping $TABLE."
fi
done
|