• 使用StreamLoad实现数据同步到StarRocks


    方法1:使用requests同步

    1. from requests import Session
    2. import base64
    3. class LoadSession(Session):
    4. def rebuild_auth(self, prepared_request, response):
    5. """
    6. No code here means requests will always preserve the Authorization
    7. header when redirected.
    8. """
    9. def main():
    10. """
    11. Stream load Demo with Standard Lib requests
    12. """
    13. username, password = 'root', ''
    14. headers={
    15. "Content-Type": "text/html; charset=UTF-8",
    16. #"Content-Type": "application/octet-stream", # file upload
    17. "connection": "keep-alive",
    18. "max_filter_ratio": "0.2",
    19. "columns": "k,v",
    20. "column_separator": ',',
    21. "Expect": "100-continue",
    22. }
    23. payload = '''k1,v1\nk2,v2\nk3,v3'''
    24. database = 'starrocks_demo'
    25. tablename = 'tb1'
    26. api = 'http://master1:8030/api/%s/%s/_stream_load' % (database, tablename)
    27. session = LoadSession()
    28. session.auth = (username, password)
    29. response = session.put(url=api, headers=headers, data=payload)
    30. #response = session.put(url=api, headers=headers, data= open("a.csv","rb")) # file upload
    31. print(response.json())
    32. if __name__ == '__main__':
    33. main()

    方法2:数据保存成文件再执行终端命令同步

    1. import subprocess
    2. import time
    3. class StarRocksClient(object):
    4. def __init__(self, host, port, database, columns, sep,
    5. username, password, filename, table, timeout):
    6. self.filename = filename
    7. self.table = table
    8. self.columns = columns
    9. self.sep = sep
    10. self.host = host
    11. self.port = port
    12. self.database = database
    13. self.user = username
    14. self.password = password
    15. self.timeout = timeout
    16. def get_label(self):
    17. t = time.time().__str__().replace(".", "_")
    18. return '_'.join([self.database,self.table, t])
    19. def load(self):
    20. label = self.get_label()
    21. cmd = "curl"
    22. param_location = "--location-trusted"
    23. param_user = "%s:%s" % (self.user, self.password)
    24. param_file = "%s" % self.filename
    25. param_url = "http://%s:%s/api/%s/%s/_stream_load" % (
    26. self.host, self.port, self.database, self.table
    27. )
    28. p = subprocess.Popen([
    29. cmd, param_location,
    30. "-H", 'columns: %s' %self.columns,
    31. "-H", "column_separator: %s" %self.sep,
    32. "-H", "label: %s" %self.get_label(),
    33. "-H", "timeout: %d" %self.timeout,
    34. "-u", param_user,
    35. "-T", param_file,
    36. param_url])
    37. p.wait()
    38. if p.returncode != 0:
    39. print """\nLoad to starrocks failed! LABEL is %s""" % (label)
    40. else:
    41. print """\nLoad to starrocks success! LABEL is %s """ % (label)
    42. return label
    43. if __name__ == '__main__':
    44. """
    45. -- Stream load Demo with Linux cmd - Curl
    46. --
    47. -- StarRocks DDL:
    48. CREATE TABLE `starrocks_demo`.`tb1` (
    49. `k` varchar(65533) NULL COMMENT "",
    50. `v` varchar(65533) NULL COMMENT ""
    51. ) ENGINE=OLAP
    52. DUPLICATE KEY(`k`)
    53. COMMENT "OLAP"
    54. DISTRIBUTED BY HASH(`k`) BUCKETS 1
    55. PROPERTIES (
    56. "replication_num" = "1",
    57. "in_memory" = "false",
    58. "storage_format" = "DEFAULT"
    59. );
    60. """
    61. # load job 1
    62. client1 = StarRocksClient(
    63. host="master1",
    64. port="8030",
    65. database="starrocks_demo",
    66. username="root",
    67. password="",
    68. filename="/tmp/test.csv", # data from local file /tmp/test.csv, usage: python CurlStreamLoad.py
    69. table="tb1",
    70. columns='k,v',
    71. sep=",",
    72. timeout=86400
    73. )
    74. client1.load()
    75. time.sleep(1)
    76. # load job 2
    77. client2 = StarRocksClient(
    78. host="master1",
    79. port="8030",
    80. database="starrocks_demo",
    81. username="root",
    82. password="",
    83. filename="-", # data from stdin, usage: echo 'k1,v1\nk2,v2'| python CurlStreamLoad.py
    84. table="tb1",
    85. columns='k,v',
    86. sep=",",
    87. timeout=86400
    88. )
    89. client2.load()

  • 相关阅读:
    Windows安装Linux双系统教程
    性能测试之cpu 分析
    车间生产设备管理有哪些问题?低代码来助力
    Metis安装(5.0.1与4.0.3)
    Spring Data Jpa 实现批量插入或更新
    python+pytest接口自动化(10)-session会话保持
    Android 线程池源码详解(一)
    Kafka3.0.0版本——消费者(分区的分配以及再平衡)
    微服务实践之网关(Spring Cloud Gateway)详解-SpringCloud(2021.0.x)-3
    Android Update Engine 分析(二十四)制作降级包时,到底发生了什么?
  • 原文地址:https://blog.csdn.net/Gzigithub/article/details/128136137