用python将txt文件数据导入到hbase中

首先虚拟机要安装好hbase环境,其次主机要能ping通虚拟机否则会报错

txt文件如下

用python将txt文件数据导入到hbase中_第1张图片

 

hbase表结构如下,列簇info下还会有多行列

用python将txt文件数据导入到hbase中_第2张图片

 插入数据的代码如下,txt文件要在py文件的同级目录下。首先要创建一个hbase表,需要注意的是txt文件有几列,hbase对应也要有几列

import pandas as pd
import matplotlib.pyplot as plt
import happybase
connect=happybase.Connection(host='10.101.1.28',port=9090)
connect.open();
def createtable(connect):
    table = happybase.Table("h6WBNR", connect)
    families = {
        "info": dict(),
    }
    connect.create_table("h6WBNR", families)
    file_name = 'WBNR.txt'
    columns = [
        'info:SEQ',
        'info:hotel',
        'info:country',
        'info:province',
        'info:city',
        'info:location',
        'info:star',
        'info:type',
        'info:rooms',
        'info:score',
        'info:comment',
        'info:avgroompernight',
        'info:totalorder',
        'info:totalnightroom',
        'info:actualorder',
        'info:actualroomnight',
        'info:directorder',
        'info:directorderroomnight',
        'info:directactualorder',
        'info:directactualroomnight',
        'info:directrefusedorder',
        'info:dierctorderrefusedProbability',
        'info:citydirectorder',
        'info:citydirectorderrefusedProbability',
        'info:Probabilityhighorlow'
    ]
    with open(file_name,encoding='utf-8') as file_obj:
        for content in file_obj:
            lines = content.split(",")
            for i in range(len(lines)):
                table.put(lines[0], {columns[i - 1]: lines[i]})

    for key, value in table.scan(): print(key, value)

插入成功的效果 

用python将txt文件数据导入到hbase中_第3张图片 

数据已经完全导入到了hbase 

 用python将txt文件数据导入到hbase中_第4张图片

 

你可能感兴趣的:(hbase,python,数据库)