本文详细介绍了在Kettle中使用 Kudu API将数据写入Kudu中, 从本文可以学习到:
1. 如何编写一个简单的 Kettle 的 Used defined Java class.
2. 如何读取Kettle 每个记录的字段. 需要注意的是 getInteger() 返回的是Long 对象; 而获取 Timestamp 字段的方法是getDate().
3. 如何调用Kudu API.

本Kettle示例非常简单, Data Grid 组件定义一些sample data(包含多种数据类型), Java class将这些sample data写入kudu.

Kudu表schema:

CREATE TABLE kudu_testdb.perf_test_t1
(
id string ENCODING PLAIN_ENCODING COMPRESSION SNAPPY,
int_value int,
bigint_value bigint,
timestamp_value timestamp,
bool_value int,
PRIMARY KEY (histdate,id)
)
PARTITION BY HASH (histdate,id) PARTITIONS 2
STORED AS KUDU
TBLPROPERTIES (
'kudu.table_name' = 'testdb.perf_test_t1',
'kudu.master_addresses' = '10.205.6.1:7051,10.205.6.2:7051,10.205.7.3:7051'
);

重点看Java class 代码:

import java.sql.Timestamp;
import java.util.UUID;
import static java.lang.Math.toIntExact; import org.apache.kudu.client.Insert;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;
import org.apache.kudu.client.KuduSession;
import org.apache.kudu.client.KuduTable;
import org.apache.kudu.client.PartialRow;
import org.apache.kudu.client.SessionConfiguration; private final static String KUDU_TABLE="testdb.perf_test_t1";
private final static String KUDU_SERVERS="10.205.6.1:7051,10.205.6.2:7051,10.205.7.3:7051";
private final static int OPERATION_BATCH = 50; KuduClient client=null;
KuduSession session=null;
KuduTable table=null;
Integer recordCount=null;
SessionConfiguration.FlushMode mode; private Object[] previousRow;
private Object[] currentRow; public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
if (first) {
first = false;
} currentRow = getRow();
if (currentRow == null) {
setOutputDone();
return false;
} try {
session.setFlushMode(mode);
session.setMutationBufferSpace(OPERATION_BATCH); int uncommit = 0;
while(currentRow != null) {
Insert insert = table.newInsert();
PartialRow kuduRow = insert.getRow(); int intTmp;
Long longTmp;
String stringTmp;
java.util.Date dateTmp;
Boolean booleanTmp; // kettle string -> kudu string
//kuduRow.addString("id",UUID.randomUUID().toString());
stringTmp = get(Fields.In, "id").getString(currentRow);
if (stringTmp!=null)
{
kuduRow.addString("id",stringTmp);
} // kettle int -> kudu int
//import static java.lang.Math.toIntExact;
longTmp=get(Fields.In, "int_value").getInteger(currentRow);
if (longTmp!=null)
{
intTmp =toIntExact(get(Fields.In, "int_value").getInteger(currentRow));
kuduRow.addInt("int_value", intTmp);
} // kettle bigint -> kudu bigint
longTmp=get(Fields.In, "bigint_value").getInteger(currentRow);
if (longTmp!=null)
{
kuduRow.addLong("bigint_value", longTmp);
} // kettle date/timestamp -> kudu timestamp
dateTmp= get(Fields.In, "timestamp_value").getDate(currentRow);
if (dateTmp!=null)
{
longTmp=dateTmp.getTime()+8*3600*1000; //转到东8区时间
kuduRow.addLong("timestamp_value", longTmp*1000);
} // kettle boolean -> kudu int
booleanTmp= get(Fields.In, "boolean_value").getBoolean(currentRow);
if (booleanTmp!=null)
{
intTmp=0;
if (booleanTmp)
{intTmp=1;}
kuduRow.addInt("boolean_value", intTmp);
} // 对于手工提交, 需要buffer在未满的时候flush,这里采用了buffer一半时即提交
uncommit = uncommit + 1;
if (uncommit > OPERATION_BATCH / 2) {
session.flush();
uncommit = 0;
}
session.apply(insert);
previousRow=currentRow;
currentRow=getRow();
} // 对于手工提交, 保证完成最后的提交
if (uncommit > 0) {
session.flush();
} } catch (Exception e) {
e.printStackTrace();
throw e;
} // Send the row on to the next step.
//putRow(data.outputRowMeta, currentRow); return false;
} public boolean init(StepMetaInterface stepMetaInterface, StepDataInterface stepDataInterface) {
try {
client = new KuduClient.KuduClientBuilder(KUDU_SERVERS).build();
session = client.newSession();
table =client.openTable(KUDU_TABLE);
mode = SessionConfiguration.FlushMode.MANUAL_FLUSH;
} catch (Exception e) {
e.printStackTrace();
throw e;
} return parent.initImpl(stepMetaInterface, stepDataInterface);
} public void dispose(StepMetaInterface smi, StepDataInterface sdi) {
try {
if (!session.isClosed()) {
session.close();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
parent.disposeImpl(smi, sdi);
}

Kettle系列:使用Kudu API插入数据到Kudu中的更多相关文章

  1. 【转载】C#批量插入数据到Sqlserver中的三种方式

    引用:https://m.jb51.net/show/99543 这篇文章主要为大家详细介绍了C#批量插入数据到Sqlserver中的三种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本篇, ...

  2. C#批量插入数据到Sqlserver中的四种方式

    我的新书ASP.NET MVC企业级实战预计明年2月份出版,感谢大家关注! 本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的 ...

  3. sql 批量插入数据到Sqlserver中 效率较高的方法

    使用SqlBulk #region 方式二 static void InsertTwo() { Console.WriteLine("使用Bulk插入的实现方式"); Stopwa ...

  4. 关于从JSP页面插入数据到数据库中乱码问题的解决

    问题描述:最近我在写一个j2ee的留言板系统模块,遇到了一个非常让我头大的问题,当我从JSP页面输入数据后,通过hibernate中的业务逻辑类HQL语句把这个数据插入到本地的mysql数据库中,可是 ...

  5. C#批量插入数据到Sqlserver中的三种方式

    本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生 成 ...

  6. C#_批量插入数据到Sqlserver中的四种方式

    先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生成一个GUID算法所花的时间肯定比你从数据表中重新查询上一条记 ...

  7. java批量插入数据进数据库中

    方式1: for循环,每一次进行一次插入数据. 方式2: jdbc的preparedStatement的batch操作 PreparedStatement.addBatch(); ...... Pre ...

  8. C# 之 批量插入数据到 SQLServer 中

    创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快.而如果存在索引的情况下,每次插入记录都会进行索引重建,这是非常耗性能的.如 ...

  9. C#批量插入数据到Sqlserver中的四种方式 - 转

    先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生成一个GUID算法所花的时间肯定比你从数据表中重新查询上一条记 ...

随机推荐

  1. 面试---Python中的模块和包是什么?

    python模块是: 自我包含并且有组织的代码片段为模块. 表现形式为:写的代码保存为文件.这个文件就是一个模块.sample.py 其中文件名smaple为模块名字. python包是: 包是一个有 ...

  2. Vue中的计算属性与$watch

    计算属性:在模板中绑定表达式是非常便利的,但是他们实际上只用于简单的操作.模板是为了描述视图的结构.在模板中放入太多的逻辑会让模板过重且难以维护.这就是为什么vue.js将绑定表达式限制为一个表达式. ...

  3. 清理XFCE4卸载残留

    apt-get remove xfce4 apt-get remove xfce4* apt-get autoremove apt-get autoclean apt-get clean --- 更新 ...

  4. string的基本用法

    #include <iostream> #include<string> #include<vector> #include<algorithm> us ...

  5. Kafka史上最详细原理总结

    https://blog.csdn.net/ychenfeng/article/details/74980531 Kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(pa ...

  6. python基础面试常见题

    1.为什么学习Python? Python是目前市面上,我个人认为是最简洁.最优雅.最有前途.最全能的编程语言,没有之一. 2.通过什么途径学习的Python? 通过自学,包括网上查看一些视频,购买一 ...

  7. ImageMagick: win7 | win8 & uac (用户帐户控制) 注册表的一些事

    现在用win7,win8的人越来越多了, 程序在一些 win 7, win8 上运行会遇到一些之前没想过的兼容性问题. 比如 64位系统运行32位程序时的注册表重定向,还有因为 uac (用户帐户控制 ...

  8. BUG描述规范管理

    BUG:软件系统中存在的可能导致系统出错.失效.死机等问题的错误或缺陷. 描述一个缺陷,需要以下核心要素 标题:用简洁的话描述该缺陷,主要让开发知道这是一个什么样的缺陷 参数设置:Bug的类型(功能/ ...

  9. python(字符串、列表、字典、元组、集合)的常用内置方法

    一.字符串: lis='my name is maple' res=lis.count('m')#计算字符串内相同字符的个数 print(res) lis='my name is maple' res ...

  10. python各模块组合实例

    # encoding: utf-8 import requests import time from Crypto.Cipher import AES import base64 import has ...