1、依赖导入
<dependency>
<groupId>org.apache.kudu</groupId>
<artifactId>kudu-client</artifactId>
<version>1.6.0</version>
</dependency>
2、创建kudu表
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) throws KuduException {
final String masteraddr = "hadoop01,hadoop02,hadoop03";
KuduClient client = new KuduClient.KuduClientBuilder(masteraddr).defaultSocketReadTimeoutMs(6000).build();
List<ColumnSchema> columns = new LinkedList<ColumnSchema>();
columns.add(newColumn("CompanyId", Type.INT32, true));
columns.add(newColumn("WorkId", Type.INT32, false));
columns.add(newColumn("Name", Type.STRING, false));
columns.add(newColumn("Gender", Type.STRING, false));
columns.add(newColumn("Photo", Type.STRING, false));
Schema schema = new Schema(columns);
CreateTableOptions options = new CreateTableOptions();
List<String> parcols = new LinkedList<String>();
parcols.add("CompanyId");
options.setNumReplicas(1);
options.setRangePartitionColumns(parcols);
options.addHashPartitions(parcols, 3);
try {
client.createTable("PERSON", schema, options);
} catch (KuduException e) {
e.printStackTrace();
}
client.close();
}
}
3、删除kudu表
public class DropTable {
public static void main(String[] args) throws KuduException {
final String masteraddr = "hadoop01,hadoop02,hadoop03";
KuduClient client = new KuduClient.KuduClientBuilder(masteraddr).defaultSocketReadTimeoutMs(6000).build();
client.deleteTable("PERSON");
client.close();
}
}
4、插入数据到kudu
public class InsertRow {
public static void main(String[] args) throws KuduException {
final String masteraddr = "hadoop01,hadoop02,hadoop03";
KuduClient client = new KuduClient.KuduClientBuilder(masteraddr).build();
KuduTable table = client.openTable("PERSON");
KuduSession session = client.newSession();
session.setFlushMode(FlushMode.MANUAL_FLUSH);
session.setMutationBufferSpace(3000);
for (int i = 1; i < 10; i++) {
Insert insert = table.newInsert();
insert.getRow().addInt("CompanyId", i);
insert.getRow().addInt("WorkId", i);
insert.getRow().addString("Name", "lisi" + i);
insert.getRow().addString("Gender", "male");
insert.getRow().addString("Photo", "person" + i);
session.flush();
session.apply(insert);
}
session.close();
client.close();
}
}
5、查询kudu数据
public class FindRow {
public static void main(String[] args) throws KuduException {
final String masteraddr = "hadoop01,hadoop02,hadoop03";
KuduClient client = new KuduClient.KuduClientBuilder(masteraddr).defaultSocketReadTimeoutMs(6000).build();
KuduTable table = client.openTable("PERSON");
KuduScannerBuilder builder = client.newScannerBuilder(table);
KuduPredicate predicate = KuduPredicate.newComparisonPredicate(table.getSchema().getColumn("CompanyId"),
KuduPredicate.ComparisonOp.EQUAL, 1);
builder.addPredicate(predicate);
KuduScanner scaner = builder.build();
while (scaner.hasMoreRows()) {
RowResultIterator iterator = scaner.nextRows();
while (iterator.hasNext()) {
RowResult result = iterator.next();
System.out.print("CompanyId:" + result.getInt("CompanyId") +" ");
System.out.print("Name:" + result.getString("Name") +" ");
System.out.print("Gender:" + result.getString("Gender")+" ");
System.out.print("WorkId:" + result.getInt("WorkId") +" ");
System.out.println("Photo:" + result.getString("Photo")+" ");
}
}
scaner.close();
client.close();
}
}
6、修改kudu表数据
public class UpdateTable {
public static void main(String[] args) {
final String masteraddr = "hadoop01,hadoop02,hadoop03";
KuduClient client = new KuduClient.KuduClientBuilder(masteraddr).build();
KuduSession session = null;
try {
KuduTable table = client.openTable("PERSON");
session = client.newSession();
session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);
Update update = table.newUpdate();
PartialRow row1 = update.getRow();
row1.addInt("CompanyId",1);
row1.addString("Name","di");
session.apply(update);
} catch (KuduException e) {
e.printStackTrace();
}finally {
try {
session.close();
} catch (KuduException e) {
e.printStackTrace();
}
}
}
}
7、删除kudu中指定行的数据
public class DeleteRow {
public static void main(String[] args) throws KuduException {
final String masteraddr = "hadoop01,hadoop02,hadoop03";
KuduClient client = new KuduClient.KuduClientBuilder(masteraddr).build();
KuduTable table = client.openTable("PERSON");
KuduSession session = client.newSession();
final Delete delete = table.newDelete();
delete.getRow().addInt("CompanyId" , 5);
session.flush();
session.apply(delete);
session.close();
client.close();
}
}