KuDu 常用Api(java)
1.首先创建一个maven工程,添加一下依赖
<dependency>
<groupId>org.apache.kudu</groupId>
<artifactId>kudu-client</artifactId>
<version>1.4.0</version>
</dependency>
2.创建表
package cn.wcb.kudu;
import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.CreateTableOptions;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;
import java.util.LinkedList;
public class CreateTable {
private static ColumnSchema newColumn(String name, Type type, Boolean isKey){
ColumnSchema.ColumnSchemaBuilder column =
new ColumnSchema.ColumnSchemaBuilder(name, type);
column.key(isKey);
return column.build();
}
public static void main(String[] args) {
//master地址
String masterAddr = "192.168.56.56";
//创建kudu的数据库连接
KuduClient client = new KuduClient.KuduClientBuilder(masterAddr)
.defaultSocketReadTimeoutMs(6000).build();
//设置表的schema
LinkedList<ColumnSchema> columns = new LinkedList<>();
/*
*和RDBMS不同的是,Kudu不提供自动递增列功能,因此应用程序必须始终
* 在插入期间提供完整的主键
*/
columns.add(newColumn("id",Type.INT32,true));
columns.add(newColumn("name",Type.STRING,false));
Schema schema = new Schema(columns);
//创建表时提供的所有选项
CreateTableOptions options = new CreateTableOptions();
//设置表的replica备份和分区规则
LinkedList<String> parcols = new LinkedList<>();
parcols.add("id");
//设置表的备份数
options.setNumReplicas(1);
//设置hash分区和数量
options.addHashPartitions(parcols,3);
try {
client.createTable("student",schema,options);
} catch (KuduException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (KuduException e) {
e.printStackTrace();
}
}
}
}
3.删除表
package cn.wcb.kudu;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;
public class DropTable {
public static void main(String[] args) {
//master地址
String masterAddr = "192.168.56.56";
KuduClient client = new KuduClient.KuduClientBuilder(masterAddr)
.defaultSocketReadTimeoutMs(6000).build();
try {
client.deleteTable("student");
} catch (KuduException e) {
e.printStackTrace();
}finally {
try {
client.close();
} catch (KuduException e) {
e.printStackTrace();
}
}
}
}
4.插入数据
package cn.wcb.kudu;
import org.apache.kudu.client.*;
public class InsertRow {
public static void main(String[] args) {
//master地址
String masterAddr = "192.168.56.56";
KuduClient client = new KuduClient.KuduClientBuilder(masterAddr)
.defaultSocketReadTimeoutMs(6000).build();
try {
KuduTable table = client.openTable("student");
KuduSession kuduSession = client.newSession();
kuduSession.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
kuduSession.setMutationBufferSpace(3000);
for (int i = 0; i <10 ; i++) {
Insert insert = table.newInsert();
insert.getRow().addInt("id",i);
insert.getRow().addString("name",i+"号");
kuduSession.flush();
kuduSession.apply(insert);
}
kuduSession.close();
} catch (KuduException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (KuduException e) {
e.printStackTrace();
}
}
}
}
5.查询数据
package cn.wcb.kudu;
import org.apache.kudu.client.*;
public class FindRow {
public static void main(String[] args) {
//master地址
String masterAddr = "192.168.56.56";
KuduClient client = new KuduClient.KuduClientBuilder(masterAddr)
.defaultSocketReadTimeoutMs(6000).build();
try {
KuduTable table = client.openTable("student");
//创建scanner扫描
KuduScanner scanner = client.newScannerBuilder(table).build();
//遍历数据
while(scanner.hasMoreRows()){
for (RowResult rowResult : scanner.nextRows()){
System.out.println(rowResult.getInt("id")
+"\t"+rowResult.getString("name"));
}
}
} catch (KuduException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (KuduException e) {
e.printStackTrace();
}
}
}
}
6.更改表数据
package cn.wcb.kudu;
import org.apache.kudu.client.*;
public class UpdateRow {
public static void main(String[] args) {
//master地址
String masterAddr = "192.168.56.56";
KuduClient client = new KuduClient.KuduClientBuilder(masterAddr)
.defaultSocketReadTimeoutMs(6000).build();
try {
KuduTable table = client.openTable("student");
KuduSession session = client.newSession();
session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);
//更新数据
Update update = table.newUpdate();
PartialRow row = update.getRow();
row.addInt("id",1);
row.addString("name","didi");
session.apply(update);
session.close();
} catch (KuduException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (KuduException e) {
e.printStackTrace();
}
}
}
}
7.删除指定行
package cn.wcb.kudu;
import org.apache.kudu.client.*;
public class DeleteRow {
public static void main(String[] args) {
//master地址
String masterAddr = "192.168.56.56";
KuduClient client = new KuduClient.KuduClientBuilder(masterAddr)
.defaultSocketReadTimeoutMs(6000).build();
try {
KuduTable table = client.openTable("student");
KuduSession session = client.newSession();
//删除数据
Delete delete = table.newDelete();
//注意:行删除和更新操作必须指定要更改行的行的完整主键
delete.getRow().addInt("id",1);
session.flush();
session.apply(delete);
session.close();
} catch (KuduException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (KuduException e) {
e.printStackTrace();
}
}
}
}
版权声明:本文为weixin_43270493原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。