代码之家  ›  专栏  ›  技术社区  ›  aaron

如何明确声明气流连接的字符集=utf8

  •  5
  • aaron  · 技术社区  · 7 年前

    此序列:

    from airflow.hooks.mysql_hook import MySqlHook
    conn = MySqlHook(mysql_conn_id='conn_id')
    engine = conn.get_sqlalchemy_engine()
    df.to_sql('test_table', engine, if_exists='append', index=False)
    

    产生以下结果:

    UnicodeEncodeError: 'latin-1' codec can't encode character '\ufffd' in position 57: ordinal not in range(256)
    

    这个序列效果很好:

    from sqlalchemy import create_engine
    engine = create_engine("mysql://{0}:{1}@{2}/capone?charset=utf8".format(user, pwd, host))
    df.to_sql('test_table', engine, if_exists='append', index=False)
    

    charset . 我尝试在气流中这样做,如下所示 {"charset": "utf8"} :

    enter image description here

    3 回复  |  直到 7 年前
        1
  •  2
  •   lolcode    5 年前

    https://issues.apache.org/jira/browse/AIRFLOW-4824

    目前,我有以下代码的变通方法:

    def get_uri(hook):
        conn = hook.get_connection(getattr(hook, hook.conn_name_attr))
        login = ''
        if conn.login:
            login = '{conn.login}:{conn.password}@'.format(conn=conn)
        host = conn.host
        if conn.port is not None:
            host += ':{port}'.format(port=conn.port)
        charset = ''
        if conn.extra_dejson.get('charset', False):
            chrs = conn.extra_dejson["charset"]
            if chrs.lower() == 'utf8' or chrs.lower() == 'utf-8':
                charset = '?charset=utf8'
        return '{conn.conn_type}://{login}{host}/{conn.schema}{charset}'.format(
            conn=conn, login=login, host=host, charset=charset)
    

    然后按如下方式使用:

    url = get_uri(sql_hook)
    from sqlalchemy import create_engine
    engine = create_engine(url)
    

        2
  •  2
  •   Lvtao Wang    5 年前
    from sqlalchemy import create_engine
    from airflow.hooks.mysql_hook import MySqlHook
    
    conn = MySqlHook(mysql_conn_id='conn_id')
    uri = conn.get_uri()
    engine = create_engine(uri+'?charset=utf8')
    df.to_sql('test_table', engine, if_exists='append', index=False)
    
    

    我用上面的代码解决了这个问题。

        3
  •  2
  •   wolf    4 年前

    我顺便解决了这个问题,效果很好(在文件aiffort.cfg中编辑):

    sql_alchemy_conn = mysql://user:password@host:port/airflow?charset=utf8