Handling Images,Video and audio types using hbase

﹥>﹥吖頭↗ 提交于 2019-12-31 03:19:10

问题


Anybody have any idea about,How to handle unstructured data like Audio,Video and Images using Hbase.I tried for this alot but i didn't get any idea.please any help is appreciated.


回答1:


  • Option 1: convert image to byte array and you can prepare put request and insert to table. Similarly audio and video files also can be achieved.

See 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;
    }
  • Option 2 : You can do that in below way using Apache commons lang API. probably this is best option than above which will be applicable to all objects including image/audio/video etc.. This can be used NOT ONLY for hbase you can save it in hdfs as well

See my answer for more details.

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

for deserializing, you can do this static Object deserialize(byte[] objectData)

see the doc in above link..

Example usage of the 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();
    }
  }
}

Note :jar of apache commons lang always available in hadoop cluster.(not external dependency)



来源:https://stackoverflow.com/questions/41160472/handling-images-video-and-audio-types-using-hbase

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!