使用hbase处理图像,视频和音频类型

时间:2016-12-15 09:21:20

标签: java hadoop hbase

任何人都有任何想法,如何使用Hbase处理音频,视频和图像等非结构化数据。我尝试了很多,但我没有任何想法。请任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

  • 选项1:将图像转换为字节数组,您可以准备放置请求并插入表格。同样也可以实现音频和视频文件。

请参阅https://docs.oracle.com/javase/7/docs/api/javax/imageio/package-summary.html

import javax.imageio.ImageIO;

/*       * Convert an image to a byte array
         */
    private byte[] convertImageToByteArray (String ImageName)throws IOException {

        byte[] imageInByte;
        BufferedImage originalImage = ImageIO.read(new File(ImageName));

        // convert BufferedImage to byte array
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(originalImage, "jpg", baos);
        imageInByte = baos.toByteArray();
        baos.close();

        return imageInByte;
    }
  • 选项2:您可以使用Apache commons lang API.以下方式执行此操作,这可能是上述最佳选项,适用于所有对象,包括图像/音频/视频等。可以使用不只是对于hbase你也可以将其保存在hdfs中

有关详细信息,请参阅我的answer

例如:byte[] mediaInBytes = org.apache.commons.lang.SerializationUtils.serialize(Serializable obj)

要进行反序列化,您可以执行此操作static Object deserialize(byte[] objectData)

请参阅上述链接中的文档..

SerializationUtils

的使用示例
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.lang.SerializationUtils;

public class SerializationUtilsTest {
  public static void main(String[] args) {
    try {
      // File to serialize object to it can be your image or any media file
      String fileName = "testSerialization.ser";

      // New file output stream for the file
      FileOutputStream fos = new FileOutputStream(fileName);

      // Serialize String
      SerializationUtils.serialize("SERIALIZE THIS", fos);
      fos.close();

      // Open FileInputStream to the file
      FileInputStream fis = new FileInputStream(fileName);

      // Deserialize and cast into String
      String ser = (String) SerializationUtils.deserialize(fis);
      System.out.println(ser);
      fis.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

注意:在hadoop集群中始终可以使用jar的apache commons。( not external dependency