storytracer's picture
Upload ol_duckdb.sh with huggingface_hub
3bdc46a verified
#!/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