Created
October 7, 2014 23:08
-
-
Save ianmac45/00fdfdb620e9a54866f7 to your computer and use it in GitHub Desktop.
example for parsing a file & inserting into a database, using golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
// "flag" | |
"fmt" | |
"os" | |
"strings" | |
"strconv" | |
"database/sql" | |
"compress/gzip" | |
"io" | |
// "io/ioutil" | |
"bufio" | |
) | |
import _ "github.com/go-sql-driver/mysql" | |
func process(db *sql.DB, ioReader io.Reader) { | |
cmd, err := db.Prepare("insert into table (...) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") | |
if err != nil { panic(err) } | |
scanner := bufio.NewScanner(ioReader) | |
scanner.Scan() // first line is header | |
for scanner.Scan() { | |
parts := strings.Split(scanner.Text(), "|") | |
tac, _ := strconv.Atoi(parts[0]) | |
_, err := cmd.Exec(tac, parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7], parts[8], parts[9], parts[10], parts[11], parts[12], parts[13], parts[14], parts[15], parts[16]) | |
if err != nil { fmt.Println(err) } | |
} | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("must provide path to data file") | |
return | |
} | |
path := os.Args[1] | |
reader, err := os.Open(path) | |
defer reader.Close() | |
if err != nil { panic(err) } | |
fmt.Println("(re)creating database tables") | |
db, err := sql.Open("mysql", ":@unix(/opt/local/var/run/mariadb/mysqld.sock)/table?parseTime=true") | |
// db, err := sql.Open("mysql", ":@192.168.2.107/table") | |
defer db.Close() | |
if err != nil { panic(err) } | |
// sql, _ := ioutil.ReadFile("db.sql") | |
// db.Exec(string(sql)) | |
db.Exec("delete from table") | |
fmt.Println("processing file") | |
if !strings.HasSuffix(path, ".gz") { | |
process(db, reader) | |
} else { | |
gzReader, _ := gzip.NewReader(reader) | |
defer gzReader.Close() | |
fmt.Println(" --> decompressing gzip") | |
process(db, gzReader) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment