Как наиболее эффективно вставить данные в Hbase из CSV-файла с помощью Python? - PullRequest
0 голосов
/ 06 октября 2018

Я пытаюсь вставить данные в Hbase в кратчайшие сроки.Я пытаюсь способом, показанным внизу, однако я получаю ошибку, показанную ниже.У кого-нибудь есть идеи, что не так и как я могу это решить?Может hbase org.apache.hadoop.hbase.mapreduce.ImportTsv эффективнее?Если требуется дополнительная информация, просто дайте мне знать.Заранее спасибо.

ОШИБКА

Connect to HBase. table name: rfic, batch size: 1000
Connected to file. name: /path/to/hbase/logs2.csv
Traceback (most recent call last):
  File "insert-data2.py", line 87, in <module>
    batch.send()
  File "/usr/local/lib/python3.4/dist-packages/happybase/batch.py", line 60, in send
    self._table.connection.client.mutateRows(self._table.name, bms, {})
  File "/usr/local/lib/python3.4/dist-packages/thriftpy/thrift.py", line 198, in _req
    return self._recv(_api)
  File "/usr/local/lib/python3.4/dist-packages/thriftpy/thrift.py", line 210, in _recv
    fname, mtype, rseqid = self._iprot.read_message_begin()
  File "thriftpy/protocol/cybin/cybin.pyx", line 439, in cybin.TCyBinaryProtocol.read_message_begin (thriftpy/protocol/cybin/cybin.c:6470)
cybin.ProtocolError: No protocol version header

Ниже приведен мой исходный код, но если у вас есть какое-либо другое рабочее решение, я буду признателен за предоставление.

insert-data2.py

import csv
import happybase
import time

batch_size = 1000
host = "0.0.0.0"
file_path = "/path/to/hbase/logs2.csv"
namespace = "sample_data"
row_count = 0
start_time = time.time()
table_name = "rfic"


def connect_to_hbase():
    """ Connect to HBase server.

    This will use the host, namespace, table name, and batch size as defined in
    the global variables above.
    """
    conn = happybase.Connection(host = host,
        table_prefix = namespace,
        table_prefix_separator = ":")
    conn.open()
    table = conn.table(table_name)
    batch = table.batch(batch_size = batch_size)
    return conn, batch


def insert_row(batch, row):
    """ Insert a row into HBase.

    Write the row to the batch. When the batch size is reached, rows will be
    sent to the database.

    Rows have the following schema:
        [ id, keyword, subcategory, type, township, city, zip, council_district,
          opened, closed, status, origin, location ]
    """
    batch.put(row[0], { "data:log": row[1]})


def read_csv():
    csvfile = open(file_path, "r")
    csvreader = csv.reader(csvfile)
    return csvreader, csvfile


# After everything has been defined, run the script.
conn, batch = connect_to_hbase()
print "Connect to HBase. table name: %s, batch size: %i" % (table_name, batch_size)
csvreader, csvfile = read_csv()
print "Connected to file. name: %s" % (file_path)

try:
    # Loop through the rows. The first row contains column headers, so skip that
    # row. Insert all remaining rows into the database.
    for row in csvreader:
        row_count += 1
        if row_count == 1:
            pass
        else:
            insert_row(batch, row)

    # If there are any leftover rows in the batch, send them now.
    batch.send()
finally:
    # No matter what happens, close the file handle.
    csvfile.close()
    conn.close()

duration = time.time() - start_time
print "Done. row count: %i, duration: %.3f s" % (row_count, duration)
...