Skip to content

Commit

Permalink
add test cli
Browse files Browse the repository at this point in the history
  • Loading branch information
intamyuto committed Jan 3, 2019
1 parent 701e831 commit 3844210
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
71 changes: 71 additions & 0 deletions cmd/impala/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"flag"
"fmt"
"log"
"time"

impalathing "github.com/bippio/go-impala"
)

func main() {

var host string
flag.StringVar(&host, "host", "", "impalad hostname")

var port int
flag.IntVar(&port, "p", 21000, "impala daemon port")

opts := impalathing.DefaultOptions
flag.BoolVar(&opts.UseLDAP, "l", false, "use ldap authentication")
flag.StringVar(&opts.Username, "username", "", "ldap usename")
flag.StringVar(&opts.Password, "password", "", "ldap password")

flag.Parse()

if opts.UseLDAP {
if opts.Username == "" {
log.Fatalf("Please specify username with --username flag")
}
if opts.Password == "" {
log.Fatalf("Please specify password with --password flag")
}
}

q := flag.Arg(0)

con, err := impalathing.Connect(host, port, &opts)

if err != nil {
log.Fatalf("Error connecting: %v", err)
}

query, err := con.Query(q)

if err != nil {
log.Fatal(err)
}

startTime := time.Now()
results := query.FetchAll()
fmt.Printf("\nFetch %d rows(s) in %.2fs\n", len(results), time.Duration(time.Since(startTime)).Seconds())

var columns []string
for _, col := range query.Schema() {
columns = append(columns, col.Name)

fmt.Printf("%25s |", fmt.Sprintf("%s (%s)", col.Name, col.Type))
}
fmt.Println()
fmt.Println("-----------------------------------------------------")

for _, row := range results {
for _, col := range columns {
fmt.Printf("%25v |", row[col])
}
fmt.Println()
}

log.Printf("Fetch %d rows(s) in %.2fs", len(results), time.Duration(time.Since(startTime)).Seconds())
}
10 changes: 10 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package impalathing

import (
"errors"
"fmt"

"git.apache.org/thrift.git/lib/go/thrift"
Expand Down Expand Up @@ -38,6 +39,15 @@ func Connect(host string, port int, options *Options) (*Connection, error) {

var transport thrift.TTransport
if options.UseLDAP {

if options.Username == "" {
return nil, errors.New("Please provide username for LDAP auth")
}

if options.Password == "" {
return nil, errors.New("Please provide password for LDAP auth")
}

transport, err = sasl.NewTSaslTransport(socket, &sasl.Options{
Host: host,
Username: options.Username,
Expand Down
29 changes: 18 additions & 11 deletions rowset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import (
impala "github.com/bippio/go-impala/services/impalaservice"
)

type ColumnSchema struct {
Name string
Type string
}

type rowSet struct {
client *impala.ImpalaServiceClient
handle *beeswax.QueryHandle
options *Options

// columns []*tcliservice.TColumnDesc
columnNames []string
columns []*ColumnSchema

offset int
rowSet *beeswax.Results
Expand All @@ -35,7 +39,7 @@ type rowSet struct {
// have a valid thrift client, and the serialized Handle()
// from the prior operation.
type RowSet interface {
Columns() []string
Schema() []*ColumnSchema
Next() bool
Scan(dest ...interface{}) error
Poll() (*Status, error)
Expand All @@ -52,7 +56,7 @@ type Status struct {
}

func newRowSet(client *impala.ImpalaServiceClient, handle *beeswax.QueryHandle, options *Options) RowSet {
return &rowSet{client: client, handle: handle, options: options, columnNames: nil, offset: 0, rowSet: nil,
return &rowSet{client: client, handle: handle, options: options, columns: nil, offset: 0, rowSet: nil,
hasMore: true, ready: false, metadata: nil, nextRow: nil}
}

Expand Down Expand Up @@ -143,9 +147,12 @@ func (r *rowSet) Next() bool {
if err != nil {
log.Printf("GetResultsMetadata failed: %v\n", err)
}
}
if len(r.columnNames) == 0 {
r.columnNames = resp.Columns

if len(r.columns) == 0 {
for _, fschema := range r.metadata.Schema.FieldSchemas {
r.columns = append(r.columns, &ColumnSchema{Name: fschema.Name, Type: fschema.Type})
}
}
}

r.hasMore = resp.HasMore
Expand Down Expand Up @@ -260,16 +267,16 @@ func (r *rowSet) FetchAll() []map[string]interface{} {
return response
}

// Returns the names of the columns for the given operation,
// Returns the name and type of the columns for the given operation,
// blocking if necessary until the information is available.
func (r *rowSet) Columns() []string {
if r.columnNames == nil {
func (r *rowSet) Schema() []*ColumnSchema {
if r.columns == nil {
if err := r.waitForSuccess(); err != nil {
return nil
}
}

return r.columnNames
return r.columns
}

// MapScan scans a single Row into the dest map[string]interface{}.
Expand Down

0 comments on commit 3844210

Please sign in to comment.