Search in sources :

Example 1 with ContentKey

use of org.projectnessie.model.ContentKey in project nessie by projectnessie.

the class TestNessieIcebergViews method verifyViewInNessie.

private void verifyViewInNessie(TableIdentifier viewIdentifier, View icebergView) throws NessieNotFoundException {
    ContentKey contentKey = ContentKey.of(viewIdentifier.toString().split("\\."));
    Map<ContentKey, Content> contentMap = api.getContent().key(contentKey).refName(BRANCH).get();
    assertThat(contentMap).hasSize(1).containsKey(contentKey);
    Content content = contentMap.get(contentKey);
    assertThat(content.unwrap(IcebergView.class)).isPresent();
    IcebergView view = content.unwrap(IcebergView.class).get();
    assertThat(metadataFilesForViewsPath(viewIdentifier.name())).contains(view.getMetadataLocation());
    // TODO: currently the schema id is always 0
    assertThat(view.getSchemaId()).isEqualTo(icebergView.currentVersion().viewDefinition().schema().schemaId());
    assertThat(view.getVersionId()).isEqualTo(view.getVersionId());
    assertThat(view.getSqlText()).isEqualTo(icebergView.currentVersion().viewDefinition().sql());
// TODO: currently not implemented in the view definition
// assertThat(view.getDialect()).isEqualTo(viewDefinition.dialect());
}
Also used : ContentKey(org.projectnessie.model.ContentKey) Content(org.projectnessie.model.Content) IcebergView(org.projectnessie.model.IcebergView)

Example 2 with ContentKey

use of org.projectnessie.model.ContentKey in project nessie by projectnessie.

the class AbstractSparkSqlTest method commitAndReturnLog.

private List<Object[]> commitAndReturnLog(String branch) throws NessieConflictException, NessieNotFoundException {
    assertThat(sql("CREATE BRANCH %s IN nessie", branch)).containsExactly(row("Branch", branch, hash));
    ContentKey key = ContentKey.of("table", "name");
    CommitMeta cm1 = ImmutableCommitMeta.builder().author("sue").authorTime(Instant.ofEpochMilli(1)).message("1").putProperties("test", "123").build();
    CommitMeta cm2 = ImmutableCommitMeta.builder().author("janet").authorTime(Instant.ofEpochMilli(10)).message("2").putProperties("test", "123").build();
    CommitMeta cm3 = ImmutableCommitMeta.builder().author("alice").authorTime(Instant.ofEpochMilli(100)).message("3").putProperties("test", "123").build();
    Operations ops = ImmutableOperations.builder().addOperations(Operation.Put.of(key, IcebergTable.of("foo", 42, 42, 42, 42))).commitMeta(cm1).build();
    Operations ops2 = ImmutableOperations.builder().addOperations(Operation.Put.of(key, IcebergTable.of("bar", 42, 42, 42, 42))).commitMeta(cm2).build();
    Operations ops3 = ImmutableOperations.builder().addOperations(Operation.Put.of(key, IcebergTable.of("baz", 42, 42, 42, 42))).commitMeta(cm3).build();
    Branch ref1 = api.commitMultipleOperations().branchName(branch).hash(hash).operations(ops.getOperations()).commitMeta(ops.getCommitMeta()).commit();
    Branch ref2 = api.commitMultipleOperations().branchName(branch).hash(ref1.getHash()).operations(ops2.getOperations()).commitMeta(ops2.getCommitMeta()).commit();
    Branch ref3 = api.commitMultipleOperations().branchName(branch).hash(ref2.getHash()).operations(ops3.getOperations()).commitMeta(ops3.getCommitMeta()).commit();
    List<Object[]> resultList = new ArrayList<>();
    resultList.add(cmToRow(cm3, ref3.getHash()));
    resultList.add(cmToRow(cm2, ref2.getHash()));
    resultList.add(cmToRow(cm1, ref1.getHash()));
    return resultList;
}
Also used : ContentKey(org.projectnessie.model.ContentKey) Branch(org.projectnessie.model.Branch) ArrayList(java.util.ArrayList) ImmutableCommitMeta(org.projectnessie.model.ImmutableCommitMeta) CommitMeta(org.projectnessie.model.CommitMeta) ImmutableOperations(org.projectnessie.model.ImmutableOperations) Operations(org.projectnessie.model.Operations)

Example 3 with ContentKey

use of org.projectnessie.model.ContentKey in project nessie by projectnessie.

the class ITUpgradePath method commit.

@Test
@Order(103)
void commit() throws Exception {
    ContentKey key = ContentKey.of("my", "tables", "table_name");
    IcebergTable content = IcebergTable.of("metadata-location", 42L, 43, 44, 45, "content-id-" + version);
    String commitMessage = "hello world " + version;
    Put operation = Put.of(key, content);
    Branch branchNew = commitMaybeRetry(api.commitMultipleOperations().commitMeta(CommitMeta.fromMessage(commitMessage)).operation(operation).branch(versionBranch));
    assertThat(branchNew).isNotEqualTo(versionBranch).extracting(Branch::getName).isEqualTo(versionBranch.getName());
    expectedRefLogEntry("COMMIT");
}
Also used : ContentKey(org.projectnessie.model.ContentKey) Branch(org.projectnessie.model.Branch) IcebergTable(org.projectnessie.model.IcebergTable) Put(org.projectnessie.model.Operation.Put) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 4 with ContentKey

use of org.projectnessie.model.ContentKey in project nessie by projectnessie.

the class GenerateContent method execute.

@Override
public void execute() throws BaseNessieClientServerException {
    if (runtimeDuration != null) {
        if (runtimeDuration.isZero() || runtimeDuration.isNegative()) {
            throw new ParameterException(spec.commandLine(), "Duration must be absent to greater than zero.");
        }
    }
    Duration perCommitDuration = Optional.ofNullable(runtimeDuration).orElse(Duration.ZERO).dividedBy(numCommits);
    ThreadLocalRandom random = ThreadLocalRandom.current();
    String runStartTime = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss").format(LocalDateTime.now());
    List<ContentKey> tableNames = IntStream.range(0, numTables).mapToObj(i -> ContentKey.of(String.format("create-contents-%s", runStartTime), "contents", Integer.toString(i))).collect(Collectors.toList());
    try (NessieApiV1 api = createNessieApiInstance()) {
        Branch defaultBranch;
        if (defaultBranchName == null) {
            // Use the server's default branch.
            defaultBranch = api.getDefaultBranch();
        } else {
            // Use the specified default branch.
            try {
                defaultBranch = (Branch) api.getReference().refName(defaultBranchName).get();
            } catch (NessieReferenceNotFoundException e) {
                // Create branch if it does not exist.
                defaultBranch = api.getDefaultBranch();
                defaultBranch = (Branch) api.createReference().reference(Branch.of(defaultBranchName, defaultBranch.getHash())).sourceRefName(defaultBranch.getName()).create();
            }
        }
        List<String> branches = new ArrayList<>();
        branches.add(defaultBranch.getName());
        while (branches.size() < branchCount) {
            // Create a new branch
            String newBranchName = "branch-" + runStartTime + "_" + (branches.size() - 1);
            Branch branch = Branch.of(newBranchName, defaultBranch.getHash());
            spec.commandLine().getOut().printf("Creating branch '%s' from '%s' at %s%n", branch.getName(), defaultBranch.getName(), branch.getHash());
            api.createReference().reference(branch).sourceRefName(defaultBranch.getName()).create();
            branches.add(newBranchName);
        }
        spec.commandLine().getOut().printf("Starting contents generation, %d commits...%n", numCommits);
        for (int i = 0; i < numCommits; i++) {
            // Choose a random branch to commit to
            String branchName = branches.get(random.nextInt(branches.size()));
            Branch commitToBranch = (Branch) api.getReference().refName(branchName).get();
            ContentKey tableName = tableNames.get(random.nextInt(tableNames.size()));
            Content tableContents = api.getContent().refName(branchName).key(tableName).get().get(tableName);
            Content newContents = createContents(tableContents, random);
            spec.commandLine().getOut().printf("Committing content-key '%s' to branch '%s' at %s%n", tableName, commitToBranch.getName(), commitToBranch.getHash());
            CommitMultipleOperationsBuilder commit = api.commitMultipleOperations().branch(commitToBranch).commitMeta(CommitMeta.builder().message(String.format("%s table %s on %s, commit #%d of %d", tableContents != null ? "Update" : "Create", tableName, branchName, i, numCommits)).author(System.getProperty("user.name")).authorTime(Instant.now()).build());
            if (newContents instanceof IcebergTable || newContents instanceof IcebergView) {
                commit.operation(Put.of(tableName, newContents, tableContents));
            } else {
                commit.operation(Put.of(tableName, newContents));
            }
            Branch newHead = commit.commit();
            if (random.nextDouble() < newTagProbability) {
                Tag tag = Tag.of("new-tag-" + random.nextLong(), newHead.getHash());
                spec.commandLine().getOut().printf("Creating tag '%s' from '%s' at %s%n", tag.getName(), branchName, tag.getHash());
                api.createReference().reference(tag).sourceRefName(branchName).create();
            }
            try {
                TimeUnit.NANOSECONDS.sleep(perCommitDuration.toNanos());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
    }
    spec.commandLine().getOut().printf("Done creating contents.%n");
}
Also used : IntStream(java.util.stream.IntStream) ImmutableIcebergView(org.projectnessie.model.ImmutableIcebergView) Put(org.projectnessie.model.Operation.Put) LocalDateTime(java.time.LocalDateTime) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ParameterException(picocli.CommandLine.ParameterException) Spec(picocli.CommandLine.Spec) ArrayList(java.util.ArrayList) Duration(java.time.Duration) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Content(org.projectnessie.model.Content) CommitMeta(org.projectnessie.model.CommitMeta) Command(picocli.CommandLine.Command) Branch(org.projectnessie.model.Branch) ImmutableDeltaLakeTable(org.projectnessie.model.ImmutableDeltaLakeTable) Min(javax.validation.constraints.Min) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) NessieApiV1(org.projectnessie.client.api.NessieApiV1) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ImmutableIcebergTable(org.projectnessie.model.ImmutableIcebergTable) Option(picocli.CommandLine.Option) IcebergView(org.projectnessie.model.IcebergView) IcebergTable(org.projectnessie.model.IcebergTable) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) Tag(org.projectnessie.model.Tag) BaseNessieClientServerException(org.projectnessie.error.BaseNessieClientServerException) ContentKey(org.projectnessie.model.ContentKey) CommandSpec(picocli.CommandLine.Model.CommandSpec) CommitMultipleOperationsBuilder(org.projectnessie.client.api.CommitMultipleOperationsBuilder) CommitMultipleOperationsBuilder(org.projectnessie.client.api.CommitMultipleOperationsBuilder) ArrayList(java.util.ArrayList) Duration(java.time.Duration) ImmutableIcebergView(org.projectnessie.model.ImmutableIcebergView) IcebergView(org.projectnessie.model.IcebergView) ContentKey(org.projectnessie.model.ContentKey) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) Branch(org.projectnessie.model.Branch) Content(org.projectnessie.model.Content) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ImmutableIcebergTable(org.projectnessie.model.ImmutableIcebergTable) IcebergTable(org.projectnessie.model.IcebergTable) ParameterException(picocli.CommandLine.ParameterException) Tag(org.projectnessie.model.Tag) NessieApiV1(org.projectnessie.client.api.NessieApiV1)

Example 5 with ContentKey

use of org.projectnessie.model.ContentKey in project nessie by projectnessie.

the class ReadContent method execute.

@Override
public void execute() throws NessieNotFoundException {
    try (NessieApiV1 api = createNessieApiInstance()) {
        ContentKey contentKey = ContentKey.of(key);
        spec.commandLine().getOut().printf("Reading content for key '%s'\n\n", contentKey);
        Map<ContentKey, Content> contentMap = api.getContent().refName(ref).key(contentKey).get();
        for (Map.Entry<ContentKey, Content> entry : contentMap.entrySet()) {
            spec.commandLine().getOut().printf("Key: %s\n", entry.getKey());
            if (isVerbose()) {
                List<String> key = entry.getKey().getElements();
                for (int i = 0; i < key.size(); i++) {
                    spec.commandLine().getOut().printf("  key[%d]: %s\n", i, key.get(i));
                }
            }
            spec.commandLine().getOut().printf("Value: %s\n", entry.getValue());
        }
        spec.commandLine().getOut().printf("\nDone reading content for key '%s'\n\n", contentKey);
    }
}
Also used : ContentKey(org.projectnessie.model.ContentKey) Content(org.projectnessie.model.Content) Map(java.util.Map) NessieApiV1(org.projectnessie.client.api.NessieApiV1)

Aggregations

ContentKey (org.projectnessie.model.ContentKey)35 Branch (org.projectnessie.model.Branch)20 Test (org.junit.jupiter.api.Test)15 Content (org.projectnessie.model.Content)15 IcebergTable (org.projectnessie.model.IcebergTable)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 CommitMeta (org.projectnessie.model.CommitMeta)10 Put (org.projectnessie.model.Operation.Put)7 Map (java.util.Map)6 Entry (org.projectnessie.model.EntriesResponse.Entry)6 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 NessieApiV1 (org.projectnessie.client.api.NessieApiV1)5 NessieNotFoundException (org.projectnessie.error.NessieNotFoundException)5 LogResponse (org.projectnessie.model.LogResponse)5 Reference (org.projectnessie.model.Reference)5 HashMap (java.util.HashMap)4 EnumSource (org.junit.jupiter.params.provider.EnumSource)4 NessieReferenceNotFoundException (org.projectnessie.error.NessieReferenceNotFoundException)4 LogEntry (org.projectnessie.model.LogResponse.LogEntry)4