Kudu-java数据库简单操作
参考官网:http://kudu.apache.org/docs/kudu_impala_integration.html
参考:https://my.oschina.net/weiqingbin/blog/189413#OSC_h2_8
参考:https://www.cloudera.com/documentation/enterprise/5-12-x/topics/impala_create_table.html
1、创建表
CREATE TABLE my_first
(
id STRING,
int_value INT,
bigint_value BIGINT,
PRIMARY KEY(id)
)
PARTITION BY HASH PARTITIONS 3
STORED AS KUDU;
需要注意的是 PARTITION BY HASH PARTITIONS 3,3这个数字需要和实际的物理机器有关,不能大于实际的物理机器数,这个很令人费解,还没有找到原因。
java测试代码
package main.java;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;
import org.apache.kudu.client.*;
/**
* @Author:sks
* @Description:
* @Date:Created in 14:59 2018/3/29
* @Modified by:
**/
import java.util.Date;
public class test {
private final static int mutationBufferSpace = 2000; public static void main(String[] args) {
try {
testInsert();
} catch (KuduException e) {
e.printStackTrace();
} } public static void testInsert() throws KuduException {
String master = "192.163.1.175:7051";
KuduSession session =null;
// String master = "101.201.197.71:7051";
try {
KuduClient client = new KuduClient.KuduClientBuilder(master)
.build();
session = client.newSession();
String tableName = "impala::default.my_first";
KuduTable table = client.openTable(tableName); SessionConfiguration.FlushMode mode;
Timestamp d1 = null;
Timestamp d2 = null;
long millis;
long seconds;
int recordCount = 1000;
mode = SessionConfiguration.FlushMode.MANUAL_FLUSH;
d1 = new Timestamp(System.currentTimeMillis());
insertManual(session, table, recordCount);
d2 = new Timestamp(System.currentTimeMillis());
millis = d2.getTime() - d1.getTime();
seconds = millis / 1000 % 60;
System.out.println(mode.name() + "耗时秒数:" + seconds); } catch (KuduException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
} finally {
if (!session.isClosed()) {
session.close();
}
} } //仅支持手动flush的测试用例
public static void insertManual(KuduSession session, KuduTable table, int recordCount) throws Exception { SessionConfiguration.FlushMode mode = SessionConfiguration.FlushMode.MANUAL_FLUSH;
session.setFlushMode(mode);
//如果不设置,默认是1000
session.setMutationBufferSpace(mutationBufferSpace); int uncommit = 0;
for (int i = 0; i < recordCount; i++) {
System.out.println(i);
Insert insert = table.newInsert();
PartialRow row = insert.getRow();
UUID uuid = UUID.randomUUID();
row.addString("id", uuid.toString());
row.addInt("int_value", i);
row.addLong("bigint_value", 10000L);
session.apply(insert);
// 对于手工提交, 需要buffer在未满的时候flush,这里采用了buffer一半时即提交
// uncommit = uncommit + 1;
// if (uncommit > OPERATION_BATCH / 2) {
// session.flush();
// uncommit = 0;
// }
} // 对于手工提交, 保证完成最后的提交
if (uncommit > 0) {
List<OperationResponse> lst = session.flush();
for(OperationResponse oor :lst){
System.out.println(oor.getRowError());
}
}
} //仅支持自动flush的测试用例
public static void insertInAutoSync(KuduSession session, KuduTable table, int recordCount) throws Exception {
// SessionConfiguration.FlushMode.AUTO_FLUSH_BACKGROUND
// SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC
// SessionConfiguration.FlushMode.MANUAL_FLUSH
SessionConfiguration.FlushMode mode = SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC;
//如果不设置,默认是1000
session.setFlushMode(mode); for (int i = 0; i < recordCount; i++) {
Insert insert = table.newInsert();
PartialRow row = insert.getRow();
UUID uuid = UUID.randomUUID();
row.addString("id", uuid.toString());
row.addInt("int_value", 100);
row.addLong("bigint_value", 10000L);
session.apply(insert);
}
} }
maven配置
<dependencies>
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <dependency>
<groupId>org.apache.kudu</groupId>
<artifactId>kudu-client</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.9</version>
</dependency>
</dependencies>
Kudu-java数据库简单操作的更多相关文章
- Java 数据库简单操作类
数据库操作类,将所有连接数据库的配置信息以及基本的CRUD操作封装在一个类里,方便项目里使用,将连接数据库的基本信息放在配置文件 "dbinfo.properties" 中,通过类 ...
- github上创建java项目简单操作
github上创建java项目简单操作 参考L: github上创建java项目简单操作 - CSDN博客http://blog.csdn.net/qq_29392425/article/detail ...
- MongoDB数据库简单操作
之前学过的有mysql数据库,现在我们学习一种非关系型数据库 一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数 ...
- SQL数据库简单操作
sql语言简介 (1)数据库是文件系统,使用标准sql对数据库进行操作 * 标准sql,在mysql里面使用语句,在oracle.db2都可以使用这个语句 (2)什么是sql * Structured ...
- MySQL数据库简单操作
title date tags layout MySQL简单操作 2018-07-16 Linux post 登录mysql mysql -h 主机名 -u 用户名 -p 查看所有数据库 show d ...
- mongodb之java CRUD 简单操作
我下载的是 mongo-2.8.0.jar — Version 2.8.0 打开mongo shell -- 新建数据库test --( use test) 打开eclipse新建工程,把junit, ...
- 使用SQLiteOpenHelper类对数据库简单操作
实现数据库基本操作 数据库创建的问题解决了,接下来就该使用数据库实现应用程序功能的时候了.基本的操作包括创建.读取.更新.删除,即我们通常说的CRUD(Create, Read, Upda ...
- java数据库 JDBC操作MySQL数据库常用API 部门表和员工表 创建表 添加数据 查询数据
package com.swift.department; import java.sql.Connection; import java.sql.PreparedStatement; import ...
- Oracle 数据库简单操作
现在大型企业一般都用Oracle数据库,Oracle数据库在一般采用expdp,impdp 导出导入数据,但是在操作中经常会遇到一些问题.下面来浅析这些问题. 1. 导出数据 一般导出数据的时候需要建 ...
- android数据库简单操作
1.DbOpenHelper package com.example.dbtest.dbHelper; import android.content.Context; import android.d ...
随机推荐
- Git和Gitlab
参考 http://www.cnblogs.com/clsn/p/7929958.html#auto_id_16https://backlog.com/git-tutorial/cn/intro/in ...
- POJ 1743 Musical Theme (字符串HASH+二分)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
- dtrace 相关资料
Here are some DTrace information sources: * [Oracle Wiki](https://wikis.oracle.com/display/DTrace/DT ...
- delphi SPCOMM的一些用法注意
使用串口SPCOMM接收数据的时候0x11和0x13无法接受,从时间间隔上看来可以接收,但是无法显示.网上查错误得: --------------------------------------- ...
- ICE概述
网络通信引擎(Internet Communications Engine, Ice)是由ZeroC的分布式系统开发专家实现的一种高性能.面向对象的中间件平台.它号称标准统一,开源,跨平台,跨语言,分 ...
- eclipse中设置自定义文档签名(工具)
今天第一次认真学习eclipse的使用,看到自定义文档签名,步骤如下: 1.点击window->preferences->java->Code Style->Code Tem ...
- 【转】iPhone易被窃听应用三分钟即可获取所有信息
2011年8月9日10:19 “你有iPhone吗?这下你麻烦了!”昨天香港<东方日报>封面文章用这样的语气报道说,一种iPhone等智能手机窃听程序,正引爆香港. 该报记者亲自试验,发现 ...
- linux 除了某个文件或某个文件夹以外全部删除
比如一个目录下有1,2,3,4,5这五个文件,现在我需要删除除了2以外的所有文件,那么我可以使用 find . ! -name 2 -exec rm -f {} \; 当然你还可以配合着 -maxde ...
- android开发:全屏和退出全屏
android开发:全屏和退出全屏 from://http://blog.csdn.net/dyllove98/article/details/8831933 2013-04-21 20:31 413 ...
- Struts2学习笔记——Struts2与Spring整合
Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的 ...