示例#1
0
def insert_execute_batch(connection, beers: Iterator[Dict[str, Any]]) -> None:
    with connection.cursor() as cursor:
        create_staging_table(cursor)

        all_beers = [{
            **beer,
            'first_brewed':
            parse_first_brewed(beer['first_brewed']),
            'volume':
            beer['volume']['value'],
        } for beer in beers]

        psycopg2.extras.execute_batch(
            cursor, """
            INSERT INTO staging_beers VALUES (
                %(id)s,
                %(name)s,
                %(tagline)s,
                %(first_brewed)s,
                %(description)s,
                %(image_url)s,
                %(abv)s,
                %(ibu)s,
                %(target_fg)s,
                %(target_og)s,
                %(ebc)s,
                %(srm)s,
                %(ph)s,
                %(attenuation_level)s,
                %(brewers_tips)s,
                %(contributed_by)s,
                %(volume)s
            );
        """, all_beers)
示例#2
0
def insert_execute_values_iterator(
    connection,
    beers: Iterator[Dict[str, Any]],
    page_size: int = 100,
) -> None:
    with connection.cursor() as cursor:
        create_staging_table(cursor)
        psycopg2.extras.execute_values(
            cursor,
            """
            INSERT INTO staging_beers VALUES %s;
        """, ((
                beer['id'],
                beer['name'],
                beer['tagline'],
                parse_first_brewed(beer['first_brewed']),
                beer['description'],
                beer['image_url'],
                beer['abv'],
                beer['ibu'],
                beer['target_fg'],
                beer['target_og'],
                beer['ebc'],
                beer['srm'],
                beer['ph'],
                beer['attenuation_level'],
                beer['brewers_tips'],
                beer['contributed_by'],
                beer['volume']['value'],
            ) for beer in beers),
            page_size=page_size)
示例#3
0
def copy_stringio(connection, beers: Iterator[Dict[str, Any]]) -> None:
    with connection.cursor() as cursor:
        create_staging_table(cursor)
        csv_file_like_object = io.StringIO()
        for beer in beers:
            csv_file_like_object.write('|'.join(
                map(clean_csv_value, (
                    beer['id'],
                    beer['name'],
                    beer['tagline'],
                    parse_first_brewed(beer['first_brewed']),
                    beer['description'],
                    beer['image_url'],
                    beer['abv'],
                    beer['ibu'],
                    beer['target_fg'],
                    beer['target_og'],
                    beer['ebc'],
                    beer['srm'],
                    beer['ph'],
                    beer['attenuation_level'],
                    beer['contributed_by'],
                    beer['brewers_tips'],
                    beer['volume']['value'],
                ))) + '\n')
        csv_file_like_object.seek(0)
        cursor.copy_from(csv_file_like_object, 'staging_beers', sep='|')
示例#4
0
def copy_string_iterator(connection,
                         beers: Iterator[Dict[str, Any]],
                         size: int = 8192) -> None:
    with connection.cursor() as cursor:
        create_staging_table(cursor)
        beers_string_iterator = StringIteratorIO(('|'.join(
            map(clean_csv_value, (
                beer['id'],
                beer['name'],
                beer['tagline'],
                parse_first_brewed(beer['first_brewed']).isoformat(),
                beer['description'],
                beer['image_url'],
                beer['abv'],
                beer['ibu'],
                beer['target_fg'],
                beer['target_og'],
                beer['ebc'],
                beer['srm'],
                beer['ph'],
                beer['attenuation_level'],
                beer['brewers_tips'],
                beer['contributed_by'],
                beer['volume']['value'],
            ))) + '\n' for beer in beers))
        cursor.copy_from(beers_string_iterator,
                         'staging_beers',
                         sep='|',
                         size=size)