【问题标题】:Trying to insert image/xml file as blob in cassandra database试图在 cassandra 数据库中将图像/xml 文件作为 blob 插入
【发布时间】:2017-02-21 18:13:17
【问题描述】:

实际上我需要在 cassandra 数据库中插入 xml 文件。所以最初我试图将图像作为 blob 内容插入,后来我将代码更改为插入 xml,但在插入和检索 blob 内容作为图像时遇到了问题。任何人都可以建议在 cassandra 数据库中插入图像/xml 文件的最佳做法。

FileInputStream fis=new FileInputStream("C:/Users/anand.png");
byte[] b= new byte[fis.available()+1];
int length=b.length;
fis.read(b);

System.out.println(length);

ByteBuffer buffer =ByteBuffer.wrap(b);

PreparedStatement ps = session.prepare("insert into usersimage (firstname,lastname,age,email,city,length,image) values(?,?,?,?,?,?,?)");

BoundStatement boundStatement = new BoundStatement(ps);

int age=22;
//System.out.println(buffer);

session.execute( boundStatement.bind( "xxx","D",age,"xxx@gmail.com","xxx",length,buffer));

//session.execute(  boundStatement.bind( buffer, "Andy", length));



 PreparedStatement ps1 = session.prepare("select * from usersimage where email =?");
    BoundStatement boundStatement1 = new BoundStatement(ps1);
    ResultSet rs =session.execute(boundStatement1.bind("ramya1@gmail.com"));

    ByteBuffer bImage=null; 

    for (Row row : rs) {
     bImage = row.getBytes("image") ;
     length=row.getInt("length");
    }

byte image[]= new byte[length];
image=Bytes.getArray(bImage);

HttpServletResponse response = null;
@SuppressWarnings("null")
OutputStream out = response.getOutputStream();
response.setContentType("image/png");
response.setContentLength(image.length);
out.write(image);

在将 blob 内容作为图像检索时遇到问题。谁能帮我解决这个问题。

【问题讨论】:

  • 尝试使用 datastax OGM 库进行 DB 操作。此外,当您说“面临问题”时,请描述具体的问题,例如错误、堆栈跟踪
  • 线程“main”java.lang.NullPointerException 中的@monish 异常,在检索记录时获取空值

标签: java xml cassandra


【解决方案1】:
  • 您正在将数据插入到电子邮件中并选择另一个;
  • 读取图像字节的更好方法是:

    BufferedImage originalImage = ImageIO.read(new File("C:/Users/anand.png"));
    ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
    ImageIO.write(originalImage, "png", imageStream );
    imageStream.flush();
    byte[] imageInByte = imageStream.toByteArray();
    

【讨论】:

  • 实际上是在插入一条记录,并根据插入的记录选择记录。
猜你喜欢
  • 1970-01-01
  • 2017-03-26
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多