package test;
import java.util.LinkedList;
import java.util.List;
import org.apache.kudu.ColumnSchema;
import org.apache.kudu.ColumnSchema.ColumnSchemaBuilder;
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;
/**
* kudu创建表
* @author Administrator
*
*/
public class CreateTable {
private static ColumnSchema newColumn(String name, Type type, boolean iskey) {
ColumnSchemaBuilder column = new ColumnSchema.ColumnSchemaBuilder(name, type);
column.key(iskey);
return column.build();
}
public static void main(String[] args) throws KuduException {
final String masteraddr = "192.168.0.128";
KuduClient client = new KuduClient.KuduClientBuilder(masteraddr).defaultSocketReadTimeoutMs(6000).build();
List<ColumnSchema> columns = new LinkedList<>();
columns.add(newColumn("CompanyId", Type.INT16, true));
columns.add(newColumn("WorkId", Type.INT16, false));
columns.add(newColumn("Name", Type.STRING, true));
columns.add(newColumn("Gender", Type.STRING, false));
columns.add(newColumn("Desc", Type.STRING, false));
columns.add(newColumn("Photo", Type.BINARY, false));
Schema schema = new Schema(columns);
CreateTableOptions options = new CreateTableOptions();
List<String> parcols = new LinkedList<>();
parcols.add("CompanyId");
options.setNumReplicas(1);
options.setRangePartitionColumns(parcols);
try {
client.createTable("PERSON", schema, options);
} catch (KuduException e) {
e.printStackTrace();
}
client.close();
}
}
package test
import org.apache.kudu.client.KuduClient
import org.apache.kudu.client.KuduException
import org.apache.kudu.client.KuduPredicate
import org.apache.kudu.client.KuduScanner
import org.apache.kudu.client.KuduSortScanerBuilder
import org.apache.kudu.client.KuduPredicate.ComparisonOp
import org.apache.kudu.client.KuduTable
import org.apache.kudu.client.RowResult
import org.apache.kudu.client.RowResultIterator
import org.apache.kudu.client.KuduScanner.KuduScannerBuilder
public class FindRow {
public static void main(String[] args) throws KuduException {
//master地址
final String masteraddr = "192.168.0.128"
//创建kudu的数据库链接
KuduClient client = new KuduClient.KuduClientBuilder(masteraddr).defaultSocketReadTimeoutMs(6000).build()
//打开表
KuduTable table = client.openTable("PERSON")
KuduScannerBuilder builder = client.newScannerBuilder(table)
//newComparisonPredicate 在一个整数或时间戳列上创建一个新的比较谓词。
KuduPredicate predicate = KuduPredicate.newComparisonPredicate(table.getSchema().getColumn("CompanyId"),
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.println("CompanyId:" + result.getInt("CompanyId"))
System.out.println("Name:" + result.getString("Name"))
System.out.println("Gender:" + result.getString("Gender"))
System.out.println("Desc:" + result.getString("Desc"))
}
}
scaner.close()
client.close()
}
}
package test
import org.apache.kudu.client.Insert
import org.apache.kudu.client.KuduClient
import org.apache.kudu.client.KuduException
import org.apache.kudu.client.KuduSession
import org.apache.kudu.client.KuduTable
import org.apache.kudu.client.SessionConfiguration.FlushMode
public class InsertRow {
public static void main(String[] args) throws KuduException {
// master地址
final String masteraddr = "192.168.0.128"
// 创建kudu的数据库链接
KuduClient client = new KuduClient.KuduClientBuilder(masteraddr).build()
// 打开表
KuduTable table = client.openTable("PERSON")
// 创建写session,kudu必须通过session写入
KuduSession session = client.newSession()
// 采取Flush方式 手动刷新
session.setFlushMode(FlushMode.MANUAL_FLUSH)
session.setMutationBufferSpace(3000)
for (int i = 1
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("Desc", "desc of the person"+ i)
insert.getRow().addBinary("Photo", "person".getBytes())
session.flush()
session.apply(insert)
}
session.close()
client.close()
}
}