聊聊flink的CsvTableSource

  序
  
  本文主要研究一下flink的CsvTableSource
  
  TableSource
  
  flink-table_2.11-1.7.1-sources.jar!/org/apache/flink/table/sources/TableSource.scala
  
  trait TableSource[T] {
  
  /** Returns the [[TypeInformation]] for the return type of the [[TableSource]].
  
  * The fields of the return type are mapped to the table schema based on their name.
  
  *
  
  * @return The type of the returned [[DataSet]] or [[DataStream]].
  
  */
  
  def getReturnType: TypeInformation[T]
  
  /**
  
  * Returns the schema of the produced table.
  
  *
  
  * @return The [[TableSchema]] of the produced table.
  
  */
  
  def getTableSchema: TableSchema
  
  /**
  
  * Describes the table source.
  
  *
  
  * @return A String explaining the [[TableSource]].
  
  */
  
  def explainSource(): String =
  
  TableConnectorUtil.generateRuntimeName(getClass, getTableSchema.getFieldNames)
  
  }
  
  TableSource定义了三个方法,分别是getReturnType、getTableSchema、explainSource
  
  BatchTableSource
  
  flink-table_2.11-1.7.1-sources.jar!/org/apache/flink/table/sources/BatchTableSource.scala
  
  trait BatchTableSource[T] extends TableSource[T] {
  
  /**
  
  * Returns the data of the table as a [[DataSet]].
  
  *
  
  * NOTE: This method is for internal use only for defining a [[TableSource]].
  
  * Do not use it in Table API programs.
  
  */
  
  def getDataSet(execEnv: ExecutionEnvironment): DataSet[T]
  
  }
  
  BatchTableSource继承了TableSource,它定义了getDataSet方法
  
  StreamTableSource
  
  flink-table_2.11-1.7.1-sources.jar!/org/apache/flink/table/sources/StreamTableSource.scala
  
  trait StreamTableSource[T] extends TableSource[T] {
  
  /**
  
  * Returns the data of the table as a [[DataStream]].
  
  *
  
  * NOTE: This method is for internal use only for defining a [[TableSource]].
  
  * Do not use it in Table API programs.
  
  */
  
  def getDataStream(execEnv: StreamExecutionEnvironment): DataStream[T]
  
  }
  
  StreamTableSource继承了TableSource,它定义了getDataStream方法
  
  CsvTableSource
  
  flink-table_2.11-1.7.1-sources.jar!/org/apache/flink/table/sources/CsvTableSource.scala
  
  class CsvTableSource private (
  
  private val path: String,
  
  private val fieldNames: Array[String],
  
  private val fieldTypes: Array[TypeInformation[_]],
  
  private val selectedFields: Array[Int],
  
  private val fieldDelim: String,
  
  private val rowDelim: String,
  
  private val quoteCharacter: Character,
  
  private val ignoreFirstLine: Boolean,
  
  private val ignoreComments: String,
  
  private val lenient: Boolean)
  
  extends BatchTableSource[Row]
  
  with StreamTableSource[Row]
  
  with ProjectableTableSource[Row] {
  
  def this(
  
  path: String,
  
  fieldNames: Array[String],
  
  fieldTypes: Array[TypeInformation[_]],
  
  fieldDelim: String = CsvInputFormat.DEFAULT_FIELD_DELIMITER,
  
  rowDelim: String = CsvInputFormat.DEFAULT_LINE_DELIMITER,
  
  quoteCharacter: Character = null,
  
  ignoreFirstLine: Boolean = false,
  
  ignoreComments: String = null,
  
  lenient: Boolean = false)www.michenggw.com = {
  
  this(
  
  path,
  
  fieldNames,
  
  fieldTypes,
  
  fieldTypes.indices.toArray, // initially, all fields are returned
  
  fieldDelim,
  
  rowDelim,
  
  quoteCharacter,
  
  ignoreFirstLine,
  
  ignoreComments,
  
  lenient)
  
  }
  
  def this(path: String, fieldNames: Array[String]www.fengshen157.com/, fieldTypes: Array[TypeInformation[_]]) = {
  
  this(path, fieldNames, fieldTypes, CsvInputFormat.DEFAULT_FIELD_DELIMITER,
  
  CsvInputFormat.DEFAULT_LINE_DELIMITER, null, false, null, false)
  
  }
  
  if (fieldNames.length != fieldTypes.length) {
  
  throw new TableException("Number of field names and field types must be equal.")
  
  }
  
  private val selectedFieldTypes = selectedFields.map(fieldTypes(_))
  
  private val selectedFieldNames = selectedFields.map(fieldNames(_))
  
  private val returnType: RowTypeInfo = new RowTypeInfo(selectedFieldTypes, selectedFieldNames)
  
  override def getDataSet(execEnv: ExecutionEnvironment): DataSet[Row] = {
  
  execEnv.createInput(createCsvInput(), returnType).name(explainSource())
  
  }
  
  /** Returns the [[RowTypeInfo]] for the return type of the [[CsvTableSource]]. */
  
  override def getReturnType: www.leyouzaixian2.com RowTypeInfo = returnType
  
  override def getDataStream(streamExecEnv: StreamExecutionEnvironment): DataStream[Row] = {
  
  streamExecEnv.createInput(createCsvInput(), returnType).name(explainSource())
  
  }
  
  /** Returns the schema of the produced table. */
  
  override def getTableSchema = new TableSchema(fieldNames, fieldTypes)
  
  /** Returns a copy of [[TableSource]] with ability to project fields */
  
  override def projectFields(fields: Array[Int]): CsvTableSource = {
  
  val selectedFields = if (fields.isEmpty) Array(0) else fields
  
  new CsvTableSource(
  
  path,
  
  fieldNames,
  
  fieldTypes,
  
  selectedFields,
  
  fieldDelim,
  
  rowDelim,
  
  quoteCharacter,
  
  ignoreFirstLine,
  
  ignoreComments,
  
  lenient)
  
  }
  
  private def createCsvInput(): RowCsvInputFormat = {
  
  val inputFormat = new RowCsvInputFormat(
  
  new Path(path),
  
  selectedFieldTypes,
  
  rowDelim,
  
  fieldDelim,
  
  selectedFields)
  
  inputFormat.setSkipFirstLineAsHeader(ignoreFirstLine)
  
  inputFormat.setLenient(www.dasheng178.com lenient)
  
  if (quoteCharacter != null) {
  
  inputFormat.enableQuotedStringParsing(quoteCharacter)
  
  }
  
  if (ignoreComments != null) {
  
  inputFormat.setCommentPrefix(ignoreComments)
  
  }
  
  inputFormat
  
  }
  
  override def equals(other: Any): Boolean = other match {
  
  case that: CsvTableSource => returnType == that.returnType &&
  
  path == that.path &&
  
  fieldDelim == that.fieldDelim &&
  
  rowDelim == that.rowDelim &&
  
  quoteCharacter == that.quoteCharacter &&
  
  ignoreFirstLine == that.ignoreFirstLine &&
  
  ignoreComments == that.ignoreComments &&
  
  lenient == that.lenient
  
  case _ => false
  
  }
  
  override def hashCode(www.hengda157.com): Int = {
  
  returnType.hashCode()
  
  }
  
  override def explainSource(): String = {
  
  s"CsvTableSource(" +
  
  s"read fields: ${getReturnType.getFieldNames.mkString(", ")})"
  
  }
  
  }
  
  CsvTableSource同时实现了BatchTableSource及StreamTableSource接口;getDataSet方法使用ExecutionEnvironment.createInput创建DataSet;getDataStream方法使用StreamExecutionEnvironment.createInput创建DataStream
  
  ExecutionEnvironment.createInput及StreamExecutionEnvironment.createInput接收的InputFormat为RowCsvInputFormat,通过createCsvInput创建而来
  
  getTableSchema方法返回的TableSchema通过fieldNames及fieldTypes创建;getReturnType方法返回的RowTypeInfo通过selectedFieldTypes及selectedFieldNames创建;explainSource方法这里返回的是CsvTableSource开头的字符串
  
  小结
  
  TableSource定义了三个方法,分别是getReturnType、getTableSchema、explainSource;BatchTableSource继承了TableSource,它定义了getDataSet方法;StreamTableSource继承了TableSource,它定义了getDataStream方法
  
  CsvTableSource同时实现了BatchTableSource及StreamTableSource接口;getDataSet方法使用ExecutionEnvironment.createInput创建DataSet;getDataStream方法使用StreamExecutionEnvironment.createInput创建DataStream
  
  ExecutionEnvironment.createInput及StreamExecutionEnvironment.createInput接收的InputFormat为RowCsvInputFormat,通过createCsvInput创建而来;getTableSchema方法返回的TableSchema通过fieldNames及fieldTypes创建;getReturnType方法返回的RowTypeInfo通过selectedFieldTypes及selectedFieldNames创建;explainSource方法这里返回的是CsvTableSource开头的字符串

原文地址:https://www.cnblogs.com/qwangxiao/p/10353179.html

时间: 2024-10-10 17:25:42

聊聊flink的CsvTableSource的相关文章

聊聊flink的log.file配置

本文主要研究一下flink的log.file配置 log4j.properties flink-release-1.6.2/flink-dist/src/main/flink-bin/conf/log4j.properties # This affects logging for both user code and Flink log4j.rootLogger=INFO, file # Uncomment this if you want to _only_ change Flink's lo

聊聊flink的AsyncWaitOperator

序本文主要研究一下flink的AsyncWaitOperator AsyncWaitOperatorflink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/operators/async/AsyncWaitOperator.java @Internalpublic class AsyncWaitOperator<IN, OUT> extends AbstractUdfStreamOperator&l

Flink与Spark Streaming在与kafka结合的区别!

本文主要是想聊聊flink与kafka结合.当然,单纯的介绍flink与kafka的结合呢,比较单调,也没有可对比性,所以的准备顺便帮大家简单回顾一下Spark Streaming与kafka的结合. 看懂本文的前提是首先要熟悉kafka,然后了解spark Streaming的运行原理及与kafka结合的两种形式,然后了解flink实时流的原理及与kafka结合的方式. kafka kafka作为一个消息队列,在企业中主要用于缓存数据,当然,也有人用kafka做存储系统,比如存最近七天的数据.

Apache流处理框架对比

分布式流处理,类似于MapReduce这样的通用计算模型,但是却要求它能够在毫秒级别或者秒级别完成响应.这些系统可以用DAG表示流处理的拓扑. Points of Interest 在比较不同系统是,可以参照如下几点 Runtime and Programming model(运行与编程模型) 一个平台提供的编程模型往往会决定很多它的特性,并且这个编程模型应该足够处理所有可能的用户案例. Functional Primitives(函数式单元) 一个合格的处理平台应该能够提供丰富的能够在独立信息

深度剖析阿里巴巴对Apache Flink的优化与改进

本文主要从两个层面深度剖析:阿里巴巴对Flink究竟做了哪些优化? 取之开源,用之开源 一.SQL层 为了能够真正做到用户根据自己的业务逻辑开发一套代码,能够同时运行在多种不同的场景,Flink首先需要给用户提供一个统一的API.在经过一番调研之后,阿里巴巴实时计算认为SQL是一个非常适合的选择.在批处理领域,SQL已经经历了几十年的考验,是公认的经典.在流计算领域,近年来也不断有流表二象性.流是表的ChangeLog等理论出现.在这些理论基础之上,阿里巴巴提出了动态表的概念,使得流计算也可以像

【翻译】Flink Table 和 SQL API 概念与通用API

本文翻译自官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/common.html Table API和SQL集成在共同API中.该API的中心概念是Table,用作查询的输入和输出.本文档介绍了使用Table API和SQL查询的程序的通用结构,如何注册 Table,如何查询Table以及如何发出 Table(数据). 两个 planner 之间的主要区别 表API和SQL程序的结构 创建一个Tab

Flink资料(4) -- 类型抽取和序列化

类型抽取和序列化 本文翻译自Type Extraction and Serialization Flink处理类型的方式比较特殊,包括它自己的类型描述,一般类型抽取和类型序列化框架.该文档描述这些概念并解释其机理. Java API和Scala API处理类型信息的方式有根本性的区别,所以本文描述的问题仅与其中一种API相关 一.Flink中对类型的处理 一般处理类型时,我们并不干涉,而是让编程语言和序列化框架来自动处理类型.与之相反的,Flink想要尽可能掌握进出用户函数的数据类型的信息. 1

Apache Flink流分区器剖析

这篇文章介绍Flink的分区器,在流进行转换操作后,Flink通过分区器来精确得控制数据流向. StreamPartitioner StreamPartitioner是Flink流分区器的基类,它只定义了一个抽象方法: public abstract StreamPartitioner<T> copy(); 但这个方法并不是各个分区器之间互相区别的地方,定义不同的分区器的核心在于--各个分区器需要实现channel选择的接口方法: int[] selectChannels(T record,

使用Flink时遇到的坑

1.启动不起来 查看JobManager日志: WARN org.apache.flink.runtime.webmonitor.JobManagerRetriever - Failed to retrieve leader gateway and port. akka.actor.ActorNotFound: Actor not found for: ActorSelection[Anchor(akka.tcp://[email protected]:6123/), Path(/user/jo