S
S
Sergio_Hunter2016-10-28 23:32:29
SQL
Sergio_Hunter, 2016-10-28 23:32:29

How to extract BLOB with JSON data from table field and decompress gzip data?

Hello. Need advice on where to start and how to implement correctly. There is a SQL database in which a table with a data field and a BLOB type. You need to extract a BLOB with JSON data from this field and decompress the gzip data (there is data compressed and not in this field) and then parse as JSON. I would be grateful for any help in implementing this code.

package main

import (
  "database/sql"
  "encoding/csv"
  "fmt"
  "log"
  "os"
  _ "github.com/go-sql-driver/mysql"
  _"src/golang.org/x/crypto/acme"
)
func main() {
  db, err := sql.Open("mysql", "user:[email protected](127.0.0.1:port)/database")

  if err != nil {
    panic(err.Error())
  }
  
  rows, err := db.Query(`SELECT field FROM table_1 ORDER BY table_2 LIMIT 10`)
  if err != nil {
    log.Fatal(err)
  }
  defer rows.Close()
  defer db.Close()

  file, err := os.Create("result.csv")
  if err != nil {
    fmt.Println(err)
  }
  defer file.Close()

  var (
    data string
  )

  cont := 0
  writer := csv.NewWriter(file)
  for rows.Next() {
    err := rows.Scan(&data)
    if err != nil {
      log.Fatal(err)
    }

    if cont == 0 {
      var record []string
      record = append(record, "Date")
      writer.Write(record)
      cont++

    }

    var record []string
    record = append(record, data)
    writer.Write(record)
    cont++

  }
  defer writer.Flush()
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
x32net, 2016-10-31
@x32net

var b []byte
rows.Scan(&b)
fmt.Printf("%s\n", b)
r, err := gzip.NewReader(&b)
io.Copy(os.Stdout, r)
r.Close()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question