コード例 #1
0
//--------------------------------------------------------------------
func init() {
	c := gocql.NewCluster("127.0.0.1")

	s, err := c.CreateSession()
	if err != nil {
		panic(err)
	}

	gct.SetDefaultSession(s)

	KeySpace = gct.NewKeyspace("gocqltable_test")
}
コード例 #2
0
ファイル: crud.go プロジェクト: apaoww/gocqltable
func main() {

	// Generic initialization of gocql
	c := gocql.NewCluster("127.0.0.1")
	s, err := c.CreateSession()
	if err != nil {
		log.Fatalln("Unable to open up a session with the Cassandra database (err=" + err.Error() + ")")
	}

	// Tell gocqltable to use this session object as the default for new objects
	gocqltable.SetDefaultSession(s)
	fmt.Println("Gocql session setup complete")

	// Now we're ready to create our first keyspace. We start by getting a keyspace object
	keyspace := gocqltable.NewKeyspace("gocqltable_test")

	// Now lets create that in the database using the simple strategy and durable writes (true)
	err = keyspace.Create(map[string]interface{}{
		"class":              "SimpleStrategy",
		"replication_factor": 1,
	}, true)
	if err != nil { // If something went wrong we print the error and quit.
		log.Fatalln(err)
	}
	fmt.Println("Keyspace created")

	// Now that we have a very own keyspace to play with, lets create our first table.

	// First we need a Row-object to base the table on. It will later be passed to the table wrapper
	// to be used for returning row-objects as the answer to fetch requests.
	type User struct {
		Email    string // Our primary key
		Password string `password`     // Use Tags to rename fields
		Active   bool   `cql:"active"` // If there are multiple tags, use `cql:""` to specify what the table column will be
		Created  time.Time
	}

	// Let's define and instantiate a table object for our user table
	userTable := struct {
		recipes.CRUD // If you looked at the base example first, notice we replaced this line with the recipe
	}{
		recipes.CRUD{ // Here we didn't replace, but rather wrapped the table object in our recipe, effectively adding more methods to the end API
			keyspace.NewTable(
				"users",           // The table name
				[]string{"email"}, // Row keys
				nil,               // Range keys
				User{},            // We pass an instance of the user struct that will be used as a type template during fetches.
			),
		},
	}

	// Lets create this table in our cassandra database
	err = userTable.Create()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("")
	fmt.Println("Table created: users")

	// Now that we have a keyspace with a table in it: lets make a few rows! In the base example we had to write out the CQL manually, this time
	// around, however, we can insert entire User objects.

	// Lets instantiate a user object, set its values and insert it
	user1 := User{
		Email:    "*****@*****.**",
		Password: "******",
		Active:   true,
		Created:  time.Now().UTC(),
	}
	err = userTable.Insert(user1)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("User inserted:", user1)

	// And again for our next user.
	user2 := User{
		Email:    "*****@*****.**",
		Password: "******",
		Active:   true,
		Created:  time.Now().UTC(),
	}
	err = userTable.Insert(user2)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("User inserted:", user2)

	// And finally again for the third user.
	user3 := User{
		Email:    "*****@*****.**",
		Password: "******",
		Active:   true,
		Created:  time.Now().UTC(),
	}
	err = userTable.Insert(user3)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("User inserted:", user3)

	// With our database filled up with users, lets query it and print out the results (containing all users in the database).
	rowset, err := userTable.List()
	fmt.Println("")
	fmt.Println("Fetched all from users:")
	for _, user := range rowset.([]*User) {
		// user := row.(*User)         // Our row variable is a pointer to "interface{}", and here we type assert it to a pointer to "User"
		fmt.Println("User: "******"*****@*****.**")
	if err != nil {
		log.Fatalln(err)
	}
	user := row.(*User)
	fmt.Println("")
	fmt.Println("Fetched single row by email: ", user)

	// Lets update this user by changing his password
	user.Password = "******"
	err = userTable.Update(user)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("")
	fmt.Println("Updated user to:", user)

	// Lets delete user [email protected]
	err = userTable.Delete(user3)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("Deleted user:"******"")
	fmt.Println("Final list of users:")
	for _, user := range rowset.([]*User) {
		// user := row.(*User)         // Our row variable is a pointer to "interface{}", and here we type assert it to a pointer to "User"
		fmt.Println("User: "******"user_logs",
				[]string{"email"},
				[]string{"id"},
				UserLog{},
			),
		},
	}

	// Lets create this table in the database
	err = userLogTable.Create()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("")
	fmt.Println("Table created: user_logs")

	// Then we populate it with example log data
	log1 := UserLog{
		Email: "*****@*****.**",
		Id:    gocql.TimeUUID(),
		Data:  1,
	}
	err = userLogTable.Insert(log1)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("Log inserted:", log1)

	log2 := UserLog{
		Email: "*****@*****.**",
		Id:    gocql.TimeUUID(),
		Data:  2,
	}
	err = userLogTable.Insert(log2)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("Log inserted:", log2)

	log3 := UserLog{
		Email: "*****@*****.**",
		Id:    gocql.TimeUUID(),
		Data:  3,
	}
	err = userLogTable.Insert(log3)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("Log inserted:", log3)

	log4 := UserLog{
		Email: "*****@*****.**",
		Id:    gocql.TimeUUID(),
		Data:  4,
	}
	err = userLogTable.Insert(log4)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("Log inserted:", log4)

	// Now finally lets do some range queries on our new table.
	rowset, err = userLogTable.Range().Fetch() // If we don't specify any ids in Range(ids...), then it will do a full Scan of the table
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("")
	fmt.Println("Fetching all user logs:")
	for _, userLog := range rowset.([]*UserLog) {
		//userLog := row.(*UserLog)
		fmt.Println("UserLog: ", userLog)
	}

	rowset, err = userLogTable.Range("*****@*****.**").Fetch() // In this case we filter the result by row key and scan over all log items
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("")
	fmt.Println("Fetching all user logs by user [email protected]:")
	for _, userLog := range rowset.([]*UserLog) {
		// userLog := row.(*UserLog)
		fmt.Println("UserLog: ", userLog)
	}

	rowset, err = userLogTable.Range("*****@*****.**").MoreThanOrEqual("id", log3.Id).OrderBy("id DESC").Fetch()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("")
	fmt.Println("Fetching all user logs by user [email protected], where id >= log3.Id:")
	for _, userLog := range rowset.([]*UserLog) {
		//userLog := row.(*UserLog)
		fmt.Println("UserLog: ", userLog)
	}

	rowset, err = userLogTable.Range("*****@*****.**").MoreThan("id", log3.Id).Fetch()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("")
	fmt.Println("Fetching all user logs by user [email protected], where id > log3.Id:")
	for _, userLog := range rowset.([]*UserLog) {
		//userLog := row.(*UserLog)
		fmt.Println("UserLog: ", userLog)
	}

	rowset, err = userLogTable.Range("*****@*****.**").LessThan("id", log3.Id).Limit(10).Fetch()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("")
	fmt.Println("Fetching all user logs by user [email protected], where id < log3.Id:")
	for _, userLog := range rowset.([]*UserLog) {
		// userLog := row.(*UserLog)
		fmt.Println("UserLog: ", userLog)
	}

	// Lets clean up after ourselves by dropping the keyspace.
	keyspace.Drop()
	fmt.Println("")
	fmt.Println("Keyspace dropped")

}
コード例 #3
0
ファイル: base.go プロジェクト: apaoww/gocqltable
func main() {

	// Generic initialization of gocql
	c := gocql.NewCluster("127.0.0.1")
	s, err := c.CreateSession()
	if err != nil {
		log.Fatalln("Unable to open up a session with the Cassandra database (err=" + err.Error() + ")")
	}

	// Tell gocqltable to use this session object as the default for new objects
	gocqltable.SetDefaultSession(s)
	fmt.Println("Gocql session setup complete")

	// Now we're ready to create our first keyspace. We start by getting a keyspace object
	keyspace := gocqltable.NewKeyspace("gocqltable_test")

	// Now lets create that in the database using the simple strategy and durable writes (true)
	err = keyspace.Create(map[string]interface{}{
		"class":              "SimpleStrategy",
		"replication_factor": 1,
	}, true)
	if err != nil { // If something went wrong we print the error and quit.
		log.Fatalln(err)
	}
	fmt.Println("Keyspace created")

	// Now that we have a very own keyspace to play with, lets create our first table.

	// First we need a Row-object to base the table on. It will later be passed to the table wrapper
	// to be used for returning row-objects as the answer to fetch requests.
	type User struct {
		Email    string // Our primary key
		Password string
		Active   bool
		Created  time.Time
	}

	// Let's define and instantiate a table object for our user table
	userTable := struct {
		gocqltable.Table
	}{
		keyspace.NewTable(
			"users",           // The table name
			[]string{"email"}, // Row keys
			nil,               // Range keys
			User{},            // We pass an instance of the user struct that will be used as a type template during fetches.
		),
	}

	// Lets create this table in our cassandra database
	err = userTable.Create()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("")
	fmt.Println("Table created: users")

	// Now that we have a keyspace with a table in it: lets make a few rows! Notice that this is the base example, it uses CQL (not ORM)
	// for database interactions such as INSERT/SELECT/UPDATE/DELETE.
	err = userTable.Query("INSERT INTO gocqltable_test.users (email, password, active, created) VALUES (?, ?, ?, ?)", "*****@*****.**", "123456", true, time.Now().UTC()).Exec()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("User inserted: [email protected]")

	err = userTable.Query("INSERT INTO gocqltable_test.users (email, password, active, created) VALUES (?, ?, ?, ?)", "*****@*****.**", "123456", true, time.Now().UTC()).Exec()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("User inserted: [email protected]")

	err = userTable.Query("INSERT INTO gocqltable_test.users (email, password, active, created) VALUES (?, ?, ?, ?)", "*****@*****.**", "123456", true, time.Now().UTC()).Exec()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("User inserted: [email protected]")

	// With our database filled up with users, lets query it and print out the results.
	iter := userTable.Query("SELECT * FROM gocqltable_test.users").Fetch()
	fmt.Println("")
	fmt.Println("Fetched all from users:")
	for row := range iter.Range() {
		user := row.(*User)        // Our row variable is a pointer to "interface{}", and here we type assert it to a pointer to "User"
		fmt.Println("User:"******"*****@*****.**").FetchRow()
	if err != nil {
		log.Fatalln(err)
	}
	user := row.(*User)
	fmt.Println("")
	fmt.Println("Fetched single row by email: ", user)

	// Lets clean up after ourselves by dropping the keyspace.
	keyspace.Drop()
	fmt.Println("")
	fmt.Println("Keyspace dropped")

}
コード例 #4
0
ファイル: gocqltest.go プロジェクト: trihatmaja/pricewatch
func main() {

	// Generic initialization of gocql
	c := gocql.NewCluster("192.168.1.54")
	s, err := c.CreateSession()
	if err != nil {
		log.Fatalln("Unable to open up a session with the Cassandra database (err=" + err.Error() + ")")
	}

	// Tell gocqltable to use this session object as the default for new objects
	gocqltable.SetDefaultSession(s)

	// Now we're ready to create our first keyspace. We start by getting a keyspace object
	keyspace := gocqltable.NewKeyspace("gocqltable_test")
	// Now lets create that in the database using the simple strategy and durable writes (true)
	// err = keyspace.Create(map[string]interface{}{
	// 	"class":              "SimpleStrategy",
	// 	"replication_factor": 1,
	// }, true)
	// if err != nil { // If something went wrong we print the error and quit.
	// 	log.Fatalln(err)
	// }

	// Now that we have a very own keyspace to play with, lets create our first table.

	// First we need a Row-object to base the table on. It will later be passed to the table wrapper
	// to be used for returning row-objects as the answer to fetch requests.
	type User struct {
		Email    string // Our primary key
		Password string `password`     // Use Tags to rename fields
		Active   bool   `cql:"active"` // If there are multiple tags, use `cql:""` to specify what the table column will be
		Created  time.Time
	}

	// Let's define and instantiate a table object for our user table
	userTable := struct {
		recipes.CRUD // If you looked at the base example first, notice we replaced this line with the recipe
	}{
		recipes.CRUD{ // Here we didn't replace, but rather wrapped the table object in our recipe, effectively adding more methods to the end API
			keyspace.NewTable(
				"users",           // The table name
				[]string{"email"}, // Row keys
				nil,               // Range keys
				User{},            // We pass an instance of the user struct that will be used as a type template during fetches.
			),
		},
	}

	// // Lets create this table in our cassandra database
	// err = userTable.Create()
	// if err != nil {
	// 	log.Fatalln(err)
	// }

	// Now that we have a keyspace with a table in it: lets make a few rows! In the base example we had to write out the CQL manually, this time
	// around, however, we can insert entire User objects.

	// Lets instantiate a user object, set its values and insert it
	user1 := User{
		Email:    "*****@*****.**",
		Password: "******",
		Active:   true,
		Created:  time.Now().UTC(),
	}
	err = userTable.Insert(user1)
	if err != nil {
		log.Fatalln(err)
	}

	// With our database filled up with users, lets query it and print out the results (containing all users in the database).
	rowset, err := userTable.List() // Our rowset variable is a "interface{}", and here we type assert it to a slice of pointers to "User"
	for _, user := range rowset.([]*User) {
		fmt.Println(user)
	}
	if err != nil {
		log.Fatalln(err)
	}

	// You can also fetch a single row, obviously
	row, err := userTable.Get("*****@*****.**")
	if err != nil {
		log.Fatalln(err)
	}
	user := row.(*User)

	// Lets update this user by changing his password
	user.Password = "******"
	err = userTable.Update(user)
	if err != nil {
		log.Fatalln(err)
	}

	// Lets delete user [email protected]
	err = userTable.Delete(user)
	if err != nil {
		log.Fatalln(err)
	}

	// Lets clean up after ourselves by dropping the keyspace.
	keyspace.Drop()
}