导入Maven坐标
<dependency>
<groupId>org.apache.kudu</groupId>
<artifactId>kudu-client</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
对表操作API
import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TableTest {
private KuduClient kuduClient = null;
@Before
public void init() {
kuduClient = new KuduClient
.KuduClientBuilder("node2:7051")
.defaultSocketReadTimeoutMs(10000)
.defaultOperationTimeoutMs(10000)
.build();
}
@Test
public void alterKuduTableDropColumn() throws KuduException {
AlterTableOptions ato = new AlterTableOptions();
ato.dropColumn("gender");
AlterTableResponse response = kuduClient.alterTable("users", ato);
System.out.println("TableId: " + response.getTableId());
}
@Test
public void alterTableAddColumn() throws KuduException {
AlterTableOptions ato = new AlterTableOptions();
ato.addColumn("gender", Type.STRING, "X");
AlterTableResponse response = kuduClient.alterTable("users", ato);
System.out.println("TableId: " + response.getTableId());
}
@Test
public void dropKuduTable() throws KuduException {
if (kuduClient.tableExists("users")) {
DeleteTableResponse response = kuduClient.deleteTable("users");
System.out.println(response.getElapsedMillis());
}
}
@Test
public void createTableMultiLevel() throws KuduException {
List<ColumnSchema> list = new ArrayList<>();
list.add(newColumnSchema("id", Type.INT32, true));
list.add(newColumnSchema("age", Type.INT32, true));
list.add(newColumnSchema("name", Type.STRING, false));
Schema schema = new Schema(list);
CreateTableOptions options = new CreateTableOptions();
options.addHashPartitions(Collections.singletonList("id"), 5);
options.setRangePartitionColumns(Collections.singletonList("age"));
PartialRow lower = new PartialRow(schema);
PartialRow upper21 = new PartialRow(schema);
upper21.addInt("age", 21);
options.addRangePartition(lower, upper21);
PartialRow lower21 = new PartialRow(schema);
lower21.addInt("age", 21);
PartialRow upper41 = new PartialRow(schema);
upper41.addInt("age", 41);
options.addRangePartition(lower21, upper41);
PartialRow lower41 = new PartialRow(schema);
lower41.addInt("age", 41);
PartialRow upper = new PartialRow(schema);
options.addRangePartition(lower41, upper);
options.setNumReplicas(1);
KuduTable kuduTable = kuduClient.createTable("users_MultiLevel", schema, options);
System.out.println("TableId: " + kuduTable.getTableId());
}
@Test
public void createTableByRange() throws KuduException {
List<ColumnSchema> list = new ArrayList<>();
list.add(newColumnSchema("id", Type.INT32, true));
list.add(newColumnSchema("name", Type.STRING, false));
list.add(newColumnSchema("age", Type.INT8, false));
Schema schema = new Schema(list);
CreateTableOptions options = new CreateTableOptions();
options.setRangePartitionColumns(Collections.singletonList("id"));
PartialRow lower = new PartialRow(schema);
PartialRow upper100 = new PartialRow(schema);
upper100.addInt("id", 100);
options.addRangePartition(lower, upper100);
PartialRow lower100 = new PartialRow(schema);
lower100.addInt("id", 100);
PartialRow upper500 = new PartialRow(schema);
upper500.addInt("id", 500);
options.addRangePartition(lower100, upper500);
PartialRow lower500 = new PartialRow(schema);
lower500.addInt("id", 500);
PartialRow upper = new PartialRow(schema);
options.addRangePartition(lower500, upper);
options.setNumReplicas(1);
KuduTable kuduTable = kuduClient.createTable("users_range", schema, options);
System.out.println("TableId: " + kuduTable.getTableId());
}
@Test
public void createKuduTable() throws KuduException {
List<ColumnSchema> list = new ArrayList<>();
list.add(newColumnSchema("id", Type.INT32, true));
list.add(newColumnSchema("name", Type.STRING, false));
list.add(newColumnSchema("age", Type.INT8, false));
Schema schema = new Schema(list);
CreateTableOptions options = new CreateTableOptions();
options.addHashPartitions(Collections.singletonList("id"), 3);
options.setNumReplicas(1);
KuduTable kuduTable = kuduClient.createTable("users", schema, options);
System.out.println("TableId: " + kuduTable.getTableId());
}
private ColumnSchema newColumnSchema(String name, Type type, boolean isKey) {
ColumnSchema.ColumnSchemaBuilder column = new ColumnSchema.ColumnSchemaBuilder(name, type);
column.key(isKey);
return column.build();
}
@After
public void close() throws KuduException {
if (kuduClient != null) {
kuduClient.close();
}
}
}
对数据CRUD操作API
import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Type;
import org.apache.kudu.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.Random;
public class DataTest {
private KuduClient kuduClient = null;
@Before
public void init() {
kuduClient = new KuduClient
.KuduClientBuilder("node2:7051")
.defaultSocketReadTimeoutMs(10000)
.defaultOperationTimeoutMs(10000)
.build();
}
@Test
public void filterQueryData() throws KuduException {
KuduTable kuduTable = kuduClient.openTable("users");
KuduScanner.KuduScannerBuilder builder = kuduClient.newScannerBuilder(kuduTable);
builder.setProjectedColumnNames(Arrays.asList("id", "age"));
builder.addPredicate(KuduPredicate.newComparisonPredicate(
newColumnSchema("id", Type.INT32, true),
KuduPredicate.ComparisonOp.GREATER,
1050
));
builder.addPredicate(KuduPredicate.newComparisonPredicate(
newColumnSchema("age", Type.INT8, false),
KuduPredicate.ComparisonOp.LESS,
(byte) 25
));
KuduScanner kuduScanner = builder.build();
int counter = 1;
while (kuduScanner.hasMoreRows()) {
System.out.println("===================" + (counter++) + "===================");
RowResultIterator rowResults = kuduScanner.nextRows();
while (rowResults.hasNext()) {
System.out.println(rowResults.next().rowToString());
}
}
}
@Test
public void selectAllData() throws KuduException {
KuduTable kuduTable = kuduClient.openTable("users");
KuduScanner kuduScanner = kuduClient.newScannerBuilder(kuduTable).build();
int counter = 1;
while (kuduScanner.hasMoreRows()) {
System.out.println("===================" + (counter++) + "===================");
RowResultIterator rowResults = kuduScanner.nextRows();
while (rowResults.hasNext()) {
System.out.println(rowResults.next().rowToString());
}
}
}
@Test
public void deleteKuduData() throws KuduException {
KuduTable kuduTable = kuduClient.openTable("users");
Delete delete = kuduTable.newDelete();
PartialRow row = delete.getRow();
row.addInt("id", 1);
KuduSession kuduSession = kuduClient.newSession();
kuduSession.apply(delete);
kuduSession.close();
}
@Test
public void upsertData() throws KuduException {
KuduTable kuduTable = kuduClient.openTable("users");
Upsert upsert = kuduTable.newUpsert();
PartialRow row = upsert.getRow();
row.addInt("id", 1);
row.addString("name", "xqh");
row.addByte("age", (byte) 22);
KuduSession kuduSession = kuduClient.newSession();
OperationResponse response = kuduSession.apply(upsert);
System.out.println(response.getElapsedMillis());
kuduSession.close();
}
@Test
public void updateData() throws KuduException {
KuduTable kuduTable = kuduClient.openTable("users");
Update update = kuduTable.newUpdate();
PartialRow row = update.getRow();
row.addInt("id", 1);
row.addString("name", "xqh");
row.addByte("age", (byte) 21);
KuduSession kuduSession = kuduClient.newSession();
OperationResponse response = kuduSession.apply(update);
System.out.println(response.getElapsedMillis());
kuduSession.close();
}
@Test
public void insertMultipleDataTest() throws KuduException {
KuduTable kuduTable = kuduClient.openTable("users");
KuduSession kuduSession = kuduClient.newSession();
kuduSession.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
kuduSession.setMutationBufferSpace(1000);
Random random = new Random();
for (int i = 1; i <= 100; i++) {
Insert insert = kuduTable.newInsert();
PartialRow row = insert.getRow();
row.addInt("id", 1000 + i);
row.addString("name", "zhangsan-" + i);
row.addByte("age", (byte) (random.nextInt(10) + 20));
kuduSession.apply(insert);
}
kuduSession.flush();
kuduSession.close();
}
@Test
public void insertSingleDataTest() throws KuduException {
KuduTable kuduTable = kuduClient.openTable("users");
Insert insert = kuduTable.newInsert();
PartialRow row = insert.getRow();
row.addInt("id", 1);
row.addString("name", "zhangsan");
row.addByte("age", (byte) 26);
KuduSession kuduSession = kuduClient.newSession();
kuduSession.apply(insert);
kuduSession.close();
}
private ColumnSchema newColumnSchema(String name, Type type, boolean isKey) {
ColumnSchema.ColumnSchemaBuilder column = new ColumnSchema.ColumnSchemaBuilder(name, type);
column.key(isKey);
return column.build();
}
@After
public void close() throws KuduException {
if (kuduClient != null) {
kuduClient.close();
}
}
}