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) {
String masterAddr = "192.168.56.56" ;
KuduClient client = new KuduClient. KuduClientBuilder ( masterAddr)
. defaultSocketReadTimeoutMs ( 6000 ) . build ( ) ;
LinkedList< ColumnSchema> columns = new LinkedList < > ( ) ;
columns. add ( newColumn ( "id" , Type. INT32, true ) ) ;
columns. add ( newColumn ( "name" , Type. STRING, false ) ) ;
Schema schema = new Schema ( columns) ;
CreateTableOptions options = new CreateTableOptions ( ) ;
LinkedList< String> parcols = new LinkedList < > ( ) ;
parcols. add ( "id" ) ;
options. setNumReplicas ( 1 ) ;
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) {
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) {
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) {
String masterAddr = "192.168.56.56" ;
KuduClient client = new KuduClient. KuduClientBuilder ( masterAddr)
. defaultSocketReadTimeoutMs ( 6000 ) . build ( ) ;
try {
KuduTable table = client. openTable ( "student" ) ;
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) {
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) {
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 ( ) ;
}
}
}
}