当前位置:网站首页>Apache Iceberg 中三种操作表的方式
Apache Iceberg 中三种操作表的方式
2020-11-09 07:35:00 【osc_tjee7sjs】
在 Apache Iceberg 中有很多种方式可以来创建表,其中就包括使用 Catalog 方式或者实现 org.apache.iceberg.Tables 接口。下面我们来简单介绍如何使用。.
使用 Hive catalog
从名字就可以看出,Hive catalog 是通过连接 Hive 的 MetaStore,把 Iceberg 的表存储到其中,它的实现类为 org.apache.iceberg.hive.HiveCatalog,下面是通过 sparkContext 中的 hadoopConfiguration 来获取 HiveCatalog 的方式:
import org.apache.iceberg.hive.HiveCatalog;
Catalog catalog = new HiveCatalog(spark.sparkContext().hadoopConfiguration());
Catalog 接口里面定义了操作表的方法,比如 createTable, loadTable, renameTable, 以及 dropTable。如果想创建表,我们需要定义 TableIdentifier,表的 Schema 以及分区的信息,如下:
import org.apache.iceberg.Table;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
TableIdentifier name = TableIdentifier.of("default", "iteblog");
Schema schema = new Schema(
Types.NestedField.required(1, "id", Types.IntegerType.get()),
Types.NestedField.optional(2, "name", Types.StringType.get()),
Types.NestedField.required(3, "age", Types.IntegerType.get()),
Types.NestedField.optional(4, "ts", Types.TimestampType.withZone())
);
PartitionSpec spec = PartitionSpec.builderFor(schema).year("ts").bucket("id", 2).build();
Table table = catalog.createTable(name, schema, spec);
使用 Hadoop catalog
Hadoop catalog 不依赖 Hive MetaStore 来存储元数据,其使用 HDFS 或者类似的文件系统来存储元数据。注意,文件系统需要支持原子的重命名操作,所以本地文件系统(local FS)、对象存储(S3、OSS等)来存储 Apache Iceberg 元数据是不安全的。下面是获取 HadoopCatalog 例子:
import org.apache.hadoop.conf.Configuration;
import org.apache.iceberg.hadoop.HadoopCatalog;
Configuration conf = new Configuration();
String warehousePath = "hdfs://www.iteblog.com:8020/warehouse_path";
HadoopCatalog catalog = new HadoopCatalog(conf, warehousePath);
和 Hive catalog 一样,HadoopCatalog 也是实现 Catalog 接口的,所以其也实现了表的各种操作方法,包括 createTable, loadTable, 以及 dropTable。下面是使用 HadoopCatalog 来创建 Iceberg 的例子:
import org.apache.iceberg.Table;
import org.apache.iceberg.catalog.TableIdentifier;
TableIdentifier name = TableIdentifier.of("logging", "logs");
Table table = catalog.createTable(name, schema, spec);
使用 Hadoop tables
Iceberg 也支持存储在 HDFS 目录中的表。和 Hadoop catalog 一样,文件系统需要支持原子的重命名操作,所以本地文件系统(local FS)、对象存储(S3、OSS等)来存储 Apache Iceberg 元数据是不安全的。这种方式存储的表并不支持表的各种操作,比如不支持 renameTable。下面是获取 HadoopTables 的例子:
import org.apache.hadoop.conf.Configuration;
import org.apache.iceberg.hadoop.HadoopTables;
import org.apache.iceberg.Table;
Configuration conf = new Configuration():
HadoopTables tables = new HadoopTables(conf);
Table table = tables.create(schema, spec, table_location);
在 Spark 中,其支持 HiveCatalog、HadoopCatalog 以及 HadoopTables 方式来创建、加载表。如果传入的表不是一个路径,则选择 HiveCatalog,否则 Spark 将推断出表是存储在 HDFS 上的。
当然,Apache Iceberg 表元数据存储地方是可插拔的,所以我们完全可以自定义元数据存储的方式,比如 AWS 就给社区提了一个 issue,其把 Apache Iceberg 中的元数据存储到 glue 里面,参见 #1633、#1608。
本博客文章除特别声明,全部都是原创!转载本文请加上:转载自过往记忆(https://www.iteblog.com/)
本文链接: 【Apache Iceberg 中三种操作表的方式】(https://www.iteblog.com/archives/9886.html)
版权声明
本文为[osc_tjee7sjs]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4410409/blog/4708492
边栏推荐
猜你喜欢
leetcode之反转字符串中的元音字母
App crashed inexplicably. At first, it thought it was the case of the name in the header. Finally, it was found that it was the fault of the container!
android开发中提示:requires permission android.permission write_settings解决方法
Chapter 5 programming
Tips in Android Development: requires permission android.permission write_ Settings solution
23 pictures, take you to the recommended system
常见特征金字塔网络FPN及变体
C++之异常捕获和处理
首次开通csdn,这篇文章送给过去的自己和正在发生的你
Esockettimeout solution in request in nodejs
随机推荐
Dark网站的后端为什么选择F#? - darklang
《MFC dialog中加入OpenGL窗体》
Database design: paradigms and anti paradigms
Service grid is still difficult - CNCF
基于链表的有界阻塞队列 —— LinkedBlockingQueue
23 pictures, take you to the recommended system
非阻塞的无界线程安全队列 —— ConcurrentLinkedQueue
How to reduce the resource consumption of istio agent through sidecar custom resource
你有没有想过为什么交易和退款要拆开不同的表
老大问我:“建表为啥还设置个自增 id ?用流水号当主键不正好么?”
平台商业化能力的另一种表现形式SAAS
C++之异常捕获和处理
失业日志 11月5日
操作系统之bios
File queue in Bifrost (1)
14.Kubenetes简介
How does semaphore, a thread synchronization tool that uses an up counter, look like?
Huawei HCIA notes
写时复制集合 —— CopyOnWriteArrayList
The vowels in the inverted string of leetcode