복붙노트

[HADOOP] Mrjob와 하둡과 PostgreSQL 데이터베이스를 채우는 방법

HADOOP

Mrjob와 하둡과 PostgreSQL 데이터베이스를 채우는 방법

나는 MrJob와 하둡 2.7.1와 매퍼를 사용하여 PostgreSQL을의 데이터베이스를 채우는 싶습니다. 저는 현재 다음과 같은 코드를 사용하여 :

# -*- coding: utf-8 -*-
#Script for storing the sparse data into a database by using Hadoop
import psycopg2
import re
from mrjob.job import MRJob

args_d = False
args_c = True
args_s = True
args_n = 'es_word_space'


def unicodize(segment):
    if re.match(r'\\u[0-9a-f]{4}', segment):
        return segment.decode('unicode-escape')
    return segment.decode('utf-8')

def create_tables(cr):
    cr.execute("create table word_list(id serial primary key, word character varying not null)")
    cr.execute("""create table word_sparse(
        id serial primary key, 
        word_id integer references word_list(id) not null,
        pos integer not null,
        val float not null)""")

def delete_tables(cr):
    cr.execute("drop table word_sparse")
    cr.execute("drop table word_list")

class MRwordStore(MRJob):
    def mapper(self, _, line):
        global cr

        item = line.strip().split('\t')
        replaced = u"".join((unicodize(seg) for seg in re.split(r'(\\u[0-9a-f]{4})', item[0])))
        key = u''.join((c for c in replaced if c != '"'))

        cr.execute("insert into word_list(word) values(%s) returning id", (key,))
        word_id = cr.fetchone()[0]

            #Parse the list, literal_eval is avoided because of memory issues
        inside = False
        number = ""
        pos = 0
        val = 0
        for c in item[1]:
            if c == '[':
                inside = True
            elif c.isdigit():
                number += c
            elif c == ',':
                if inside:
                    pos = int(number)
                    number = ""
            elif c == ']':
                if inside:
                    val = int(number)
                    number = ""
                    cr.execute("insert into word_sparse(word_id, pos, val) values (%s, %s, %s)", (word_id, pos, val))
                inside = False

if __name__ == "__main__":
    """
    Stores words in the database.

    The first time, run with the arguments -cs.
    If the database has to be recreated, run again with the d argument (-dcs)

    It also asumes the owner of the database is a user named semeval with password semeval
    """
    global cr

    conn = psycopg2.connect("dbname=%s user=semeval password=semeval" % args_n)
    cr = conn.cursor()
    if args_d:
        delete_tables(cr)
    if args_c:
        create_tables(cr)
    if args_s:
        MRwordStore().run()

    conn.commit()
    conn.close()

나는 감속기를 사용하지 않을 것을 시도했다. 내 스크립트를 호출하여 나는이 출력을 가지고 :

$ python db_store_hadoop.py -r hadoop /almac/ignacio/data/wdSp_sparse.txt
no configs found; falling back on auto-configuration
no configs found; falling back on auto-configuration
creating tmp directory /tmp/db_store_hadoop.hduser.20160113.012419.718376
writing wrapper script to /tmp/db_store_hadoop.hduser.20160113.012419.718376/setup-wrapper.sh
Using Hadoop version 2.7.1
Copying local files into hdfs:///user/hduser/tmp/mrjob/db_store_hadoop.hduser.20160113.012419.718376/files/

PLEASE NOTE: Starting in mrjob v0.5.0, protocols will be strict by default. It's recommended you run your job with --strict-protocols or set up mrjob.conf as described at https://pythonhosted.org/mrjob/whats-new.html#ready-for-strict-protocols

그리고 교수형 것 같다, 더 없다. 여기 내 입력 파일의 예입니다 :

"\u00e1gil" [[1572, 1], [1590, 1], [4, 1], [774, 1]]
"\u00e1guila"   [[10, 5], [1116, 2], [15, 1], [1590, 1], [1641, 2], [1704, 1], [1740, 3], [183, 1], [3, 1], [428, 2], [900, 3]]
"\u00e1guilas"  [[1043, 1], [248, 1], [618, 1], [701, 2], [862, 2], [864, 2]]
"\u00e1lava"    [[1572, 1], [1576, 2], [1590, 1], [726, 2]]

이는 1.5GB의 길이입니다. 난 이미 데이터베이스를 생성하고이 비어 있습니다. 나는 많은 오해가 아마 생각하기 때문에 당신의 도움을 주셔서 감사합니다.

해결법

  1. ==============================

    1.각 매퍼는 자신의 데이터베이스 연결을 필요로한다. () mapper_init에서 데이터베이스 연결을 생성하고 mapper_final에 닫습니다 (). 당신은 mrjob 스크립트에서 별도로 데이터베이스를 작성해야합니다. 먼저 아주 간단한 mrjob 스크립트를 시도해야합니다. 당신은 그것을 올바른 방법을 시작하지 않았습니다. 문서의 예제를 통해 작업 할 수 있습니다.

    각 매퍼는 자신의 데이터베이스 연결을 필요로한다. () mapper_init에서 데이터베이스 연결을 생성하고 mapper_final에 닫습니다 (). 당신은 mrjob 스크립트에서 별도로 데이터베이스를 작성해야합니다. 먼저 아주 간단한 mrjob 스크립트를 시도해야합니다. 당신은 그것을 올바른 방법을 시작하지 않았습니다. 문서의 예제를 통해 작업 할 수 있습니다.

  2. from https://stackoverflow.com/questions/34758338/how-to-populate-a-postgresql-database-with-mrjob-and-hadoop by cc-by-sa and MIT license