コード例 #1
0
ファイル: srvclient.go プロジェクト: matishsiao/ssdbproxy
func (cl *SrvClient) CreateConnection(v DBNodeInfo) {
	db, err := ssdb.Connect(v.Host, v.Port, v.Password)
	if CONFIGS.Debug {
		log.Println("Connect to ", v.Host, v.Port)
	}
	if err != nil {
		log.Println("Connect to Fail:", v.Host, v.Port, err)
	}
	db.Debug(CONFIGS.Debug)
	cl.DBNodes = append(cl.DBNodes, &DBNode{Client: db, Id: v.Id, Info: v})
}
コード例 #2
0
ファイル: srvclient.go プロジェクト: matishsiao/ssdbproxy
func (cl *SrvClient) CreateBatchConnection() *ssdb.Client {
	for _, v := range CONFIGS.Nodelist {
		if v.Mode == "main" {
			db, err := ssdb.Connect(v.Host, v.Port, v.Password)
			if CONFIGS.Debug {
				log.Println("Connect to ", v.Host, v.Port)
			}
			if err != nil {
				continue
			}
			db.Debug(CONFIGS.Debug)
			return db
		}
	}
	return nil
}
コード例 #3
0
ファイル: serverclient.go プロジェクト: matishsiao/ssdbproxy
func (scp *ServerConnectionPool) ConnectToDB(name string, v DBNodeInfo) {
	for i := 0; i < scp.ConnectionLimit; i++ {
		db, err := ssdb.Connect(v.Host, v.Port, v.Password)
		if CONFIGS.Debug {
			log.Println("Connect to ", v.Host, v.Port)
		}
		if err != nil {
			log.Printf("Connect to %s:%d Error:%v\n", v.Host, v.Port, err)
			continue
		}
		db.Debug(CONFIGS.Debug)
		//default use zip transfered data
		if v.Mode == "mirror" {
			db.UseZip(true)
			db.Do("zip", 1)
		}
		scp.Pool[name] = append(scp.Pool[name], ServerConnection{Client: db, Info: v, Mu: &sync.Mutex{}})
	}
	log.Printf("Add Mirror Connection[%s][%s]:%d Connections.", name, v.Mode, len(scp.Pool[name]))
}
コード例 #4
0
ファイル: ssdb2hbase.go プロジェクト: jerry1022/ssdb2hbase
func main() {
	var f *os.File
	if !FileExists("/var/log/ssdb2hbase/ssdb2hbase.log") {
		f, _ = os.Create("/var/log/ssdb2hbase/ssdb2hbase.log")
	} else {
		f, _ = os.OpenFile("/var/log/ssdb2hbase/ssdb2hbase.log", os.O_APPEND|os.O_WRONLY, 0755)
	}
	log.SetOutput(f)
	defer f.Close()

	host, _ = os.Hostname()
	flag.StringVar(&configPath, "c", "/etc/ssdb2hbase/config.json", "config filename")
	flag.Parse()
	ok, config := loadConfigs(configPath)
	if !ok {
		log.Println("load config failed")
		os.Exit(0)
	}

	configs = config
	log.Printf("version:%s ssdb:%s:%d hbase thrift:%s:%d", version,
		configs.SSDB.Address, configs.SSDB.Port,
		configs.HBase.Address, configs.HBase.Port)

	//ssdb connection setting
	db, err = ssdb.Connect(configs.SSDB.Address, configs.SSDB.Port, configs.SSDB.Auth)
	if err != nil {
		log.Printf("ssdb connect failed: %s\n", err)
		os.Exit(1)
	}
	defer db.Close()

	//hbase client setting
	Address := fmt.Sprintf("%s:%d", configs.HBase.Address, configs.HBase.Port)
	hclient, err = goh.NewTcpClient(Address, goh.TBinaryProtocol, false)
	if err != nil {
		log.Printf("hbase connect failed: %s\n", err)
		os.Exit(1)
	}
	if err := hclient.Open(); err != nil {
		log.Printf("hbase open connection failed: %s\n", err)
		os.Exit(1)
	}
	defer hclient.Close()

	// set hash for sync all conf purpose
	db.HashSet("ssdb2hbase-status", host, strconv.FormatInt(time.Now().Unix(), 10))

	var cf = []string{"proxyagent"}

	var proxyCounter ProxyCounter
	var mutations []*Hbase.Mutation
	var rowBatches []*Hbase.BatchMutation

	result := CreateHbaseTable(configs.HBase.Tbl, cf)
	if !result {
		log.Printf("Create Table Error")
	}

	data := ReadSSDBData(configs.SSDB.HT)
	for k, v := range data {
		jsonerr := json.Unmarshal([]byte(v.(string)), &proxyCounter)
		if jsonerr != nil {
			fmt.Println(jsonerr)
		}
		cfq := GetStructFieldsKV(proxyCounter)
		for cfqk, cfqv := range cfq {
			cfqkey := fmt.Sprintf("%s:%s", cf[0], cfqk)
			switch cfqv.(type) {
			case string:
				mutations = append(mutations, goh.NewMutation(cfqkey, []byte(cfqv.(string))))
			case int:
				mutations = append(mutations, goh.NewMutation(cfqkey, []byte(string(strconv.FormatInt(int64(cfqv.(int)), 10)))))
			case float64:
				mutations = append(mutations, goh.NewMutation(cfqkey, []byte(strconv.FormatFloat(cfqv.(float64), 'E', 1, 64))))
			}
		}
		rowBatches = append(rowBatches, goh.NewBatchMutation([]byte(k), mutations))
	}
	updateResult := UpdateHbaseBatchData(configs.HBase.Tbl, rowBatches, nil)
	if updateResult {
		fmt.Println("Update Successful")
	}

}