自学kuduAPI小总结
【摘要】 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/PO...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>Kudu_all</artifactId> <groupId>cn.itcast.Kudu_all</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Kudu</artifactId> <repositories> <repository> <id>cdh.repo</id> <name>Cloudera Repositories</name> <url>https://repository.cloudera.com/artifactory/cloudera-repos</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <properties> <junit.version>4.10</junit.version> <maven.version>3.5.1</maven.version> <kudu.version>1.7.0-cdh5.16.1</kudu.version> </properties> <dependencies> <!-- Kudu client --> <dependency> <groupId>org.apache.kudu</groupId> <artifactId>kudu-client</artifactId> <version>1.7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.kudu</groupId> <artifactId>kudu-client-tools</artifactId> <version>1.7.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.kudu/kudu-spark2 --> <dependency> <groupId>org.apache.kudu</groupId> <artifactId>kudu-spark2_2.11</artifactId> <version>1.7.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>2.1.0</version> </dependency> <!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.12</version> </dependency> <!-- Unit testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.version}</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> <configuration> <args> <arg>-dependencyfile</arg> <arg>${project.build.directory}/.scala_dependencies</arg> </args> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
</project>
package com.itheima.kudu
import java.util
import org.junit.Test
import org.apache.kudu.{ColumnSchema, Schema, Type}
import org.apache.kudu.client._ /**
* 创建表
* 增
* 查
* 删
* 改
* 删除表
* 删库
*/
class KuduAPI {
val kuduMaster:String = "node01:7051,node02:7051,node03:7051" //表名 val tableName:String = "person"
@Test
def createTable():Unit={ val kuduClient: KuduClient = new KuduClient.KuduClientBuilder(kuduMaster).build() import scala.collection.JavaConverters._ val schema:Schema = new Schema(List[ColumnSchema]( new ColumnSchema.ColumnSchemaBuilder("id",Type.INT64).key(true).build(), new ColumnSchema.ColumnSchemaBuilder("name",Type.STRING).nullable(false).build(), new ColumnSchema.ColumnSchemaBuilder("age",Type.INT32).build() ).asJava) val options: CreateTableOptions = new CreateTableOptions() options.addHashPartitions(List("id").asJava,2) //执行表的创建 kuduClient.createTable(tableName,schema,options)
}
@Test
def insert():Unit={ val kuduClient = new KuduClient.KuduClientBuilder(kuduMaster).build() val kuduTable = kuduClient.openTable(tableName) val kuduSession: KuduSession = kuduClient.newSession() val insert: Insert = kuduTable.newInsert() insert.getRow.addLong("id",1) insert.getRow.addString(1,"String") insert.getRow.addInt("age",33) //6.执行操作 kuduSession.apply(insert) kuduSession.close()
} @Test
def select():Unit={ //1.init kuduclient val kuduClient: KuduClient = new KuduClient.KuduClientBuilder(kuduMaster).build() //2.open table val kuduTable = kuduClient.openTable(tableName) val kuduScanner: KuduScanner = kuduClient.newScannerBuilder(kuduTable).build() //4.双层遍历 while (kuduScanner.hasMoreRows){ val rowResultIterator: RowResultIterator = kuduScanner.nextRows() while (rowResultIterator.hasNext){ val rowResult = rowResultIterator.next() println(rowResult.getLong(0),rowResult.getString(1),rowResult.getInt("age")) } }
} @Test
def update():Unit={ //init kuduClient val kuduClient = new KuduClient.KuduClientBuilder(kuduMaster).build() val kudutable: KuduTable = kuduClient.openTable(tableName) val kudusSession: KuduSession = kuduClient.newSession() val kuduUpdate: Update = kudutable.newUpdate() val kudurow = kuduUpdate.getRow kudurow.addLong("id",1) kudurow.addString("name","lisi") kudusSession.apply(kuduUpdate) kudusSession.close()
} @Test
def delete():Unit={ val kuduClient = new KuduClient.KuduClientBuilder(kuduMaster).build() val kuduTable = kuduClient.openTable(tableName)
}
}
文章来源: www.jianshu.com,作者:百忍成金的虚竹,版权归原作者所有,如需转载,请联系作者。
原文链接:www.jianshu.com/p/41adce856497
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)