123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- package oss
- import (
- . "gopkg.in/check.v1"
- "os"
- "io/ioutil"
- "strings"
- "net/http"
- )
- type OssSelectJsonSuite struct {
- client *Client
- bucket *Bucket
- }
- var _ = Suite(&OssSelectJsonSuite{})
- func (s *OssSelectJsonSuite) SetUpSuite(c *C) {
- client, err := New(endpoint, accessID, accessKey)
- c.Assert(err, IsNil)
- s.client = client
- s.client.Config.LogLevel = Error // Debug
- err = s.client.CreateBucket(bucketName)
- c.Assert(err, IsNil)
- bucket, err := s.client.Bucket(bucketName)
- c.Assert(err, IsNil)
- s.bucket = bucket
- testLogger.Println("test select json started")
- }
- func(s *OssSelectJsonSuite) TearDownSuite(c *C){
- // Delete objects
- marker := Marker("")
- for {
- lor, err := s.bucket.ListObjects(marker)
- c.Assert(err, IsNil)
- for _, object := range lor.Objects {
- err = s.bucket.DeleteObject(object.Key)
- c.Assert(err, IsNil)
- }
- marker = Marker(lor.NextMarker)
- if !lor.IsTruncated {
- break
- }
- }
- err := s.client.DeleteBucket(bucketName)
- c.Assert(err, IsNil)
- testLogger.Println("test select json completed")
- }
- func (s *OssSelectJsonSuite) SetUpTest(c *C) {
- testLogger.Println("test func", c.TestName(), "start")
- }
- func (s *OssSelectJsonSuite) TearDownTest(c *C) {
- testLogger.Println("test func", c.TestName(), "succeed")
- }
- func (s *OssSelectJsonSuite) TestCreateSelectJsonObjectMeta(c *C){
- key := "sample_json_lines.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
- c.Assert(err, IsNil)
- jsonMeta := JsonMetaRequest{
- InputSerialization: InputSerialization {
- JSON: JSON {
- JSONType:"LINES",
- },
- },
- }
- res,err := s.bucket.CreateSelectJsonObjectMeta(key, jsonMeta)
- c.Assert(err, IsNil)
- c.Assert(res.RowsCount, Equals, int64(100))
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonDocument(c *C){
- key := "sample_json.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.Expression = "select * from ossobject.objects[*] where party = 'Democrat'"
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "DOCUMENT"
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- var responseHeader http.Header
- body, err := s.bucket.SelectObject(key, selReq, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- defer body.Close()
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- p := make([]byte, 512)
- n, err := body.Read(p)
- c.Assert(err, IsNil)
- c.Assert(n, Equals, 512)
- p1 := make([]byte, 3)
- _, err = body.Read(p1)
- c.Assert(err, IsNil)
- rets, err := ioutil.ReadAll(body)
- c.Assert(err,IsNil)
- str,err := readJsonDocument("../sample/sample_json.json")
- c.Assert(err, IsNil)
- c.Assert(string(p) + string(p1) + string(rets), Equals, str)
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonLines(c *C) {
- key := "sample_json_lines.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.Expression = "select * from ossobject where party = 'Democrat'"
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
- var responseHeader http.Header
- body, err := s.bucket.SelectObject(key, selReq, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- defer body.Close()
-
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- rets, err := ioutil.ReadAll(body)
- c.Assert(err,IsNil)
- str,err := readJsonDocument("../sample/sample_json.json")
- c.Assert(string(rets), Equals, str)
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonLinesIntoFile(c *C) {
- key := "sample_json_lines.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
- c.Assert(err, IsNil)
-
- jsonMeta := JsonMetaRequest{
- InputSerialization: InputSerialization {
- JSON: JSON {
- JSONType:"LINES",
- },
- },
- }
- res,err := s.bucket.CreateSelectJsonObjectMeta(key, jsonMeta)
- c.Assert(err, IsNil)
- c.Assert(res.RowsCount, Equals, int64(100))
- selReq := SelectRequest{}
- selReq.Expression = "select * from ossobject where party = 'Democrat'"
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
- var responseHeader http.Header
- outfile := "sample_json_out.json"
- err = s.bucket.SelectObjectIntoFile(key, outfile, selReq, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- _, err = os.Stat(outfile)
- c.Assert(err,IsNil)
- err = os.Remove(outfile)
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonDocumentIntoFile(c *C) {
- key := "sample_json_lines.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.Expression = "select * from ossobject.objects[*] where party = 'Democrat'"
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "DOCUMENT"
- var responseHeader http.Header
- outfile := "sample_json_out.json"
- err = s.bucket.SelectObjectIntoFile(key, outfile, selReq, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- _, err = os.Stat(outfile)
- c.Assert(err,IsNil)
- err = os.Remove(outfile)
- c.Assert(err, IsNil)
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonLinesLike(c *C) {
- key := "sample_json_lines.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.Expression = "select person.firstname, person.lastname from ossobject where person.birthday like '1959%'"
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
- jsonMeta := JsonMetaRequest{
- InputSerialization: InputSerialization {
- JSON: JSON {
- JSONType:"LINES",
- },
- },
- }
- res,err := s.bucket.CreateSelectJsonObjectMeta(key, jsonMeta)
- c.Assert(err, IsNil)
- c.Assert(res.RowsCount, Equals, int64(100))
- var responseHeader http.Header
- body, err := s.bucket.SelectObject(key, selReq, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- defer body.Close()
-
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- rets, err := ioutil.ReadAll(body)
- c.Assert(err,IsNil)
- str,err := readJsonLinesLike("../sample/sample_json.json")
- c.Assert(string(rets), Equals, str)
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonLinesRange(c *C) {
- key := "sample_json_lines.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
- c.Assert(err, IsNil)
- jsonMeta := JsonMetaRequest{
- InputSerialization: InputSerialization {
- JSON: JSON {
- JSONType:"LINES",
- },
- },
- }
- res,err := s.bucket.CreateSelectJsonObjectMeta(key, jsonMeta)
- c.Assert(err, IsNil)
- c.Assert(res.RowsCount, Equals, int64(100))
- selReq := SelectRequest{}
- selReq.Expression = "select person.firstname as aaa as firstname, person.lastname, extra from ossobject'"
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
- selReq.InputSerializationSelect.JsonBodyInput.Range = "0-1"
-
- var responseHeader http.Header
- body, err := s.bucket.SelectObject(key, selReq, GetResponseHeader(&responseHeader))
- c.Assert(err, IsNil)
- defer body.Close()
-
- requestId := GetRequestId(responseHeader)
- c.Assert(len(requestId) > 0, Equals, true)
- rets, err := ioutil.ReadAll(body)
- c.Assert(err,IsNil)
- str,err := readJsonLinesRange("../sample/sample_json.json", 0, 2)
- c.Assert(string(rets), Equals, str)
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonDocumentIntAggregation(c *C) {
- key := "sample_json.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.Expression = `
- select
- avg(cast(person.cspanid as int)), max(cast(person.cspanid as int)),
- min(cast(person.cspanid as int))
- from
- ossobject.objects[*]
- where
- person.cspanid = 1011723
- `
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "Document"
-
- body, err := s.bucket.SelectObject(key, selReq)
- c.Assert(err, IsNil)
- defer body.Close()
- rets, err := ioutil.ReadAll(body)
- c.Assert(err,IsNil)
- c.Assert(string(rets), Equals, "{\"_1\":1011723,\"_2\":1011723,\"_3\":1011723},")
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonDocumentFloatAggregation(c *C) {
- key := "sample_json.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.Expression = `
- select
- avg(cast(person.cspanid as double)), max(cast(person.cspanid as double)),
- min(cast(person.cspanid as double))
- from
- ossobject.objects[*]
- `
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "Document"
-
- body, err := s.bucket.SelectObject(key, selReq)
- c.Assert(err, IsNil)
- defer body.Close()
- rets, err := ioutil.ReadAll(body)
- c.Assert(err,IsNil)
- testLogger.Println(string(rets))
- // avg, max, min, err := readJsonFloatAggregation("../sample/sample_json.json")
- // fmt.Println(string(rets), "\n", avg, max, min)
- // retsArr := strings.Split(string(rets), ":")
- // s1 := strconv.FormatFloat(avg, 'f', 6, 64) + ","
- // s1 += strconv.FormatFloat(max, 'f', 6, 64) + ","
- // s1 += strconv.FormatFloat(min, 'f', 6, 64) + ","
- // retS := ""
- // l := len(retsArr[1])
- // vv, err := strconv.ParseFloat(retsArr[1][:l-35], 64)
- // c.Assert(err, IsNil)
- // retS += strconv.FormatFloat(vv, 'f', 6, 64) + ","
- // l = len(retsArr[2])
- // vv, err = strconv.ParseFloat(retsArr[2][:l-6], 64)
- // c.Assert(err, IsNil)
- // retS += strconv.FormatFloat(vv, 'f', 6, 64) + ","
- // l = len(retsArr[3])
- // vv, err = strconv.ParseFloat(retsArr[3][:l-2], 64)
- // c.Assert(err, IsNil)
- // retS += strconv.FormatFloat(vv, 'f', 6, 64) + ","
- // c.Assert(retS, Equals, s1)
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonDocumentConcat(c *C) {
- key := "sample_json.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.Expression = `
- select
- person
- from
- ossobject.objects[*]
- where
- (person.firstname || person.lastname) = 'JohnKennedy'
- `
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "Document"
-
- body, err := s.bucket.SelectObject(key, selReq)
- c.Assert(err, IsNil)
- defer body.Close()
- rets, err := ioutil.ReadAll(body)
- c.Assert(err,IsNil)
- str, err := readJsonDocumentConcat("../sample/sample_json.json")
- c.Assert(err,IsNil)
- c.Assert(string(rets), Equals, str)
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonComplicateConcat(c *C) {
- key := "sample_json.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.Expression = `
- select
- person.firstname, person.lastname, congress_numbers
- from
- ossobject
- where
- startdate > '2017-01-01' and
- senator_rank = 'junior' or
- state = 'CA' and
- party = 'Republican'
- `
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
-
- body, err := s.bucket.SelectObject(key, selReq)
- c.Assert(err, IsNil)
- defer body.Close()
- rets, err := ioutil.ReadAll(body)
- c.Assert(err,IsNil)
- str, err := readJsonComplicateConcat("../sample/sample_json.json")
- c.Assert(err,IsNil)
- c.Assert(string(rets), Equals, str)
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonLineInvalidSql(c *C) {
- key := "sample_json.json"
- err := s.bucket.PutObjectFromFile(key, "../sample/sample_json_lines.json")
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
-
- selReq.Expression = `select * from ossobject where avg(cast(person.birthday as int)) > 2016`
- _, err = s.bucket.SelectObject(key, selReq)
- c.Assert(err, NotNil)
- selReq.Expression = ``
- _, err = s.bucket.SelectObject(key, selReq)
- c.Assert(err, NotNil)
- selReq.Expression = `select person.lastname || person.firstname from ossobject`
- _, err = s.bucket.SelectObject(key, selReq)
- c.Assert(err, NotNil)
- selReq.Expression = `select * from ossobject group by person.firstname`
- _, err = s.bucket.SelectObject(key, selReq)
- c.Assert(err, NotNil)
- selReq.Expression = `select * from ossobject order by _1`
- _, err = s.bucket.SelectObject(key, selReq)
- c.Assert(err, NotNil)
- selReq.Expression = `select * from ossobject oss join s3object s3 on oss.CityName = s3.CityName`
- _, err = s.bucket.SelectObject(key, selReq)
- c.Assert(err, NotNil)
- err = s.bucket.PutObjectFromFile(key, "../sample/sample_json.json")
- c.Assert(err, IsNil)
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "DOCUMENT"
- selReq.Expression = `select _1 from ossobject.objects[*]`
- body, err := s.bucket.SelectObject(key, selReq)
- c.Assert(err, IsNil)
- defer body.Close()
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
- func (s *OssSelectJsonSuite) TestSelectJsonParseNumAsString(c *C) {
- key := "sample_json.json"
- content := "{\"a\":123456789.123456789}"
- err := s.bucket.PutObject(key, strings.NewReader(content))
- c.Assert(err, IsNil)
- selReq := SelectRequest{}
- selReq.Expression = `select a from ossobject where cast(a as decimal) = 123456789.1234567890`
- bo := true
- selReq.InputSerializationSelect.JsonBodyInput.ParseJSONNumberAsString = &bo
- selReq.InputSerializationSelect.JsonBodyInput.JSONType = "DOCUMENT"
-
- body, err := s.bucket.SelectObject(key, selReq)
- c.Assert(err, IsNil)
- defer body.Close()
- rets, err := ioutil.ReadAll(body)
- c.Assert(err,IsNil)
- c.Assert(string(rets), Equals, "{\"a\":123456789.123456789}\n")
- err = s.bucket.DeleteObject(key)
- c.Assert(err, IsNil)
- }
|