Exemple #1
0
func TestSession(host string, keyspace string) *gocql.Session {
	cluster := gocql.NewCluster(host)
	cluster.Keyspace = keyspace
	session, err := cluster.CreateSession()
	if err != nil {
		log.Fatalf("Could not connect to cassandra: %v", err)
		os.Exit(1)
	}
	return session
}
Exemple #2
0
func ColumnFamilies(host string, keyspace string, verbose bool) ([]ColumnFamily, error) {

	cluster := gocql.NewCluster(host)
	session, err := cluster.CreateSession()

	if err != nil {
		fmt.Errorf("Connect error", err)
	}

	fmt.Printf("Reading schema from keyspace: %s\n", keyspace)

	iter := session.Query(`SELECT columnfamily_name 
                           FROM system.schema_columnfamilies
                           WHERE keyspace_name = ?`, keyspace).Iter()

	columnFamilies := make([]ColumnFamily, 0)
	var cf ColumnFamily
	for iter.Scan(&cf.Name) {
		columnFamilies = append(columnFamilies, cf)
	}

	err = iter.Close()
	if err != nil {
		fmt.Errorf("Read error", err)
	}

	for i, cf := range columnFamilies {

		if verbose {
			fmt.Printf("Reading metadata for %s.%s ...\n", keyspace, cf.Name)
		}

		iter := session.Query(`SELECT column_name, type, validator, component_index 
                               FROM system.schema_columns
                               WHERE keyspace_name = ? AND columnfamily_name = ?`, keyspace, cf.Name).Iter()
		columns := make([]Column, 0)
		var col Column
		var colKeyType, validator string
		for iter.Scan(&col.Name, &colKeyType, &validator, &col.ComponentIndex) {
			col.KeyType = keyTypes[colKeyType]
			dataType, present := dataTypes[validator]

			if !present {
				// TODO This is extremely hacky, must clean this up
				// Basically a map<text,text> type will come through as:
				// org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.UTF8Type)
				parts := strings.Split(validator, "(")
				if len(parts) == 0 {
					// TODO should error out here really, since we can't map the type
					fmt.Printf("Unmapped data type: %s\n", validator)
				}
				dataType = collectionDataTypes[parts[0]]
				if dataType == 0 {
					// TODO should error out here really, since we can't map the type
					fmt.Printf("Unmapped data type: %s\n", validator)
				}
			}

			col.DataType = dataType

			if col.DataType == CounterType {
				columnFamilies[i].IsCounter = true
			}

			columns = append(columns, col)
		}

		sort.Sort(sort.Reverse(ByComponentIndex(columns)))

		foundParitioned := false
		foundClustered := false

		for i, _ := range columns {

			if foundClustered && foundParitioned {
				break
			}

			if !foundClustered {
				if columns[i].SupportsClustering() {
					columns[i].IsLastComponent = true
					foundClustered = true
				}
			}

			if !foundParitioned {
				if columns[i].SupportsPartitioning() {
					columns[i].IsLastComponent = true
					foundParitioned = true
				}
			}
		}

		if verbose {
			for _, col := range columns {
				fmt.Printf("[%s.%s] Column: %+v\n", keyspace, cf.Name, col)
			}
		}

		columnFamilies[i].Columns = columns
	}

	err = iter.Close()
	if err != nil {
		fmt.Printf("Read error", err)
	}

	session.Close()

	return columnFamilies, err
}