rebulk.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Before you execute the program, Launch `cqlsh` and execute:
  3. create keyspace example with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
  4. create table example.tweet(timeline text, id UUID, text text, PRIMARY KEY(id));
  5. create index on example.tweet(timeline);
  6. */
  7. package main
  8. import (
  9. "fmt"
  10. "os"
  11. "strconv"
  12. "strings"
  13. "time"
  14. . "git.wecise.com/wecise/odbserver/odb"
  15. "git.wecise.com/wecise/odbserver/odb/notify"
  16. "git.wecise.com/wecise/odbserver/odb/test"
  17. "gitee.com/wecisecode/util/logger"
  18. )
  19. func main() {
  20. // connect to the cluster
  21. //cluster := gocql.NewCluster("192.168.40.14")
  22. keyspace := "demo"
  23. if len(os.Args) > 1 {
  24. keyspace = os.Args[1]
  25. }
  26. option := &Option{Cache: notify.CacheAll, Keyspace: keyspace}
  27. g, err := test.NewG(option)
  28. if err != nil {
  29. logger.Error(err.Error())
  30. } else {
  31. defer g.Close()
  32. }
  33. //logger.SetRollingDaily("C:/test/zkcron/src/test", "test.log")
  34. logger.SetConsole(true)
  35. colcap := map[string]int{}
  36. if rows, err := g.RawQuery(`select column_name from system_schema.columns where keyspace_name=? and table_name='object'`, keyspace); err != nil {
  37. logger.Error(err)
  38. return
  39. } else {
  40. for _, row := range rows {
  41. col := row["column_name"].(string)
  42. if strings.HasPrefix(col, "_") {
  43. continue
  44. }
  45. if idx := strings.LastIndex(col, "_"); idx == -1 {
  46. continue
  47. } else {
  48. prefix := col[:idx]
  49. n, _ := strconv.Atoi(col[idx+1:])
  50. if max, ok := colcap[prefix]; ok {
  51. if n > max {
  52. colcap[prefix] = n
  53. }
  54. } else {
  55. colcap[prefix] = n
  56. }
  57. }
  58. }
  59. }
  60. var collucene = map[string]string{
  61. "varchar": `{"type" : "string"}`,
  62. "text": `{"type" : "text"}`,
  63. "smallint": `{"type" : "integer"}`,
  64. "int": `{"type" : "integer"}`,
  65. "bigint": `{"type" : "bigint"}`,
  66. "double": `{"type" : "double"}`,
  67. "float": `{"type" : "float"}`,
  68. "boolean": `{"type" : "boolean"}`,
  69. "blob": `{"type" : "bytes"}`,
  70. "date": `{"type" : "date", "pattern" :"yyyy-MM-dd"}`,
  71. "timestamp": `{"type" : "date", "pattern" :"yyyy-MM-dd HH:mm:ss.SSS"}`,
  72. "set_varchar": `{"type" : "string"}`,
  73. "set_text": `{"type" : "text"}`,
  74. "set_double": `{"type" : "double"}`,
  75. "set_float": `{"type" : "float"}`,
  76. "set_int": `{"type" : "integer"}`,
  77. "list_varchar": `{"type" : "string"}`,
  78. "list_text": `{"type" : "text"}`,
  79. "list_double": `{"type" : "double"}`,
  80. "list_float": `{"type" : "float"}`,
  81. "list_int": `{"type" : "integer"}`,
  82. "map_varchar_text": `{"type" : "text"}`,
  83. "map_varchar_varchar": `{"type" : "string"}`,
  84. "map_varchar_float": `{"type" : "float"}`,
  85. "map_varchar_set": `{"type" : "string"}`,
  86. }
  87. var colstamp = map[string]time.Time{}
  88. now := time.Now()
  89. for colprefix := range colcap {
  90. colstamp[colprefix] = now
  91. }
  92. fmt.Println("update colbulk ...")
  93. logger.Warn(colcap)
  94. if _, err := g.RawQuery(`INSERT INTO colbulk (domain, cap, collucene, colstamp) VALUES(?, ?, ?, ?)`, keyspace, colcap, collucene, colstamp); err != nil {
  95. logger.Errorf(" error: %v", err)
  96. }
  97. }