• Stars
    star
    868
  • Rank 50,506 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 7 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

All-In-One: CQL query builder, ORM and migration tool

🚀 GocqlX GoDoc Go Report Card Build Status

GocqlX makes working with Scylla easy and less error-prone. It’s inspired by Sqlx, a tool for working with SQL databases, but it goes beyond what Sqlx provides.

Features

  • Binding query parameters from struct fields, map, or both
  • Scanning query results into structs based on field names
  • Convenient functions for common tasks such as loading a single row into a struct or all rows into a slice (list) of structs
  • Making any struct a UDT without implementing marshalling functions
  • GocqlX is fast. Its performance is comparable to raw driver. You can find some benchmarks here.

Subpackages provide additional functionality:

Installation

    go get -u github.com/scylladb/gocqlx/v2

Getting started

Wrap gocql Session:

// Create gocql cluster.
cluster := gocql.NewCluster(hosts...)
// Wrap session on creation, gocqlx session embeds gocql.Session pointer. 
session, err := gocqlx.WrapSession(cluster.CreateSession())
if err != nil {
	t.Fatal(err)
}

Specify table model:

// metadata specifies table name and columns it must be in sync with schema.
var personMetadata = table.Metadata{
	Name:    "person",
	Columns: []string{"first_name", "last_name", "email"},
	PartKey: []string{"first_name"},
	SortKey: []string{"last_name"},
}

// personTable allows for simple CRUD operations based on personMetadata.
var personTable = table.New(personMetadata)

// Person represents a row in person table.
// Field names are converted to camel case by default, no need to add special tags.
// A field will not be persisted by adding the `db:"-"` tag or making it unexported.
type Person struct {
	FirstName string
	LastName  string
	Email     []string
	HairColor string `db:"-"`  // exported and skipped
	eyeColor  string           // unexported also skipped
}

Bind data from a struct and insert a row:

p := Person{
	"Michał",
	"Matczuk",
	[]string{"[email protected]"},
	"red",    // not persisted
	"hazel"   // not persisted
}
q := session.Query(personTable.Insert()).BindStruct(p)
if err := q.ExecRelease(); err != nil {
	t.Fatal(err)
}

Load a single row to a struct:

p := Person{
	"Michał",
	"Matczuk",
	nil, // no email
}
q := session.Query(personTable.Get()).BindStruct(p)
if err := q.GetRelease(&p); err != nil {
	t.Fatal(err)
}
t.Log(p)
// stdout: {MichaÅ‚ Matczuk [[email protected]]}

Load all rows in to a slice:

var people []Person
q := session.Query(personTable.Select()).BindMap(qb.M{"first_name": "Michał"})
if err := q.SelectRelease(&people); err != nil {
	t.Fatal(err)
}
t.Log(people)
// stdout: [{MichaÅ‚ Matczuk [[email protected]]}]

Generating table metadata with schemagen

Installation

go get -u "github.com/scylladb/gocqlx/v2/cmd/schemagen"

Usage:

$GOBIN/schemagen [flags]

Flags:
  -cluster string
    	a comma-separated list of host:port tuples (default "127.0.0.1")
  -keyspace string
    	keyspace to inspect (required)
  -output string
    	the name of the folder to output to (default "models")
  -pkgname string
    	the name you wish to assign to your generated package (default "models") 

Example:

Running the following command for examples keyspace:

$GOBIN/schemagen -cluster="127.0.0.1:9042" -keyspace="examples" -output="models" -pkgname="models"

Generates models/models.go as follows:

// Code generated by "gocqlx/cmd/schemagen"; DO NOT EDIT.

package models

import "github.com/scylladb/gocqlx/v2/table"

// Table models.
var (
	Playlists = table.New(table.Metadata{
		Name: "playlists",
		Columns: []string{
			"album",
			"artist",
			"id",
			"song_id",
			"title",
		},
		PartKey: []string{
			"id",
		},
		SortKey: []string{
			"title",
			"album",
			"artist",
		},
	})

	Songs = table.New(table.Metadata{
		Name: "songs",
		Columns: []string{
			"album",
			"artist",
			"data",
			"id",
			"tags",
			"title",
		},
		PartKey: []string{
			"id",
		},
		SortKey: []string{},
	})
)

Examples

You can find lots of examples in example_test.go.

Go and run them locally:

make run-scylla
make run-examples

Performance

GocqlX performance is comparable to the raw gocql driver. Below benchmark results running on my laptop.

BenchmarkBaseGocqlInsert            2392            427491 ns/op            7804 B/op         39 allocs/op
BenchmarkGocqlxInsert               2479            435995 ns/op            7803 B/op         39 allocs/op
BenchmarkBaseGocqlGet               2853            452384 ns/op            7309 B/op         35 allocs/op
BenchmarkGocqlxGet                  2706            442645 ns/op            7646 B/op         38 allocs/op
BenchmarkBaseGocqlSelect             747           1664365 ns/op           49415 B/op        927 allocs/op
BenchmarkGocqlxSelect                667           1877859 ns/op           42521 B/op        932 allocs/op

See the benchmark in benchmark_test.go.

License

Copyright (C) 2017 ScyllaDB

This project is distributed under the Apache 2.0 license. See the LICENSE file for details. It contains software from:

Apache®, Apache Cassandra® are either registered trademarks or trademarks of the Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.

GitHub star is always appreciated!

More Repositories

1

scylladb

NoSQL data store using the seastar framework, compatible with Apache Cassandra
C++
12,344
star
2

seastar

High performance server-side application framework
C++
7,604
star
3

go-set

Type-safe, zero-allocation sets for Go
Go
804
star
4

scylla-rust-driver

Async CQL driver for Rust, optimized for Scylla!
Rust
517
star
5

scylla-operator

The Kubernetes Operator for ScyllaDB
Go
312
star
6

charybdefs

ScyllaDB fault injection filesystem
C++
241
star
7

scylla-code-samples

Code samples for working with ScyllaDB
Python
222
star
8

diskplorer

Python
210
star
9

scylla-monitoring

Simple monitoring of Scylla with Grafana
Shell
203
star
10

dpdk

Mirror of Data Plane Development Kit, git://dpdk.org/dpdk (http://dpdk.org)
C++
173
star
11

scylla-go-driver

Experimental, high performance Scylla Driver, University of Warsaw students' project
Go
146
star
12

termtables

Fork of github.com/apcera/termtables
Go
55
star
13

scylla-migrator

Migrate data extract using Spark to Scylla, normally from Cassandra
Scala
50
star
14

scylla-cluster-tests

Tests for Scylla Clusters
Python
50
star
15

scylla-tools-java

Apache Cassandra, supplying tools for Scylla
Java
49
star
16

gaming-leaderboard-demo

Learn how to use ScyllaDB to generate a monstrously fast leadearboard for your application!
TypeScript
48
star
17

scylla-manager

The Scylla Manager
Go
42
star
18

kafka-connect-scylladb

Kafka Connect Scylladb Sink
Java
42
star
19

scylla-ansible-roles

Ansible roles for deploying and managing Scylla, Scylla-Manager and Scylla-Monitoring
Python
39
star
20

scylla-bench

Go
39
star
21

scylla-cdc-source-connector

A Kafka source connector capturing Scylla CDC changes
Java
38
star
22

scylla-cloud-getting-started

Rust
38
star
23

scylla-cdc-go

Go
37
star
24

care-pet

Care Pet IoT ScyllaDB example
JavaScript
37
star
25

cassandra-test-and-deploy

Python
30
star
26

scylla-cdc-rust

Rust
27
star
27

gemini

Test data integrity by comparing against an Oracle running in parallel
Go
27
star
28

scylla-seastar

Stable-branch fork of seatar, for scylla use
C++
26
star
29

scylla-jmx

Scylla JMX proxy
Java
25
star
30

go-reflectx

Go reflection library to find struct field by its tag
Go
24
star
31

scylla-graphite-monitoring

Python
20
star
32

scylla-cdc-java

Java
20
star
33

scylla-ccm

Cassandra Cluster Manager, modified for Scylla
Python
18
star
34

scylla-machine-image

Python
17
star
35

scylla-grafana-datasource

A grafana backend plugin for ScyllaDB
TypeScript
17
star
36

scylladb-feature-store

A feature store sample application built with ScyllaDB
Jupyter Notebook
14
star
37

jepsen

scylladb jepsen clone
Clojure
14
star
38

alternator-load-balancing

Various tricks, scripts, and libraries, for load balancing multiple Alternator nodes
Java
14
star
39

sphinx-scylladb-theme

The documentation toolchain for Scylla projects.
Python
12
star
40

cpp-rust-driver

API-compatible rewrite of https://github.com/scylladb/cpp-driver as a wrapper for Rust driver.
C++
11
star
41

go-sshtools

Go SSH client wrapper and other tools
Go
11
star
42

video-streaming

Video streaming sample app with NextJs + ScyllaDB
TypeScript
10
star
43

terraform-provider-scylladbcloud

Terraform provider for ScyllaDB Cloud.
Go
10
star
44

scylla-rust-udf

Rust
10
star
45

scylla-cqlsh

A fork of the cqlsh code
Python
9
star
46

sstable-tools

Python
7
star
47

scylla-ami

Python
7
star
48

1m-ops-demo

1 million operations/second ScyllaDB demo
HCL
6
star
49

scylla-swagger-ui

Private fork of https://github.com/swagger-api/swagger-ui for scylla
JavaScript
6
star
50

scylla-python3

Python
6
star
51

scylla-stress-orchestrator

Python
5
star
52

cql-stress

Rust
5
star
53

scylla-price-calculator

Compare Scylla Cloud cost to estimations of other DBaaS services cost. Data is served with no guarantees. Please validate with each DBaaS price list per region.
Vue
5
star
54

argus

Svelte
4
star
55

go-log

Wrapper over Uber Zap
Go
4
star
56

scylla-api-client

Scylla CLI Python module and command line utility
Python
4
star
57

jenkins-slave

Jenkins slaves as docker images
4
star
58

k8s-local-volume-provisioner

Go
3
star
59

python-driver-matrix

Groovy
3
star
60

seastar-website

Seastar static website
HTML
3
star
61

scylladb-web-install

Shell
3
star
62

scylla-java-driver-matrix

Groovy
3
star
63

scylla-doc-issues

Repository for reporting issues about Scylla documentation (Deprecated)
2
star
64

scylla-artifact-tests

Tests for the scylla distro packages
Python
2
star
65

scylla-cluster-autoscaler

Go
2
star
66

scylla-operator-images

This repository contains the source for shared images used by Scylla Operator projects. We do not provide any support or compatibility guaranties for these images as their sole purpose is only to provide a common base in our projects.
Dockerfile
2
star
67

scylla-cpp-driver-matrix

Groovy
1
star
68

scylla-build-dependencies-docker

Dockerfiles for generating images with scylla build dependencies
1
star
69

scylladb-cloud-doc-issues

A repo for Scylla Cloud docs issues
1
star