singer 团队官方处了一个tap-s3-csv 的tap,对于没有使用aws 的人来说并不是很方便了,所以简单修改了
下源码,可以支持通用的s3 csv 文件的处理,同时发布到了官方pip 仓库中,方便大家使用。
以下是简单代码修改部分的说明,以及如何发布pip包

修改说明

主要是关于连接s3 的部分,因为tap-s3-csv 使用的是boto3 我们需要修改的就是关于boto3 连接s3 的部署
添加上aws_access_key_id,aws_secret_access_key,endpoint_url
关于s3 自定义连接的说明,格式如下:

 
  s3_client = boto3.session.Session().client(
        service_name='s3',
        aws_access_key_id=aws_access_key_id,
        aws_secret_access_key=aws_secret_access_key,
        endpoint_url=endpoint_url,
    )

几个需要修改的部分

  • s3.py
    get_input_files_for_table 部分,主要是传递参数的
    修改如下:
 
def get_input_files_for_table(config, table_spec, modified_since=None):
    bucket = config['bucket']
    aws_access_key_id = config['aws_access_key_id']
    aws_secret_access_key =config['aws_secret_access_key']
    endpoint_url =config['endpoint_url']
    to_return = []
    pattern = table_spec['search_pattern']
    try:
        matcher = re.compile(pattern)
    except re.error as e:
        raise ValueError(
            ("search_pattern for table `{}` is not a valid regular "
             "expression. See "
             "https://docs.python.org/3.5/library/re.html#regular-expression-syntax").format(table_spec['table_name']),
            pattern) from e
    LOGGER.info(
        'Checking bucket "%s" for keys matching "%s"', bucket, pattern)
    matched_files_count = 0
    unmatched_files_count = 0
    max_files_before_log = 30000
    for s3_object in list_files_in_bucket(bucket,aws_access_key_id,aws_secret_access_key,endpoint_url, table_spec.get('search_prefix')):
        key = s3_object['Key']
        last_modified = s3_object['LastModified']
        LOGGER.info(key)
        LOGGER.info(last_modified)
        if s3_object['Size'] == 0:
            LOGGER.info('Skipping matched file "%s" as it is empty', key)
            unmatched_files_count += 1
            continue
        if matcher.search(key):
            matched_files_count += 1
            if modified_since is None or modified_since < last_modified:
                LOGGER.info('Will download key "%s" as it was last modified %s',
                            key,
                            last_modified)
                yield {'key': key, 'last_modified': last_modified}
        else:
            unmatched_files_count += 1
        if (unmatched_files_count + matched_files_count) % max_files_before_log == 0:
            # Are we skipping greater than 50% of the files?
            if 0.5 < (unmatched_files_count / (matched_files_count + unmatched_files_count)):
                LOGGER.warn(("Found %s matching files and %s non-matching files. "
                             "You should consider adding a `search_prefix` to the config "
                             "or removing non-matching files from the bucket."),
                            matched_files_count, unmatched_files_count)
            else:
                LOGGER.info("Found %s matching files and %s non-matching files",
                            matched_files_count, unmatched_files_count)
    if 0 == matched_files_count:
        raise Exception("No files found matching pattern {}".format(pattern))

list_files_in_bucket 修改核心部分,关于连接s3 的
修改如下:

 
 @retry_pattern()
def list_files_in_bucket(bucket,aws_access_key_id,aws_secret_access_key,endpoint_url, search_prefix=None):
    s3_client = boto3.session.Session().client(
        service_name='s3',
        aws_access_key_id=aws_access_key_id,
        aws_secret_access_key=aws_secret_access_key,
        endpoint_url=endpoint_url,
    )
    s3_object_count = 0
    max_results = 1000
    args = {
        'Bucket': bucket,
        'MaxKeys': max_results,
    }
    if search_prefix is not None:
        args['Prefix'] = search_prefix
    paginator = s3_client.get_paginator('list_objects_v2')
    pages = 0
    for page in paginator.paginate(**args):
        pages += 1
        LOGGER.debug("On page %s", pages)
        s3_object_count += len(page['Contents'])
        yield from page['Contents']
    if 0 < s3_object_count:
        LOGGER.info("Found %s files.", s3_object_count)
    else:
        LOGGER.warning('Found no files for bucket "%s" that match prefix "%s"', bucket, search_prefix)

get_file_handle 部分,主要是关于获取s3 对象内容的

@retry_pattern()
def get_file_handle(config, s3_path):
    bucket = config['bucket']
    aws_access_key_id = config['aws_access_key_id']
    aws_secret_access_key =config['aws_secret_access_key']
    endpoint_url =config['endpoint_url']
    s3_client = boto3.resource(
       service_name="s3",
       aws_access_key_id=aws_access_key_id,
       aws_secret_access_key=aws_secret_access_key,
       endpoint_url=endpoint_url)
    s3_bucket = s3_client.Bucket(bucket)
    s3_object = s3_bucket.Object(s3_path)
    return s3_object.get()['Body']
  • init.py
    关于tap 命令处理的部分,比如模式发现,执行同步,以及参数检验的
    参数校验修改,修改为我们配置参数需要的
REQUIRED_CONFIG_KEYS = ["start_date", "bucket", "aws_access_key_id", "aws_secret_access_key", "endpoint_url"]

main 函数:

@singer.utils.handle_top_exception(LOGGER)
def main():
    args = singer.utils.parse_args(REQUIRED_CONFIG_KEYS)
    config = args.config
    bucket = config['bucket']
    aws_access_key_id = config['aws_access_key_id']
    aws_secret_access_key =config['aws_secret_access_key']
    endpoint_url =config['endpoint_url']
    config['tables'] = validate_table_config(config)
    try:
        for page in s3.list_files_in_bucket(bucket,aws_access_key_id,aws_secret_access_key,endpoint_url):
            break
        LOGGER.warning("I have direct access to the bucket without assuming the configured role.")
    except:
        LOGGER.error("can't connect to s3 storage")
    if args.discover:
        do_discover(args.config)
    elif args.properties:
        do_sync(config, args.properties, args.state)
  • pip 包约定处理
    为了不和官方冲突,重新别名
    setup.py:
 
#!/usr/bin/env python
from setuptools import setup
setup(name='tap-minio-csv',
      version='1.2.2',
      description='Singer.io tap for extracting CSV files from minio',
      author='rongfengliang',
      url='https://github.com/rongfengliang/tap-minio-csv',
      classifiers=['Programming Language :: Python :: 3 :: Only'],
      py_modules=['tap_minio_csv'],
      install_requires=[
          'backoff==1.3.2',
          'boto3==1.9.57',
          'singer-encodings==0.0.3',
          'singer-python==5.1.5',
          'voluptuous==0.10.5'
      ],
      extras_require={
          'dev': [
              'ipdb==0.11'
          ]
      },
      entry_points='''
          [console_scripts]
          tap-minio-csv=tap_minio_csv:main
      ''',
      packages=['tap_minio_csv'])
  • 项目包名称

发布pip 包

  • 安装工具
python3 -m pip install --user --upgrade setuptools wheel twine
  • 生成文件
python3 setup.py sdist bdist_wheel
  • 上传
    需要先注册账户,执行以下命令,按照提示输入账户信息即可
 
twine upload dist/*
  • pip 包

说明

以上是一个简单的说明,详细代码可以参考https://github.com/rongfengliang/tap-minio-csv

参考资料

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
https://github.com/singer-io/tap-s3-csv
https://github.com/rongfengliang/tap-minio-csv

Singer 修改tap-s3-csv 支持minio 连接的更多相关文章

  1. ECMall如何支持SSL连接邮件服务器的配置

    首先,主要是ecmall使用的phpmailer版本太低,不支持加密连接. 然后,得对相应代码做一定调整. 1. 覆盖phpmailer 请从附件进行下载: http://files.cnblogs. ...

  2. 在SSIS 的 64 位版本中不支持 Excel 连接管理器

    Microsoft sql server 2008 R2——> SQL SERVER Business Intelligence Development Studio 使用EXCEL数据源或目标 ...

  3. vs中开发web站点使IIS Express支持局域网连接

    vs中开发web站点使IIS Express支持局域网连接 在开发webapi的时候,客户端设备都会使用局域网的地址访问webapi,有时候需要调试api.这个时候就需要使用一些技巧了,这里我记录了我 ...

  4. MySQL不支持远程连接的解决办法

    如果mysql不支持远程连接,会出现提示:错误代码是1130,ERROR 1130: Host * is not allowed to connect to this MySQL server ,解决 ...

  5. NetworkComms V3 之支持TCP连接和UDP连接

    NetworkComms V3 无缝的支持TCP连接和UDP连接. 您可以很容易的创建这两种连接 //创建一个连接信息对象 ConnectionInfo connInfo = ); //创建一个TCP ...

  6. Mysql 连接查询 Mysql支持的连接查询有哪些

    CREATE TABLE `chx` (   `id` VARCHAR(20) NOT NULL,   `name` VARCHAR(50) DEFAULT NULL,   `name2` CHAR( ...

  7. 已使用 163 邮箱测试通过,且支持 SSL 连接。 发送邮件

    示例:Jack 发送一封邮件给 Rose. public class SendMail {     public static void main(String[] args) {         b ...

  8. HslCommunication库的二次协议扩展,适配第三方通讯协议开发,基础框架支持长短连接模式

    本文将使用一个gitHub开源的项目来扩展实现二次协议的开发,该项目已经搭建好了基础层架构,并实现了三菱,西门子,欧姆龙,MODBUS-TCP的通讯示例,也可以参照这些示例开发其他的通讯协议,并Pul ...

  9. SQLServer 2016 Express 安装部署,并配置支持远程连接

    在项目中需要用到SQLServer,于是安装部署了SQLServer,部署的过程中遇到了一下问题,记录一下以便之后遇到同样问题能快速解决. 一.安装包下载 首先下载必要的安装包: 1.SQLServe ...

随机推荐

  1. 一起来学习.net core程序使用中介者模式:MediatR插件

    中介者模式是一种常见的设计模式,旨再降低程序的耦合性,因为传统的三层模式层层之间需要显示的调用,必须上层依赖下层,耦合性很高,为了解耦,将所有的指令单独放在一个位置处理,其他位置均通过这个位置来间接的 ...

  2. 配置两个不同kerberos认证中心的集群间的互信

    两个Hadoop集群开启Kerberos验证后,集群间不能够相互访问,需要实现Kerberos之间的互信,使用Hadoop集群A的客户端访问Hadoop集群B的服务(实质上是使用Kerberos Re ...

  3. NuGet包 安装相关指令

    一.安装 1.安装指定版本类库install-package <程序包名> -version <版本号> 2.安装到指定的项目install-package <程序包名& ...

  4. 微信小程序和asp.net core基于docker和nginx的交互

    这个文章的题目起的比较长,我想实现这样一个产品: 前端是微信小程序,后端是基于docker运行的asp.net core webapi.webapi通过nginx实现的反向代理接入,nginx同样基于 ...

  5. 彻底搞懂etcd raft选举、数据同步

    etcd raft选举机制 etcd 是一个分布式的k/V存储系统.核心使用了RAFT分布式一致性协议.一致性这个概念,它是指多个服务器在状态达成一致,但是在一个分布式系统中,因为各种意外可能,有的服 ...

  6. python基础03--int,bool,str

    1.1 数字int 1.i = 100  i.bit_length()   转化为二进制的最小位数 1.2 布尔 bool 1.True  False      0是False 1.3 数据转换    ...

  7. 交互式脚本expect场景示例

    expect语法示例 #spawn 新建一个进程,这个进程的交互由expect控制 #expect 等待接受进程返回的字符串,直到超时时间,根据规则决定下一步操作 #send 发送字符串给expect ...

  8. springmvc处理json数据

    springMVC提供了处理JSON格式请求/响应的HttpMessageConverter MappingJckson2HttpMessageConverter利用Jackson开源类包处理JSON ...

  9. MES应用案例|新宏泰电器乘上智能制造的东风

    企业背景: 无锡新宏泰电器科技股份有限公司(下文简称:新宏泰电器)创立于1984年,公司主要生产断路器.微型电机.BMC/SMC材料.BMC/SMC模压制品及各类塑料模具的设计制造.已于2016年在沪 ...

  10. Charles弱网测试转载

    一.破解版安装 工具好用是好用,但有个蛋疼的地方,非开源,非开源也就算了,还来个试用30分钟,当时的我就中这招了, 试用了之后发现这工具确实好用,对于测试工程师来说兼抓包定位协议类bug.设置网络阀来 ...