(轉)使用binary memtable 批量導資料到cassandra

oxoxooxx發表於2011-05-22
package com.alibaba.dw.thrift.client;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.db.Column;
import org.apache.cassandra.db.ColumnFamily;
import org.apache.cassandra.db.RowMutation;
import org.apache.cassandra.db.filter.QueryPath;
import org.apache.cassandra.io.util.DataOutputBuffer;
import java.net.InetAddress;
 
import org.apache.cassandra.net.Message;
import org.apache.cassandra.net.MessagingService;
import org.apache.cassandra.service.StorageService;
 
/**
 * TODO Comment of BinaryMemtableTest
 * 
 * @author aaron.guop
 */
public class BinaryMemtableTest {
 
    /**
     * @param args
     * @throws IOException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws IOException, InterruptedException {
        System.setProperty("storage-config", "D:apache-cassandra-0.6.1conf");
 
        StorageService.instance.initClient();
 
        while (StorageService.instance.getNaturalEndpoints("Keyspace1", "bmt").isEmpty()) {
            Thread.sleep(1 * 1000);
        }
        
 
        doInsert();
 
        StorageService.instance.stopClient();
    }
 
    /**
     * @throws UnsupportedEncodingException
     */
    private static void doInsert() throws UnsupportedEncodingException {
        String keyspace = "Keyspace1";
        String cfName = "Member";
        String memberID = "bmt";
 
        /* Create a column family */
        ColumnFamily columnFamily = ColumnFamily.create(keyspace, cfName);
 
        //while (values.hasNext()) {
        String SuperColumnName = "SuperColumnName";
        String ColumnName = "ColumnName";
        String ColumnValue = "ColumnValue";
        long timestamp = 0;
        columnFamily.addColumn(new QueryPath(cfName, SuperColumnName.getBytes("UTF-8"), ColumnName
                .getBytes("UTF-8")), ColumnValue.getBytes(), timestamp);
        //}
 
        /* Get serialized message to send to cluster */
        Message message = createMessage(keyspace, memberID, cfName, columnFamily);
        for (InetAddress endpoint : StorageService.instance.getNaturalEndpoints(keyspace, memberID)) {
            /* Send message to end point */
            MessagingService.instance.sendOneWay(message, endpoint);
            System.out.println("Send message to " + endpoint.toString());
        }
    }
 
    public static Message createMessage(String Keyspace, String Key, String CFName,
                                        ColumnFamily columnFamile) {
        DataOutputBuffer bufOut = new DataOutputBuffer();
        Column column;
 
        /*
         * Get the first column family from list, this is just to get past
         * validation
         */
        ColumnFamily baseColumnFamily = new ColumnFamily(CFName, "Standard", DatabaseDescriptor
                .getComparator(Keyspace, CFName), DatabaseDescriptor.getSubComparator(Keyspace,
                CFName));
 
        bufOut.reset();
        try {
            ColumnFamily.serializer().serializeWithIndexes(columnFamile, bufOut);
            byte[] data = new byte[bufOut.getLength()];
            System.arraycopy(bufOut.getData(), 0, data, 0, bufOut.getLength());
 
            column = new Column(columnFamile.name().getBytes("UTF-8"), data, 0, false);
            baseColumnFamily.addColumn(column);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
 
        RowMutation rm = new RowMutation(Keyspace, Key);
        rm.add(baseColumnFamily);
 
        try {
            /* Make message */
            return rm.makeRowMutationMessage(StorageService.Verb.BINARY);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
}
[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23937368/viewspace-1050213/,如需轉載,請註明出處,否則將追究法律責任。

相關文章