如何在Impala中存储图像文件

时间:2017-03-01 06:50:08

标签: database image hadoop hdfs impala

我在本地系统中有一个图像文件(jpg或jpeg),我想存储在Impala数据库中,请帮助我怎么做?

1 个答案:

答案 0 :(得分:2)

我认为您有几种方法可以解决这个问题,具体取决于您的具体要求。

<强> 1。使用Hive

Hive允许您将二进制数据存储在Hive&#39;数据库中。 Hive与Impala类似,但通常速度较慢但功能更多。 您可以在表定义中使用DataType BINARY并使用LOAD DATA加载图像。 这样的事情可能有效(未经测试)。

Create table images (picture binary); 
LOAD DATA LOCAL inpath 'x/y/image.jpg' INTO TABLE images;

<强> 2。使用Impala

Impala does not allow binary data。您可以做的是使用序列化 - 反序列化方法。这意味着您将图像转换为String格式,该格式仍包含将其转换回所需的所有信息。一旦需要在HDFS上检索图像,就需要反序列化,这意味着将字符串转换为原始格式。

以Python为例,这将是这样的:

import base64

def img_to_string(image_path):
    with open(image_path, "rb") as imageFile:
        image_string= base64.b64encode(imageFile.read())
        print image_string

def string_to_img(image_string):
    with open("new_image.png", "wb") as imageFile:
        imageFile.write(str.decode('base64'))

第3。仅使用HDFS

通常不需要将数据存储在数据库中。你可以做的只是将图像放在HDFS中。如有必要,您可以将HDFS文件路径保存在数据库中。然后,您可以使用Impala查询检索路径。 从远程位置获取文件然后需要您运行以下(更多信息here):

ssh <user>@<host> "hadoop fs -get <hdfs_path> <os_path>"
then scp command to copy files