kudu java_KUDU的java操作

5、KUDU的java操作

5.1、导入依赖

org.apache.kudu

kudu-client

${kudu.version}

test

org.apache.kudu

kudu-client-tools

${kudu.version}

5.2、API

5.2.1、表创建

/**

* 创建表

* @throws Exception

*/

public static void createTable() throws Exception{

//1、创建一个client

KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();

//2、创建schema信息

List columns = new ArrayList();

columns.add(new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).nullable(false).build());

columns.add(new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).key(false).nullable(false).build());

columns.add(new ColumnSchema.ColumnSchemaBuilder("age", Type.INT32).key(false).nullable(false).build());

Schema schema = new Schema(columns);

//3、指定分区字段

List partions = new ArrayList();

partions.add("id");

//4、指定分区方式为hash分区、6个分区,一个副本

CreateTableOptions options = new CreateTableOptions().addHashPartitions(partions, 6).setNumReplicas(1);

//5、创建表,

client.createTable("person",schema,options);

client.close();

}

5.2.2、Insert

/**

* 插入数据

* @throws Exception

*/

public static void add() throws Exception{

//1、创建一个client

KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();

//2、打开表

KuduTable table = client.openTable("person");

//3、创建一个session会话

KuduSession session = client.newSession();

//4、创建插入

Insert insert = table.newInsert();

//5、指定插入数据

insert.getRow().addInt("id",1);

insert.getRow().addInt("age",18);

insert.getRow().addString("name","张三");

//6、应用插入

session.apply(insert);

session.close();

client.close();

}

5.2.3、update

/**

* 更新数据

* @throws Exception

*/

public static void update() throws Exception{

//1、创建kudu client

KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();

//2、打开表

KuduTable table = client.openTable("person");

KuduSession session = client.newSession();

Update update = table.newUpdate();

update.getRow().addInt("id",1);

update.getRow().addString("name","李四");

session.apply(update);

session.flush();

session.close();

client.close();

}

5.2.4、delete

/**

* 删除数据

* @throws Exception

*/

public static void delete() throws Exception{

//1、创建kudu client

KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();

//2、打开表

KuduTable table = client.openTable("person");

KuduSession session = client.newSession();

Delete delete = table.newDelete();

delete.getRow().addInt("id",1);

session.apply(delete);

session.flush();

session.close();

client.close();

}

5.2.5、query

/**

* 条件查询 select * from person where id=1

* @throws Exception

*/

public static void query() throws Exception{

//1、创建kudu client

KuduClient client = new KuduClientBuilder(KUDU_MASTER).build();

//2、打开表

KuduTable table = client.openTable("person");

//3、创建scanner扫描器

KuduScanner.KuduScannerBuilder kuduScannerBuilder = client.newScannerBuilder(table);

//4、创建查询条件

KuduPredicate filter = KuduPredicate.newComparisonPredicate(table.getSchema().getColumn("id"), KuduPredicate.ComparisonOp.EQUAL, 1);

//5、将查询条件加入到scanner中

KuduScanner scanner = kuduScannerBuilder.addPredicate(filter).build();

//6、获取查询结果

while (scanner.hasMoreRows()){

RowResultIterator rows = scanner.nextRows();

while (rows.hasNext()){

RowResult row = rows.next();

Integer id = row.getInt("id");

String name = row.getString("name");

int age = row.getInt("age");

System.out.println(id+"---"+name+"---"+age);

}

}

//7、关闭client

client.close();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值