Domanda

Sono nuovo di cassandra, attualmente sto usando CassandraCSharpDriver in un'applicazione fittizia.Voglio ottenere la lista di tutte le tabelle di un utente ha descritto in un determinato tasto spazio.

        Cluster cluster = Cluster.Builder().AddContactPoints("IPAddress").Build();
        Session session = cluster.Connect("MyKeySpace");

In questo codice che voglio ottenere l'elenco di tutte le tabelle di MyKeySpace

È stato utile?

Soluzione

Correrò attraverso come vorrei preparare quell'elenco delle tabelle con il DATASTAX Cassandra C # Driver:

Connetti al tuo cluster:

Cluster cluster = Cluster.Builder().AddContactPoints("IPAddress").Build();
Session session = cluster.Connect();
.

Creerò un List<String> e utilizzerò una dichiarazione preparata (perché è solo una buona idea) per legare il nome della tua chiave.La mia istruzione CQL sta solo selezionando columnfamily_name, che dovrebbe essere ciò di cui hai bisogno.

List<String> tableList = new List<String>();

String strCQL = "SELECT columnfamily_name "
    + "FROM system.schema_columnfamilies WHERE keyspace_name=? ";
PreparedStatement pStatement = _session.Prepare(strCQL);
BoundStatement boundStatement = new BoundStatement(pStatement);
boundStatement.Bind("MyKeySpace");
.

Ora eseguirò l'istruzione, iterarò attraverso i risultati e aggiungi ogni nome tabella all'elenco che ho creato sopra.

RowSet results = session.Execute(boundStatement);

foreach (Row result in results.GetRows())
{
    tableName = result.GetValue<String>("columnfamily_name");
    tableList.Add(tableName);
}
.

Ora dovresti avere un elenco di tabelle che puoi aggiungere al tuo UI.

Altri suggerimenti

È possibile eseguire:

select * from system.schema_columnfamilies where keyspace_name='MyKeySpace';
.

funziona contro il tasto "Sistema".

  1. Ottenere i nomi di colonna:

    SELECT column_name FROM system.schema_columns WHERE keyspace_name = 'KeySpaceName' AND columnfamily_name = 'TableName';
    
  2. Ottenere colonna di nomi di famiglia, cioè, i nomi di tabella:

    select columnfamily_name from system.schema_columnfamilies where keyspace_name='KeySpaceName';
    

C'è la possibilità di utilizzare anche i metadati del cluster per ottenere un elenco di stringhe di tasti come

var cluster = Cluster.Builder().AddContactPoints("IPAddress").Build();
var keyspaces = cluster.Metadata.GetKeyspaces();
foreach (var keyspaceName in keyspaces)
{
   Console.WriteLine(keyspaceName);
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top