前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java操作kudu 增删改查

java操作kudu 增删改查

作者头像
IT云清
发布2020-05-14 15:26:05
1.8K0
发布2020-05-14 15:26:05
举报
文章被收录于专栏:IT云清

1. maven导入依赖

代码语言:javascript
复制
        <dependency>
            <groupId>org.apache.kudu</groupId>
            <artifactId>kudu-client</artifactId>
            <version>1.6.0</version>
        </dependency>

2.增删改查

代码语言:javascript
复制
/**
 * @description kudu测试demo
 * @author IT云清
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class KuduTest {
    private static Logger logger = LoggerFactory.getLogger(KuduTest.class);

    private KuduClient kuduClient;
    private String kuduMaster;
    private String tableName;

    @Before
    public void init(){
        kuduMaster = "";
        tableName = "user";
        KuduClientBuilder kuduClientBuilder = new KuduClientBuilder(kuduMaster);
        kuduClientBuilder.defaultSocketReadTimeoutMs(10000);
        kuduClient = kuduClientBuilder.build();
    }

    @Test
    public void testCreateTable() throws KuduException {
        if(!kuduClient.tableExists(tableName)){
            ArrayList<ColumnSchema> columnSchemas = new ArrayList<>();
            columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).build());
            columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("name",Type.STRING).build());
            columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("date",Type.UNIXTIME_MICROS).build());//日期待定
            columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("money",Type.DOUBLE).build());//小数待定
            Schema schema = new Schema(columnSchemas);

            CreateTableOptions options = new CreateTableOptions();
            List<String> partitionList = new ArrayList<>();
            //kudu表的分区字段是什么 TODO
            partitionList.add("id");
            //按照id.hashcode % 分区数 = 分区号
            options.addHashPartitions(partitionList,6);

            kuduClient.createTable(tableName,schema,options);
        }
    }


    @Test
    public void insert() throws KuduException {
        KuduSession kuduSession = kuduClient.newSession();
        kuduSession.setFlushMode(FlushMode.AUTO_FLUSH_SYNC);

        for(int i = 0; i <= 100;i ++){
            KuduTable userTable = kuduClient.openTable(tableName);
            Insert insert = userTable.newInsert();
            PartialRow row = insert.getRow();
            row.addInt("id",i);
            row.addString("name","wang"+i);
            row.addDouble("money",100.342+i);

            kuduSession.apply(insert);
        }
    }

    @Test
    public void query() throws KuduException {
        KuduScannerBuilder kuduScannerBuilder = kuduClient
                .newScannerBuilder(kuduClient.openTable(tableName));
        List<String> columns = Arrays.asList("id","name","moeny");
        kuduScannerBuilder.setProjectedColumnNames(columns);
        KuduScanner kuduScanner = kuduScannerBuilder.build();
        while (kuduScanner.hasMoreRows()){
            RowResultIterator rowResults = kuduScanner.nextRows();
            while(rowResults.hasNext()){
                RowResult row = rowResults.next();
                logger.info("id={},name={},money={}",
                        row.getInt("id"),
                        row.getString("name"),
                        row.getDouble("money"));
            }
        }
    }
    
    @Test
    public void update() throws KuduException {
        KuduSession kuduSession = kuduClient.newSession();
        kuduSession.setFlushMode(FlushMode.AUTO_FLUSH_SYNC);
        KuduTable kuduTable = kuduClient.openTable(tableName);

//        Update update = kuduTable.newUpdate();
        //id存在就修改,不存在就新增
        Upsert upsert = kuduTable.newUpsert();
        PartialRow row = upsert.getRow();
        row.addInt("id",100000);
        row.addString("name","yangege");
        row.addDouble("money",100.222);
        kuduSession.apply(upsert);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/05/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. maven导入依赖
  • 2.增删改查
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档