select_object.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package sample
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. // SelectObjectSample shows how to get data from csv/json object by sql
  8. func SelectObjectSample() {
  9. // Create a bucket
  10. bucket, err := GetTestBucket(bucketName)
  11. if err != nil {
  12. HandleError(err)
  13. }
  14. //
  15. // Create a Csv object
  16. //
  17. err = bucket.PutObjectFromFile(objectKey, localCsvFile)
  18. if err != nil {
  19. HandleError(err)
  20. }
  21. // Create Csv Meta
  22. csvMeta := oss.CsvMetaRequest{}
  23. ret, err := bucket.CreateSelectCsvObjectMeta(objectKey, csvMeta)
  24. if err != nil {
  25. HandleError(err)
  26. }
  27. fmt.Println("csv file meta:", ret)
  28. // case 1: Isn't NULL
  29. selReq := oss.SelectRequest{}
  30. selReq.Expression = "select Year, StateAbbr, CityName, PopulationCount from ossobject where CityName != ''"
  31. selReq.InputSerializationSelect.CsvBodyInput.FileHeaderInfo = "Use"
  32. body, err := bucket.SelectObject(objectKey, selReq)
  33. if err != nil {
  34. HandleError(err)
  35. }
  36. defer body.Close()
  37. databyte, err := ioutil.ReadAll(body)
  38. if err != nil {
  39. HandleError(err)
  40. }
  41. fmt.Println("some data in SelectCSVObject result:", string(databyte[:9]))
  42. // case 2: Like
  43. selReq = oss.SelectRequest{}
  44. selReq.Expression = "select Year, StateAbbr, CityName, Short_Question_Text from ossobject where Measure like '%blood pressure%Years'"
  45. selReq.InputSerializationSelect.CsvBodyInput.FileHeaderInfo = "Use"
  46. body, err = bucket.SelectObject(objectKey, selReq)
  47. if err != nil {
  48. HandleError(err)
  49. }
  50. defer body.Close()
  51. databyte, err = ioutil.ReadAll(body)
  52. if err != nil {
  53. HandleError(err)
  54. }
  55. fmt.Println("some data in SelectCSVObject result:", string(databyte[:9]))
  56. // delete object
  57. err = bucket.DeleteObject(objectKey)
  58. if err != nil {
  59. HandleError(err)
  60. }
  61. //
  62. // Create a LINES json object
  63. //
  64. err = bucket.PutObjectFromFile(objectKey, localJSONLinesFile)
  65. if err != nil {
  66. HandleError(err)
  67. }
  68. // Create LINES JSON Meta
  69. jsonMeta := oss.JsonMetaRequest{
  70. InputSerialization: oss.InputSerialization {
  71. JSON: oss.JSON {
  72. JSONType:"LINES",
  73. },
  74. },
  75. }
  76. restSt, err := bucket.CreateSelectJsonObjectMeta(objectKey, jsonMeta)
  77. if err != nil {
  78. HandleError(err)
  79. }
  80. fmt.Println("csv json meta:", restSt)
  81. // case 1: sql where A=B
  82. selReq = oss.SelectRequest{}
  83. selReq.Expression = "select * from ossobject where party = 'Democrat'"
  84. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  85. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
  86. body, err = bucket.SelectObject(objectKey, selReq)
  87. if err != nil {
  88. HandleError(err)
  89. }
  90. defer body.Close()
  91. databyte, err = ioutil.ReadAll(body)
  92. if err != nil {
  93. HandleError(err)
  94. }
  95. fmt.Println("some data in SelectJsonObject result:", string(databyte[:9]))
  96. // case 2: LIKE
  97. selReq = oss.SelectRequest{}
  98. selReq.Expression = "select person.firstname, person.lastname from ossobject where person.birthday like '1959%'"
  99. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  100. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "LINES"
  101. body, err = bucket.SelectObject(objectKey, selReq)
  102. if err != nil {
  103. HandleError(err)
  104. }
  105. defer body.Close()
  106. databyte, err = ioutil.ReadAll(body)
  107. if err != nil {
  108. HandleError(err)
  109. }
  110. fmt.Println("some data in SelectJsonObject result:", string(databyte[:9]))
  111. // delete object
  112. err = bucket.DeleteObject(objectKey)
  113. if err != nil {
  114. HandleError(err)
  115. }
  116. //
  117. // Create a Document json object
  118. //
  119. err = bucket.PutObjectFromFile(objectKey, localJSONFile)
  120. if err != nil {
  121. HandleError(err)
  122. }
  123. // case 1: int avg, max, min
  124. selReq = oss.SelectRequest{}
  125. selReq.Expression = `
  126. select
  127. avg(cast(person.cspanid as int)), max(cast(person.cspanid as int)),
  128. min(cast(person.cspanid as int))
  129. from
  130. ossobject.objects[*]
  131. where
  132. person.cspanid = 1011723
  133. `
  134. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  135. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "Document"
  136. body, err = bucket.SelectObject(objectKey, selReq)
  137. if err != nil {
  138. HandleError(err)
  139. }
  140. defer body.Close()
  141. databyte, err = ioutil.ReadAll(body)
  142. if err != nil {
  143. HandleError(err)
  144. }
  145. fmt.Println("data:", string(databyte))
  146. // case 2: Concat
  147. selReq = oss.SelectRequest{}
  148. selReq.Expression = `
  149. select
  150. person
  151. from
  152. ossobject.objects[*]
  153. where
  154. (person.firstname || person.lastname) = 'JohnKennedy'
  155. `
  156. selReq.OutputSerializationSelect.JsonBodyOutput.RecordDelimiter = ","
  157. selReq.InputSerializationSelect.JsonBodyInput.JSONType = "Document"
  158. body, err = bucket.SelectObject(objectKey, selReq)
  159. if err != nil {
  160. HandleError(err)
  161. }
  162. defer body.Close()
  163. databyte, err = ioutil.ReadAll(body)
  164. if err != nil {
  165. HandleError(err)
  166. }
  167. fmt.Println("some data in SelectJsonObject result:", string(databyte[:9]))
  168. // Delete the object and bucket
  169. err = DeleteTestBucketAndObject(bucketName)
  170. if err != nil {
  171. HandleError(err)
  172. }
  173. fmt.Println("SelectObjectSample completed")
  174. }