Example #1
0
func main() {

	address := "192.168.17.129:9090"
	fmt.Println(address)

	client, err := goh.NewTcpClient(address, goh.TBinaryProtocol, false)
	if err != nil {
		fmt.Println(err)
		return
	}

	if err = client.Open(); err != nil {
		fmt.Println(err)
		return
	}

	defer client.Close()

	table := "test"

	attributes := make(map[string]string)
	attributes["attr1"] = "attr-val1"
	attributes["attr2"] = "attr-val2"

	columns := make([]string, 2)
	columns[0] = "cf:a"
	columns[1] = "cf:b"

	var timestamp int64
	timestamp = 1362555589004

	rows := make([][]byte, 2)
	rows[0] = []byte("row1")
	rows[1] = []byte("row2")

	mutations := make([]*Hbase.Mutation, 1)
	mutations[0] = goh.NewMutation("cf:c", []byte("value3-mutation"))

	rowBatches := make([]*Hbase.BatchMutation, 1)
	rowBatches[0] = goh.NewBatchMutation([]byte("row3"), mutations)

	scan := &goh.TScan{
		StartRow:     []byte("row1"),
		StopRow:      []byte("row9"),
		Timestamp:    0,
		Columns:      []string{"cf:a", "cf:b"},
		Caching:      10,
		FilterString: "",
		//FilterString: "substring:value",
	}

	var scanId int32

	if "test" == "" {

		fmt.Print("IsTableEnabled:")
		fmt.Println(client.IsTableEnabled(table))

		fmt.Print("DisableTable:")
		fmt.Println(client.DisableTable(table))

		fmt.Print("IsTableEnabled:")
		fmt.Println(client.IsTableEnabled(table))

		fmt.Print("EnableTable:")
		fmt.Println(client.EnableTable(table))

		fmt.Print("IsTableEnabled:")
		fmt.Println(client.IsTableEnabled(table))

		fmt.Print("Compact:")
		fmt.Println(client.Compact(table))

		fmt.Print("MajorCompact:")
		fmt.Println(client.MajorCompact(table))

		fmt.Print("GetTableNames:")
		if data, err := client.GetTableNames(); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
		}

		fmt.Print("GetColumnDescriptors:")
		if data, err := client.GetColumnDescriptors(table); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
		}

		fmt.Print("GetTableRegions:")
		if data, err := client.GetTableRegions(table); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
		}

		fmt.Print("CreateTable:")
		cols := make([]*goh.ColumnDescriptor, 2)
		cols[0] = goh.NewColumnDescriptorDefault("cfa")
		cols[1] = goh.NewColumnDescriptorDefault("cfb")
		if exist, err := client.CreateTable("test_create", cols); err != nil {
			fmt.Println(err)
		} else {
			fmt.Println(exist)
		}

		fmt.Print("DisableTable:")
		fmt.Println(client.DisableTable("test_create"))

		fmt.Print("DeleteTable:")
		fmt.Println(client.DeleteTable("test_create"))

		fmt.Print("Get:")
		if data, err := client.Get(table, []byte("row1"), "cf:a", attributes); err != nil {
			fmt.Println(err)
		} else {
			printCells(data)
		}

		fmt.Print("GetVer:")
		if data, err := client.GetVer(table, []byte("row1"), "cf:a", 10, attributes); err != nil {
			fmt.Println(err)
		} else {
			printCells(data)
			//0 : value1 ; 1362555589004
			//1 : value1 ; 1362463859431
		}

		fmt.Print("GetVerTs:")
		if data, err := client.GetVerTs(table, []byte("row1"), "cf:a", timestamp, 10, attributes); err != nil {
			fmt.Println(err)
		} else {
			printCells(data)
		}

		fmt.Print("GetRow:")
		if data, err := client.GetRow(table, []byte("row1"), nil); err != nil {
			fmt.Println(err)
		} else {
			printRows(data)
		}

		fmt.Print("GetRowWithColumns:")
		if data, err := client.GetRowWithColumns(table, []byte("row1"), columns, nil); err != nil {
			fmt.Println(err)
		} else {
			printRows(data)
		}

		fmt.Print("GetRowTs:")
		if data, err := client.GetRowTs(table, []byte("row1"), timestamp, nil); err != nil {
			fmt.Println(err)
		} else {
			printRows(data)
		}

		fmt.Print("GetRowWithColumnsTs:")
		if data, err := client.GetRowWithColumnsTs(table, []byte("row1"), columns, timestamp, nil); err != nil {
			fmt.Println(err)
		} else {
			printRows(data)
		}

		fmt.Print("GetRows:")
		if data, err := client.GetRows(table, rows, nil); err != nil {
			fmt.Println(err)
		} else {
			printRows(data)
		}

		fmt.Print("GetRowsWithColumns:")
		if data, err := client.GetRowsWithColumns(table, rows, columns, nil); err != nil {
			fmt.Println(err)
		} else {
			printRows(data)
		}

		fmt.Print("GetRowsTs:")
		if data, err := client.GetRowsTs(table, rows, timestamp, nil); err != nil {
			fmt.Println(err)
		} else {
			printRows(data)
		}

		fmt.Print("GetRowsWithColumnsTs:")
		if data, err := client.GetRowsWithColumnsTs(table, rows, columns, timestamp, nil); err != nil {
			fmt.Println(err)
		} else {
			printRows(data)
		}

		fmt.Print("GetRowOrBefore:")
		if data, err := client.GetRowOrBefore(table, "row1", "cf"); err != nil {
			fmt.Println(err)
		} else {
			printCells(data)
		}

		fmt.Print("GetRegionInfo:")
		if data, err := client.GetRegionInfo(""); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
		}

		fmt.Print("MutateRow:")
		fmt.Println(client.MutateRow(table, []byte("row3"), mutations, nil))

		fmt.Print("MutateRowTs:")
		fmt.Println(client.MutateRowTs(table, []byte("row3"), mutations, timestamp, nil))

		fmt.Print("MutateRows:")
		fmt.Println(client.MutateRows(table, rowBatches, nil))

		fmt.Print("MutateRowsTs:")
		fmt.Println(client.MutateRowsTs(table, rowBatches, timestamp, nil))

		fmt.Print("AtomicIncrement:")
		if data, err := client.AtomicIncrement(table, []byte("row4"), "cf:a", 64); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
		}

		fmt.Print("DeleteAll:")
		fmt.Println(client.DeleteAll(table, []byte("row4"), "cf:a", nil))

		fmt.Print("DeleteAllTs:")
		fmt.Println(client.DeleteAllTs(table, []byte("row4"), "cf:a", timestamp, nil))

		fmt.Print("DeleteAllRow:")
		fmt.Println(client.DeleteAllRow(table, []byte("row4"), nil))

		fmt.Print("Increment:")
		fmt.Println(client.Increment(goh.NewTIncrement(table, []byte("row4"), "cf:a", 64)))

		fmt.Print("IncrementRows:")
		fmt.Println(client.IncrementRows([]*Hbase.TIncrement{goh.NewTIncrement(table, []byte("row4"), "cf:a", 64)}))

		fmt.Print("DeleteAllRowTs:")
		fmt.Println(client.DeleteAllRowTs(table, []byte("row4"), timestamp, nil))

		fmt.Print("ScannerOpenWithScan:")
		if data, err := client.ScannerOpenWithScan(table, scan, nil); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
			scanId = data
		}

		fmt.Print("ScannerOpen:")
		if data, err := client.ScannerOpen(table, []byte("row1"), columns, nil); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
			scanId = data
		}

		fmt.Print("ScannerOpenWithStop:")
		if data, err := client.ScannerOpenWithStop(table, []byte("row1"), []byte("row9"), columns, nil); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
			scanId = data
		}

		fmt.Print("ScannerOpenWithPrefix:")
		if data, err := client.ScannerOpenWithPrefix(table, []byte("row"), columns, nil); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
			scanId = data
		}

		fmt.Print("ScannerOpenTs:")
		if data, err := client.ScannerOpenTs(table, []byte("row1"), columns, 0, nil); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
			scanId = data
		}

		fmt.Print("ScannerOpenWithStopTs:")
		if data, err := client.ScannerOpenWithStopTs(table, []byte("row1"), []byte("row9"), columns, 0, nil); err != nil {
			fmt.Println(err)
		} else {
			dump(data)
			scanId = data
		}
	}

	fmt.Print("ScannerOpen:")
	if data, err := client.ScannerOpen(table, []byte("row1"), columns, nil); err != nil {
		fmt.Println(err)
	} else {
		dump(data)
		scanId = data
	}

	if scanId > 0 {
		// fmt.Println("scan start")
		// for {
		// 	if data, err := client.ScannerGet(scanId); err != nil {
		// 		fmt.Println(err)
		// 		break
		// 	} else if len(data) == 0 {
		// 		fmt.Println("scan end")
		// 		break
		// 	} else {
		// 		fmt.Println(data)
		// 	}
		// }

		fmt.Println("ScannerGetList")
		if data, err := client.ScannerGetList(scanId, 1000); err != nil {
			fmt.Println(err)
		} else {
			printRows(data)
		}

	}

	if scanId > 0 {
		fmt.Print("ScannerClose:")
		fmt.Println(client.ScannerClose(scanId))
	}

	fmt.Println("done")

}
Example #2
0
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")
	}

}