forked from bippio/go-impala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumerateDB.go
executable file
·83 lines (66 loc) · 1.56 KB
/
enumerateDB.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
// Simple program to list databases and the tables
import (
"context"
"database/sql"
"log"
impala "github.com/bippio/go-impala"
)
func main() {
opts := impala.DefaultOptions
opts.Host = "<impala host>"
opts.Port = "21050"
// enable LDAP authentication:
opts.UseLDAP = true
opts.Username = "<ldap username>"
opts.Password = "<ldap password>"
// enable TLS
opts.UseTLS = true
opts.CACertPath = "/path/to/cacert"
connector := impala.NewConnector(&opts)
db := sql.OpenDB(connector)
defer db.Close()
ctx := context.Background()
rows, err := db.QueryContext(ctx, "SHOW DATABASES")
if err != nil {
log.Fatal(err)
}
r := struct {
name string
comment string
}{}
databases := make([]string, 0) // databases will contain all the DBs to enumerate later
for rows.Next() {
if err := rows.Scan(&r.name, &r.comment); err != nil {
log.Fatal(err)
}
databases = append(databases, r.name)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
log.Println("List of Databases", databases)
stmt, err := db.PrepareContext(ctx, "SHOW TABLES IN @p1")
if err != nil {
log.Fatal(err)
}
tbl := struct {
name string
}{}
for _, d := range databases {
rows, err := stmt.QueryContext(ctx, d)
if err != nil {
log.Printf("error in querying database %s: %s", d, err.Error())
continue
}
tables := make([]string, 0)
for rows.Next() {
if err := rows.Scan(&tbl.name); err != nil {
log.Println(err)
continue
}
tables = append(tables, tbl.name)
}
log.Printf("List of Tables in Database %s: %v\n", d, tables)
}
}