CoderOnly
CoderOnly

Reputation: 1742

how to read file from cassandra and write file to cassandra?

I don't know what keywords to google.

I 'm new to datastax.

I guess I can insert file into cassandra EASILY And I guess I need not write file to cassandra like this :

  out = new FileOutputStream(new File(“C:/test.txt”));   
  out.write(“java java java\r\n”.getBytes()); 

Upvotes: 3

Views: 3485

Answers (2)

Oded Peer
Oded Peer

Reputation: 2427

Using the Datastax driver:

Insert:

    ByteBuffer fileByteBuffer = ByteBuffer.wrap( readFileToByteArray( filename ) );
    Statement insertFile = QueryBuilder.insertInto( "files" ).value( "filename", filename ).value( "file", fileByteBuffer );
    session.execute( insertFile );

Read:

    Statement readFile = QueryBuilder.select( "file" ).from( "files" ).where( QueryBuilder.eq( "filename", filename ) );
    Row fileRow = session.execute( readFile ).one();
    if ( fileRow != null ) {
        ByteBuffer fileBytes = fileRow.getBytes( "file" );
        File f = convertToFile( fileBytes );
    }

Upvotes: 4

Raedwald
Raedwald

Reputation: 48634

Cassandra is a key-value store, not a filesystem, so you can't write files to it. All values in Cassandra are, ultimately, just byte sequences. So you although you can not store a file in Cassandra, you can store the contents of a file. You will want to read the file contents into a byte array, then store the contents of that arrat in Cassandra.

Upvotes: 2

Related Questions