rebulk.go 2.9 KB

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