0
回答
如何修改 能插入内容歇息额
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
import java.io.UnsupportedEncodingException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.Mutation;
import org.apache.cassandra.thrift.SuperColumn;
import org.apache.cassandra.thrift.TBinaryProtocol;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class SuperColumnDemo {
    static Cassandra.Client cassandraClient;
    static TTransport socket;
    static String  columnFamily = "t2";
    static String tablename = "1";
    private static void init(String keySpace) throws InvalidRequestException, TException {
        String server = "192.168.2.33";
        int port = 9160;
        socket = new TSocket(server, port);
        System.out.println(" connected to " + server + ":" + port + ".");
        TFramedTransport transport = new TFramedTransport(socket);
        TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport);
        cassandraClient = new Cassandra.Client(binaryProtocol);
        socket.open();
        cassandraClient.set_keyspace(keySpace);
    }
    public static void main(String[] args) throws Exception {
        String keyspace = "keyspace2";
        init(keyspace);
        insertSuperColumn();
    }
    public static void insertSuperColumn() throws Exception {
        Map<ByteBuffer, Map<String,List<Mutation>>> mutationMap = createWaldorfMutation(); 
        System.out.println(mutationMap);
        cassandraClient.batch_mutate(mutationMap, ConsistencyLevel.ONE);
    }
    private static Map<ByteBuffer, Map<String, List<Mutation>>> createWaldorfMutation() 
            throws UnsupportedEncodingException { 
            Column nameCol = new Column();
            nameCol.setName("name".getBytes());
            nameCol.setValue("mike".getBytes());
            nameCol.setTimestamp(System.currentTimeMillis());
            Column phoneCol = new Column();
            phoneCol.setName("phone".getBytes());
            phoneCol.setValue("212-555-5555".getBytes());
            phoneCol.setTimestamp(System.currentTimeMillis());
            ColumnOrSuperColumn nameCosc = new ColumnOrSuperColumn(); 
            nameCosc.column = nameCol; 
            ColumnOrSuperColumn phoneCosc = new ColumnOrSuperColumn(); 
            phoneCosc.column = phoneCol; 
            Mutation nameMut = new Mutation(); 
            nameMut.column_or_supercolumn = nameCosc; 
            Mutation phoneMut = new Mutation(); 
            phoneMut.column_or_supercolumn = phoneCosc; 
            Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap = 
            new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); 
            Map<String, List<Mutation>> muts = 
            new HashMap<String, List<Mutation>>(); 
            List<Mutation> cols = new ArrayList<Mutation>(); 
            cols.add(nameMut); 
            cols.add(phoneMut);  
            muts.put(columnFamily, cols); 
            System.out.println(muts);
            mutationMap.put(Translater.toByteBuffer(tablename), muts); 
            return mutationMap; 
            } 
} 
<无标签>
举报
jy34521
发帖于6年前 0回/140阅
顶部