Kettle系列:使用Kudu API插入数据到Kudu中
本文详细介绍了在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中的更多相关文章
- 【转载】C#批量插入数据到Sqlserver中的三种方式
引用:https://m.jb51.net/show/99543 这篇文章主要为大家详细介绍了C#批量插入数据到Sqlserver中的三种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本篇, ...
- C#批量插入数据到Sqlserver中的四种方式
我的新书ASP.NET MVC企业级实战预计明年2月份出版,感谢大家关注! 本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的 ...
- sql 批量插入数据到Sqlserver中 效率较高的方法
使用SqlBulk #region 方式二 static void InsertTwo() { Console.WriteLine("使用Bulk插入的实现方式"); Stopwa ...
- 关于从JSP页面插入数据到数据库中乱码问题的解决
问题描述:最近我在写一个j2ee的留言板系统模块,遇到了一个非常让我头大的问题,当我从JSP页面输入数据后,通过hibernate中的业务逻辑类HQL语句把这个数据插入到本地的mysql数据库中,可是 ...
- C#批量插入数据到Sqlserver中的三种方式
本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生 成 ...
- C#_批量插入数据到Sqlserver中的四种方式
先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生成一个GUID算法所花的时间肯定比你从数据表中重新查询上一条记 ...
- java批量插入数据进数据库中
方式1: for循环,每一次进行一次插入数据. 方式2: jdbc的preparedStatement的batch操作 PreparedStatement.addBatch(); ...... Pre ...
- C# 之 批量插入数据到 SQLServer 中
创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快.而如果存在索引的情况下,每次插入记录都会进行索引重建,这是非常耗性能的.如 ...
- C#批量插入数据到Sqlserver中的四种方式 - 转
先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生成一个GUID算法所花的时间肯定比你从数据表中重新查询上一条记 ...
随机推荐
- hdu 1527 (威佐夫博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1527 Problem Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石 ...
- python爬虫解析库学习
一.xpath库使用: 1.基本规则: 2.将文件转为HTML对象: html = etree.parse('./test.html', etree.HTMLParser()) result = et ...
- es6+的javascript拓展内容
一.let,const 1.因为块级的作用域,这样打印01234,循环外打印i会报错 for (let i = 0; i < 5; i++) { setTimeout(console.log(i ...
- (二)flask-sqlalchemy基本操作
对数据库基本操作 在Flask-SQLAlchemy中,插入.修改.删除操作,均由数据库会话管理. 会话用 db.session 表示.在准备把数据写入数据库前,要先将数据添加到会话中然后调用 db. ...
- SFTP多文件上传,删除
公司项目中需要把项目的相关文件上传到服务器的tomcat中,需要在项目中进行以下几步操作: 1.添加项目信息,包括名称,描述,服务器ip,sftp的用户名,密码,端口号等,存在配置,部署,删除等操作 ...
- ecplise 修改编码
1.修改eclipse默认工作空间编码方式 window->preferences->general->workspace 2.修改工程编码方式 项目右键->propertie ...
- js定时器setInterval()与setTimeout()
js定时器setInterval()与setTimeout() 1.setTimeout(Expression,DelayTime),在DelayTime过后,将执行一次Expression,setT ...
- django(六)之ORM数据库操作
https://www.cnblogs.com/haiyan123/p/7732190.html 一.ORM介绍 ORM——object relation mapping 映射关系: 表名 ----- ...
- Python之迭代器,生成器
迭代器 1.什么是可迭代对象 字符串.列表.元组.字典.集合都可以被for循环,说明他们都是可迭代的. from collections import Iterable l = [1,2,3,4] t ...
- 实现迁徙学习-《Tensorflow 实战Google深度学习框架》代码详解
为了实现迁徙学习,首先是数据集的下载 #利用curl下载数据集 curl -o flower_photos.tgz http://download.tensorflow.org/example_ima ...