Mavenを用いたKuduクライアントの導入
<dependency>
<groupId>org.apache.kudu</groupId>
<artifactId>kudu-client</artifactId>
<version>1.6.0</version>
</dependency>
テーブル作成の実装
public class KuduTableCreator {
private static ColumnSchema buildColumn(String colName, Type dataType, boolean primaryKey) {
return new ColumnSchema.ColumnSchemaBuilder(colName, dataType)
.key(primaryKey)
.build();
}
public static void main(String[] args) {
final String masterNodes = "master-node1,master-node2";
try (KuduClient client = new KuduClient.KuduClientBuilder(masterNodes).build()) {
List<ColumnSchema> schemaColumns = Arrays.asList(
buildColumn("OrgID", Type.INT32, true),
buildColumn("EmployeeID", Type.INT32, false),
buildColumn("FullName", Type.STRING, false)
);
CreateTableOptions tableConfig = new CreateTableOptions()
.setNumReplicas(2)
.setRangePartitionColumns(List.of("OrgID"))
.addHashPartitions(List.of("OrgID"), 4);
client.createTable("EmployeeData", new Schema(schemaColumns), tableConfig);
} catch (Exception e) {
e.printStackTrace();
}
}
}
テーブル削除処理
public class TableRemover {
public static void main(String[] args) {
try (KuduClient client = new KuduClient.KuduClientBuilder("master-nodes").build()) {
client.deleteTable("EmployeeData");
} catch (KuduException e) {
e.printStackTrace();
}
}
}
データ登録方法
public class DataInserter {
public static void main(String[] args) {
try (KuduClient client = new KuduClient.KuduClientBuilder("cluster-masters").build();
KuduSession session = client.newSession()) {
KuduTable table = client.openTable("EmployeeData");
session.setFlushMode(FlushMode.AUTO_FLUSH_BACKGROUND);
for (int i = 1; i <= 50; i++) {
Insert record = table.newInsert();
record.getRow()
.addInt("OrgID", i)
.addInt("EmployeeID", 1000 + i)
.addString("FullName", "Employee_" + i);
session.apply(record);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
データ検索処理
public class DataQuery {
public static void main(String[] args) {
try (KuduClient client = new KuduClient.KuduClientBuilder("kudu-masters").build()) {
KuduTable table = client.openTable("EmployeeData");
KuduScanner scanner = client.newScannerBuilder(table)
.addPredicate(
KuduPredicate.newComparisonPredicate(
table.getSchema().getColumn("OrgID"),
KuduPredicate.ComparisonOp.EQUAL,
5
)
)
.build();
for (RowResult row : scanner) {
System.out.printf(
"ID:%d, Name:%s%n",
row.getInt("EmployeeID"),
row.getString("FullName")
);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
データ更新手法
public class DataUpdater {
public static void main(String[] args) {
try (KuduClient client = new KuduClient.KuduClientBuilder("master-hosts").build();
KuduSession session = client.newSession()) {
KuduTable table = client.openTable("EmployeeData");
Update modification = table.newUpdate();
modification.getRow()
.addInt("OrgID", 3)
.addString("FullName", "Updated_Name");
session.apply(modification);
} catch (Exception e) {
e.printStackTrace();
}
}
}
行削除の実装
public class RowDeleter {
public static void main(String[] args) {
try (KuduClient client = new KuduClient.KuduClientBuilder("master-servers").build();
KuduSession session = client.newSession()) {
KuduTable table = client.openTable("EmployeeData");
Delete removal = table.newDelete();
removal.getRow().addInt("OrgID", 7);
session.apply(removal);
} catch (Exception e) {
e.printStackTrace();
}
}
}